Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebadima/75ba9d233054bd5e2e9b75f4dd824602 to your computer and use it in GitHub Desktop.
Save sebadima/75ba9d233054bd5e2e9b75f4dd824602 to your computer and use it in GitHub Desktop.
int ledPin = 13;
char* letters[] = {
“.-”, “-…”, “-.-.”, “-..”, “.”, “..-.”, “–.”, “….”, “..”,
“.—”, “-.-”, “.-..”, “–”, “-.”, “—”, “.–.”, “–.-”, “.-.”,
“…”, “-”, “..-”, “…-”, “.–”, “-..-”, “-.–”, “–..”
};
// A-I
// J-R
// S-Z
char* numbers[] = { “—–”, “.—-”, “..—”,
“…–”, “….-”, “…..”, “-….”,
“–…”, “—..”, “—-.” };
int dotDelay = 200;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char ch;
if (Serial.available()) {
ch = Serial.read();
// read a single letter
if (ch >= ‘a’ && ch <= ‘z’) {
flashSequence(letters[ch - ‘a’]);
}
else if (ch >= ‘A’ && ch <= ‘Z’) {
flashSequence(letters[ch - ‘A’]);
}
else if (ch >= ‘0’ && ch <= ‘9’) {
flashSequence(numbers[ch - ‘0’]);
}
else if (ch == ‘ ‘) {
delay(dotDelay * 4);
// gap between words
}
}
}
void flashSequence(char* sequence)
{
int i = 0;
while (sequence[i] != NULL) {
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3);
}
void flashDotOrDash(char dotOrDash)
{
digitalWrite(ledPin, HIGH);
if (dotOrDash == ‘.’) {
delay(dotDelay);
}
else // must be a -
{
delay(dotDelay * 3);
}
digitalWrite(ledPin, LOW);
delay(dotDelay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment