Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
making a monkey keychain scream a morse code message, while displaying on a little 128x64 OLED screen
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
int current_char = 0;
String str = "hello world";
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
}
void loop() {
int highlighted_char = current_char % str.length();
// write the string to the screen
display.clearDisplay();
ape_scream_string(str, 1, highlighted_char);
display.display();
// now make let Chuggzilla do his thing
morse_transmit(str[highlighted_char]);
current_char++;
}
void ape_scream_string(String s, int row, int highlightedPosition) {
for (int i = 0; i < s.length(); i++) {
ape_scream_char(s[i], i, row, highlightedPosition);
}
}
void ape_scream_char(char c, int n, int row, int highlightedPosition) {
if (n == highlightedPosition) {
display.drawChar(n * 6, row * 8, c, BLACK, WHITE, 1);
} else {
display.drawChar(n * 6, row * 8, c, WHITE, BLACK, 1);
}
}
// https://github.com/smcl/arduino_morse quickly ported from C
// using pin 2 instead of pin 13.
#define MORSE_UNIT 100
void morse_setup() {
pinMode(2, OUTPUT);
}
void key_down() {
digitalWrite(2, HIGH);
}
void key_up() {
digitalWrite(2, LOW);
}
void key_press(int n) {
key_down();
delay(n * MORSE_UNIT);
key_up();
}
void dit() {
key_press(1);
}
void dah() {
key_press(3);
}
void morse_nop() {
delay(3 * MORSE_UNIT);
}
String morse_translate(char c) {
switch (c) {
case 'a':
return ".-";
case 'b':
return "-...";
case 'c':
return "-.-.";
case 'd':
return "-..";
case 'e':
return ".";
case 'f':
return "..-.";
case 'g':
return "--.";
case 'h':
return "....";
case 'i':
return "..";
case 'j':
return ".---";
case 'k':
return "-.-";
case 'l':
return ".-..";
case 'm':
return "--";
case 'n':
return "-.";
case 'o':
return "---";
case 'p':
return ".--.";
case 'q':
return "--.-";
case 'r':
return ".-.";
case 's':
return "...";
case 't':
return "-";
case 'u':
return "..-";
case 'v':
return "...-";
case 'w':
return ".--";
case 'x':
return "-..-";
case 'y':
return "-.--";
case 'z':
return "--..";
case '0':
return "-----";
case '1':
return ".----";
case '2':
return "..---";
case '3':
return "...--";
case '4':
return "....-";
case '5':
return ".....";
case '6':
return "-....";
case '7':
return "--...";
case '8':
return "---..";
case '9':
return "----.";
default:
return " ";
}
}
void morse_transmit(char ascii_char) {
String morse_char = morse_translate(ascii_char);
delay(MORSE_UNIT);
for (int i = 0; i < morse_char.length(); i++) {
delay(MORSE_UNIT);
switch (morse_char[i]) {
case '.':
dit();
break;
case '-':
dah();
break;
default:
morse_nop();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment