/**
This program is designed to flash an led to transmit morse code.
The rules of signalling Morse Code messages are as follows:
1. Each dot is one unit, each dash is three units.
2. Leave one unit between parts of the same letter.
3. Leave three units between letters.
4. Leave seven units between words.
*/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
uint8_t greenLed = 12; // HERO board pin 12, LED
uint8_t blueLed = 11; // HEO Board pin 11, buzzer
uint8_t yellowLed = 10; // HERO Board pin 10, AM transmitter
uint8_t redLed = 8; // HERO board pin 10, used for debugging purposes
uint8_t greenSwitch = 2; // HERO board pin 2 , transmitt via radio on/off
uint8_t blueSwitch = 3; // HERO board pin 3, transmitt via sound on/off
uint8_t yellowSwitch = 4; // HERO board pin 4, transmitt visually on/off
uint8_t sensorPin = A0; // HERO boad pin A0 (analog)
int sensorValue = 0;
int dit = 250;
int dah = dit * 3;
int letterBreak = dit * 3;
int wordBreak = dit * 7;
int partBreak = dit;
// The first element of the array is the length of the actual data, sorta like string were in DOS
int letterCodes[26][5] = {
{2, dit, dah}, // A
{4, dah, dit, dit, dit}, // B
{4, dah, dit, dah, dit}, // C
{3, dah, dit, dit}, // D
{1, dit}, // E
{4, dit, dit, dah, dit}, // F
{3, dah, dah, dit}, // G
{4, dit, dit, dit, dit}, // H
{2, dit, dit}, // I
{4, dit, dah, dah, dah}, // J
{3, dah, dit, dah}, // K
{4, dit, dah, dit, dit}, // L
{2, dah, dah}, // M
{2, dah, dit}, // N
{3, dah, dah, dah}, // O
{4, dit, dah, dah, dit}, // P
{4, dah, dah, dit, dah}, // Q
{3, dit, dah, dit}, // R
{3, dit, dit, dit}, // S
{1, dah}, // T
{3, dit, dit, dah}, // U
{4, dit, dit, dit, dah}, // V
{3, dit, dah, dah}, // W
{4, dah, dit, dit, dah}, // X
{4, dah, dit, dah, dah}, // Y
{4, dah, dah, dit, dit}, // Z
};
int numberCodes[10][6] = {
{5, dah, dah, dah, dah, dah}, // '0'
{5, dit, dah, dah, dah, dah}, // '1'
{5, dit, dit, dah, dah, dah}, // '2'
{5, dit, dit, dit, dah, dah}, // '3'
{5, dit, dit, dit, dit, dah}, // '4'
{5, dit, dit, dit, dit, dit}, // '5'
{5, dah, dit, dit, dit, dit}, // '6'
{5, dah, dah, dit, dit, dit}, // '7'
{5, dah, dah, dah, dit, dit}, // '8'
{5, dah, dah, dah, dah, dit} // '9'
};
void sendLetter(const int code[]);
void sendMessage(const char message[], int);
bool checkSwitch(int switchNum);
void setup() {
Serial.begin(9600);
// setup the output pins
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenSwitch, INPUT);
pinMode(blueSwitch, INPUT);
pinMode(yellowSwitch, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// the red LED is used to signal the start of the message
digitalWrite(redLed, HIGH); // signal start of message ( just so I know when it starts during the loop)
delay(wordBreak); // keep the red led on for a bit
digitalWrite(redLed, LOW); // turn off the LED
sensorValue = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
dit = 250;
dah = dit * 3;
letterBreak = dit * 3;
wordBreak = dit * 7;
partBreak = dit;
char message[] = "I love you";
sendMessage(message, strlen(message));
delay(wordBreak);
}
void sendLetter(const int code[], bool yesTransGreen, bool yesTransBlue, bool yesTransYellow) {
for (int i = 1; i <= code[0]; i++) {
if (yesTransGreen) { digitalWrite(greenLed, HIGH); } // Turn the Green LED on
if (yesTransBlue) { digitalWrite(blueLed, HIGH); }
if (yesTransYellow) { digitalWrite(yellowLed, HIGH); }
// keep the signal on long enough to signal a dit or a dah
delay(code[i]);
digitalWrite(greenLed, LOW); // no real need to check the transmitt status on turning them off
digitalWrite(blueLed, LOW);
digitalWrite(yellowLed, LOW);
if (i < code[0]) { // Check to see if there is more code left for this character
delay(partBreak); // add in the delay between the dits and dahs if this is not the last part of the code
}
}
}
/**
*
*/
void sendMessage(const char message[], const int length) {
// get the state of the transmitt type switches
// probably need to change this logic once I learn how to initiate a transmission.
bool yesTransGreen = checkSwitch(greenSwitch);
bool yesTransBlue = checkSwitch(blueSwitch);
bool yesTransYellow = checkSwitch(yellowSwitch);
for (int i = 0; i < length; i++) {
char letter = toupper(message[i]);
if (letter >= 'A' && letter <= 'Z') {
sendLetter(letterCodes[LetterOffset(letter)], yesTransGreen, yesTransBlue, yesTransYellow);
} else if (letter >= '0' && letter <= '9') {
sendLetter(numberCodes[NumberOffset(letter)], yesTransGreen, yesTransBlue, yesTransYellow);
} else if (letter == ' ') {
delay(wordBreak);
}
delay(letterBreak); // add in the delay between leters of the word
}
}
/**
* LetterOffset finds the offset into the letterCodes
*
* A simple function that find the offset into the array by finding the integer distance
* between 'A" and the letter.
*/
int LetterOffset(char letter) {
return int(letter) - int('A');
}
/**
* NumberOffset finds the offset into the letterCodes
*
* A simple function that find the offset into the array by finding the integer distance
* between '0' and the letter.
*/
int NumberOffset(char letter) {
return int(letter) - int('0');
}
bool checkSwitch(int switchNum) {
Serial.print("Switch ");
Serial.print(switchNum);
Serial.print(" is currently ");
Serial.println(digitalRead(switchNum));
return (digitalRead(switchNum) == HIGH);
}