Last active
May 14, 2020 17:53
-
-
Save ori229/24a28dda75b7af7abf8fa8081e3fb146 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SIZE 26 | |
// See demo in https://youtu.be/vD1pToPe5N4 | |
String letters[SIZE]={ | |
// A to I | |
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", | |
// J to R | |
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", | |
// S to Z | |
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." | |
}; | |
int photocellPin = 0; // the cell and 10K pulldown are connected to a0 | |
int minToBeHigh; | |
int maxToBeLow; | |
unsigned long DOT_MIN_LEN = 100; | |
unsigned long DOT_MAX_LEN = 200; | |
unsigned long DASH_MIN_LEN = 500; | |
unsigned long MIN_TIME_BETWEEN_LETTERS = 700; | |
unsigned long MIN_TIME_BETWEEN_WORDS = 5000; | |
String lastStatus = "OFF"; | |
String statusNow; | |
unsigned long lastChangeTime; | |
String lastSignsInLetter = ""; | |
void setup(void) { | |
Serial.begin(9600); | |
// calibrate - read 50 times = 10 measures * 5 sec | |
int measuredLow = 9999; | |
int measuredHigh = 0; | |
for (int i=0; i<50 ; i++) { | |
int measured = analogRead(photocellPin); | |
print("measured = "); println(measured); | |
if (measured > measuredHigh) | |
measuredHigh = measured; | |
if (measured < measuredLow) | |
measuredLow = measured; | |
delay(100); | |
} | |
print("measuredHigh = "); println(measuredHigh); | |
print("measuredLow = "); println(measuredLow); | |
int epsilon = (measuredHigh - measuredLow) / 10; | |
print("epsilon = "); println(epsilon); | |
int avg = (measuredHigh + measuredLow) / 2; | |
minToBeHigh = avg + epsilon; | |
maxToBeLow = avg - epsilon; | |
print("minToBeHigh = "); println(minToBeHigh); | |
print("maxToBeLow = "); println(maxToBeLow); | |
lastChangeTime = millis(); | |
} | |
void loop(void) { | |
unsigned long timeSinceLastChange = millis() - lastChangeTime; | |
//print("timeSinceLastChange = "); println(timeSinceLastChange); | |
//delay(10); | |
int measured = analogRead(photocellPin); | |
//print("measured = "); println(measured); | |
if (measured > minToBeHigh) { | |
statusNow = "ON"; | |
} else if (measured < maxToBeLow) { | |
statusNow = "OFF"; | |
} else { | |
//println("measured as avg"); | |
return; | |
} | |
if (statusNow == lastStatus) { | |
return; | |
} | |
// there was a change in the light | |
//print("there was a change to "); println(statusNow); | |
//print("from "); println(lastStatus); | |
lastStatus = statusNow; | |
if (statusNow == "OFF") { | |
if (timeSinceLastChange > DOT_MIN_LEN && timeSinceLastChange < DOT_MAX_LEN) { | |
lastSignsInLetter = lastSignsInLetter + "."; | |
//print("timeSinceLastChange = "); println(timeSinceLastChange); | |
println("lastSign ."); | |
} | |
if (timeSinceLastChange > DASH_MIN_LEN) { | |
lastSignsInLetter = lastSignsInLetter + "-"; | |
//print("timeSinceLastChange = "); println(timeSinceLastChange); | |
println("lastSign -"); | |
} | |
} | |
if (statusNow == "ON") { | |
// new letter | |
if (timeSinceLastChange > MIN_TIME_BETWEEN_LETTERS) { | |
//print("timeSinceLastChange = "); println(timeSinceLastChange); | |
translateMorseToLetter(lastSignsInLetter); | |
lastSignsInLetter = ""; | |
} | |
// new word | |
if (timeSinceLastChange > MIN_TIME_BETWEEN_WORDS) { | |
println(" NEW WORD \n\n\n"); | |
} | |
} | |
lastChangeTime = millis(); | |
} | |
void translateMorseToLetter(String letterInMorse) { | |
print("end of letter: "); | |
print(letterInMorse); | |
for (int i=0; i < SIZE ; i++) { | |
if (letterInMorse == letters[i]) { | |
print (" letter num: "); | |
print (i); | |
print (" char: "); | |
char theChar[1]; | |
sprintf(theChar, "%c", (i+65)); | |
print (theChar); | |
//Serial.write('0' + (i+65)); | |
} | |
} | |
println("\n"); | |
} | |
void print (int s) { | |
Serial.print(s); | |
} | |
void println (int s) { | |
Serial.print(s); | |
Serial.println(""); | |
} | |
void print (String s) { | |
Serial.print(s); | |
} | |
void println (String s) { | |
Serial.print(s); | |
Serial.println(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment