Skip to content

Instantly share code, notes, and snippets.

@M0LTE
Last active May 14, 2018 22:12
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 M0LTE/5100306634227a77bcc03a8bd816e049 to your computer and use it in GitHub Desktop.
Save M0LTE/5100306634227a77bcc03a8bd816e049 to your computer and use it in GitHub Desktop.
Arduino pips
void setup() {
pinMode(6, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(10, HIGH);
delay(1000);
timesignal();
delay(2000);
morse("M0LTE");
delay(500);
digitalWrite(10, LOW);
while(true) {}
}
static const char* CHAR_TO_MORSE[128] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, "-.-.--", ".-..-.", NULL, NULL, NULL, NULL, ".----.",
"-.--.", "-.--.-", NULL, NULL, "--..--", "-....-", ".-.-.-", "-..-.",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...",
"---..", "----.", "---...", NULL, NULL, "-...-", NULL, "..--..",
".--.-.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--..", NULL, NULL, NULL, NULL, "..--.-",
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
};
void play(char inp){
int cur=0;
while(true){
int asc = (int)inp;
char c = CHAR_TO_MORSE[asc][cur];
if (c == 0){
return;
}
if (c == '.') {
dit();
} else {
dah();
}
cur++;
}
}
void morse(char msg[]){
int i=0;
while (true) {
if (msg[i] == 0){
return;
}
if (msg[i] == ' '){
wordspace();
} else {
play(msg[i]);
letterspace();
}
i++;
}
}
void timesignal(){
pip(100);
delay(900);
pip(100);
delay(900);
pip(100);
delay(900);
pip(100);
delay(900);
pip(100);
delay(900);
pip(500);
}
void pip(int ms){
tone(6,1000,ms);
delay(ms);
}
int ditlen=50;
void dit() {
tone(6, 1000, ditlen);
delay(ditlen);
delay(ditlen);
}
void dah() {
tone(6,1000,3*ditlen);
delay(3 * ditlen);
delay(ditlen);
}
void letterspace(){
delay(3 * ditlen);
}
void wordspace(){
delay(7 * ditlen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment