/* Hello World // Morse code .... . .-.. .-.. --- / .-- --- .-. .-.. -.. Says "Hello World" in morse code upon the press of the button. Expresses the dots and dashes of morse code by the timed blinking of an LED light. Author: Alex Berke (aberke) */ // Set up pins corresponding to my board. const int LED = 7; const int SWITCH = 8; const int ONE_SECOND = 1000; // Set up the short vs long time intervals that correspond // with morse code dots and dashes. // A "dot" is 1 unit. // A "dash" is 3 units. // A space is 7 units. const int DOT_TIME = 200; const int DASH_TIME = 3*DOT_TIME; // Hold current state of the switch (on/off). int switchState = 0; void setup() { // Initialize the LED pin as an output. pinMode(LED, OUTPUT); // Initialize the button switch as a input. pinMode(SWITCH, INPUT); } void loop() { // Read whether button switch is on vs off. switchState = digitalRead(SWITCH); if (switchState == LOW) { sayHelloWorld(); } else { digitalWrite(LED, LOW); // LED off } } void sayHelloWorld() { /* Says "Hello World" in morse code, i.e.: * .... . .-.. .-.. --- / .-- --- .-. .-.. -.. * * A dot is expressed as a short blink of the LED. * A dash is expressed as a long blink of the LED. */ // H dot(); dot(); dot(); dot(); letterSpace(); // E dot(); letterSpace(); // L dot(); dash(); dot(); dot(); letterSpace(); // L dot(); dash(); dot(); dot(); letterSpace(); // O dash(); dash(); dash(); letterSpace(); wordSpace(); // W dot(); dash(); dash(); letterSpace(); // O dash(); dash(); dash(); letterSpace(); // R dot(); dash(); dot(); letterSpace(); // L dot(); dash(); dot(); dot(); letterSpace(); // D dash(); dot(); dot(); letterSpace(); } void dot() { /* * Express a dot in morse code. */ digitalWrite(LED, HIGH); delay(DOT_TIME); digitalWrite(LED, LOW); delay(DOT_TIME); } void dash() { /* * Express a dash in morse code. */ digitalWrite(LED, HIGH); delay(DASH_TIME); digitalWrite(LED, LOW); delay(DOT_TIME); } void letterSpace() { /* * Express the space between letters in morse code. * 2 extra units of no signal. * Assumes that it follows a LOW write to the LED for 1 unit of time. */ delay(2*DOT_TIME); } void wordSpace() { /* * Express the space between letters in morse code. * 6 extra units of no signal. * Assumes that it follows a LOW write to the LED for 1 unit of time. */ delay(6*DOT_TIME); }