Skip to content

Instantly share code, notes, and snippets.

@psychemist
Last active December 10, 2025 12:47
Show Gist options
  • Select an option

  • Save psychemist/eba14514598804561bb23519b6f49af9 to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/eba14514598804561bb23519b6f49af9 to your computer and use it in GitHub Desktop.
Arduino Starter Project 12
#include <Servo.h>
Servo myServo;
// constant variables for input and output components
const int piezo = A0;
const int switchPin = 2;
const int redLED = 3;
const int yellowLED = 4;
const int greenLED = 5;
// variables to hold switch and piezo values
int knockVal;
int switchVal;
// knock thresholds
const int quietKnock = 10;
const int loudKnock = 100;
// variables for lock state and number of knocks
bool locked = false;
int numberOfKnocks = 0;
int knocksExpected = 5;
void setup() {
// set direction of digital pins
pinMode(switchPin, INPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
// initialize servo object and serial port
myServo.attach(9);
Serial.begin(9600);
// toggle green LED; unlock servo; print status to serial monitor
digitalWrite(greenLED, HIGH);
myServo.write(0);
Serial.println("The box is unlocked!");
}
void loop() {
if ( locked == false ) {
// read value from switch sensor into variable
switchVal = digitalRead(switchPin);
// if switch is locked, perform operations
if ( switchVal == HIGH ) {
// flip locked state
locked = true;
// toggle green LED to OFF and red LED to ON
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
// print status to output
myServo.write(90);
Serial.println("The box is locked!");
delay(1000);
}
}
if ( locked == true ) {
// read value from piezo sensor into variable
knockVal = analogRead(piezo);
// count only valid knocks into variables
if ( numberOfKnocks < knocksExpected && knockVal > 0 ) {
if ( checkForKnock(knockVal) == true ) {
numberOfKnocks++;
}
// print out number of knocks still needed
Serial.print(knocksExpected - numberOfKnocks);
Serial.print(" more knocks to go!");
}
// unlock if expected number of knocks has been reached
if ( numberOfKnocks >= knocksExpected ) {
locked = false;
myServo.write(0);
delay(20);
// toggle green LED to ON and red LED to OFF
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
// print lock status to output; reset variable(s)
Serial.println("The box is unlocked!");
numberOfKnocks = 0;
}
}
}
bool checkForKnock(int value) {
// check validity of knock: intensity of input from sensor reading
if ( value > quietKnock && value < loudKnock ) {
// blink yellow LED
digitalWrite(yellowLED, HIGH);
delay(50);
digitalWrite(yellowLED, LOW);
// print value of knock to serial output
Serial.print("Valid knock of value ");
Serial.println(value);
return true;
}
// if value is too quiet or loud, indicate it is invalid
else {
Serial.print("Bad knock value ");
Serial.println(value);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment