Arduino Telegraph

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
greenleaf
 
Posts: 21
Joined: Sat Jun 25, 2011 10:18 pm

Arduino Telegraph

Post by greenleaf »

Here's a short program that turns your Arudino into a simple telegraph key. It outputs dots and dashes to the serial port while simultaneously lighting up the LED on pin 13. Now you can print "Hello world" like it's 1939!

.... . .-.. .-.. --- / .-- --- .-. .-.. -..

Code: Select all

#include <Bounce.h>
#define BUTTON 2
#define LED 13

/* 
Arduino Telegraph Key - turns your Arduino into a simple morse-code device.
Pressing the button will transmit dits and dahs to the serial port, while 
lighting up the LED plugged into pin 13 (or on-board LED).

The Bounce library is required for this program to work:
http://www.arduino.cc/playground/Code/Bounce

You may do whatever you like with this code!
*/

// First set up our bounce instance, with a 20ms delay
Bounce bouncer = Bounce(BUTTON,20);

// Define variables for clicks and pauses
int clickDuration;
int pauseDuration;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Update the bouncer
  bouncer.update();
  int buttonState = bouncer.read();
  clickDuration = 0;
  pauseDuration = 0;
 
  while (buttonState == HIGH) {
    // Measure how long the button is pressed
    digitalWrite(LED, HIGH);
    clickDuration++;
    delay(1);
    bouncer.update();
    buttonState = bouncer.read();
  }
  
  if (clickDuration > 0 && clickDuration < 200) {
    Serial.print(".");                // Print a 'dit' for presses less than 200ms
  } else if (clickDuration > 200) {
    Serial.print("-");                // Print a 'dah' for presses more than 200ms
  } 
  
  while (buttonState == LOW) {
    // Measure the pause between presses
    digitalWrite(LED, LOW);
    pauseDuration++;
    delay(1);
    bouncer.update();
    buttonState = bouncer.read();
  }
  
  if (pauseDuration > 1000) {
    Serial.print(" ");                // Print a space for pauses greater than 1 second
  }
  if (pauseDuration > 3000) {
    Serial.print("/ ");               // Print a / for pauses greater than 3 seconds
  }
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”