Lab 3 — Morse Code Lock
For Lab 3, I decided to follow the prompt in the syllabus from last week and make a lock, incorporating everything we’ve learned so far. I decided to use morse code as the lock mechanism — using a button press, you had to type in the morse code for “open”, which is as follows (hyphens are long, dots are short).--- .--. . -.
I used an arduino speaker to mimic the sound of a morse code button, LED lights to signify when the box was locked or open, and a servo to open the box (this step didn’t work quite as well because my servo is extremely weak).
For the morse code, I just used Arduino’s array storage for comparison values (basically, assuming any button press over a second and less than two seconds was “long” and anything under a second was “short”), storing press values up to 10 and then wiping them if the button was inactive for more than 3 seconds.
You can see my (probably overly complex) code here:#include <Servo.h>
Servo servoMotor;
int servoPin = 9;const int PIN = 3;
const int PRESS = HIGH;
const int NOPRESS = LOW;
bool isOpen = false;
unsigned int storageIndex = 0;
int storage[10];
int combinationUpper[10] = { 200, 200, 200, 100, 200, 200, 100, 100, 200, 100 };
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servoMotor.attach(servoPin);
pinMode(3, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(6, OUTPUT);
}void loop() {
unsigned int pressCount = 0;
unsigned int noPressCount = 0;
if (digitalRead(PIN) == PRESS) {
while (digitalRead(PIN) == PRESS) {
tone(6, 500);
pressCount += 1;
delay(5);
}
}
if (digitalRead(PIN) == NOPRESS && pressCount > 0) {
noTone(6);
storage[storageIndex] = pressCount;
Serial.print("index and value: ");
Serial.println(storageIndex);
Serial.println(pressCount);
storageIndex += 1;
if (check() == 10) {
isOpen = true;
servoMotor.write(90);
Serial.println("open!!!!!!");
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}
while (digitalRead(PIN) == NOPRESS) {
noPressCount +=1;
if (noPressCount >= 3000) {
isOpen = false;
wipe();
servoMotor.write(0);
Serial.println("wiping");
check();
}
if (isOpen == false) {
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
delay(5);
}
}
}
int check() {
unsigned int check = 0;
for(int i = 0; i <= 9; i++) {
Serial.print(i);
Serial.print(": ");
Serial.println(storage[i]);
if (storage[i] <= combinationUpper[i] && storage[i] > (combinationUpper[i] - 100)) {
check += 1;
}
}
Serial.print("check: ");
Serial.println(check);
return check;
}void wipe() {
for(int i = 1; i <= 10; i++) {
storage[i] = 0;
}
}
This lab turned out fairly well, although working with Arduino code is taking some getting used to — I never know what’s going to end up throwing my timing off. My final circuit is here:
And you can see it in action here (complete with not-so-effective opening mechanism):