Blink LED
const int ledPin = 13;
const int timeInterval = 100;
void sendDOT()
{
digitalWrite(ledPin, HIGH);
delay(timeInterval);
digitalWrite(ledPin, LOW);
delay(timeInterval);
}
void sendDASH()
{
digitalWrite(ledPin, HIGH);
delay(timeInterval * 3);
digitalWrite(ledPin, LOW);
delay(timeInterval);
}
void setup() {
pinMode(ledPin,OUTPUT);
}
void loop(){
sendDOT();
sendDASH();
}
Morse example
char* morse[] = {
".-", // A
"--", // B
"..-", // C
".-.", // D
"-.-", // F
".--",
".-..",
".-.-",
".--.",
"-..-",
};
char word[] = "ABCD";
void setup(){
Serial.begin(9600);
}
void loop() {
int wordlen = strlen(word);
for(int i=0; i<wordlen; i++) {
int index = word[i] - 65;
send_morsecode(index);
}
}
void send_morsecode(int index) {
char * morsecode = morse[index];
int length = strlen(morsecode);
for(int i=0; i<length; i++) {
send_bit( morsecode[i] );
}
}
void send_bit(char b) {
Serial.print(b);
if(b == '.') {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
} else if( b=='-') {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(100);
}
}
Joost's code
/*
* Exercise: show "Hackerspace Brussels" in Morse code on the led on pin 13.
*
*
*/
static const char *bhs = "hackerspace brussels";
static const int dotMark = 1;
static const int dashMark = 3;
static const int intraCharGap = 1;
static const int betweenLettersGap = 3;
static const int betweenWordsGap = 7;
static const int wordsPerMinute = 33; // 33;
static const int charsPerMinute = wordsPerMinute * 5;
static const int unitTimeMs = (600*1000)/charsPerMinute;
static const char *codeTable[] = {
".-", // A
"-...", // B
"-.-.", // C
"-..", // D
".", // E
"..-.", // F
"--.", // G
"....", // H
"..", // I
".---", // J
"-.-", // K
".-..", // L
"--", // M
"-.", // N
"---", // O
".--.", // P
"--.-", // Q
".-.", // R
"...", // S
"-", // T
"..-", // U
"...-", // V
".--", // W
"-..-", // X
"-.--", // Y
"--..", // Z
};
static const int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
static inline void doGap(int gapSize) {
digitalWrite(ledPin, LOW);
delay(unitTimeMs * gapSize);
}
static inline void doMark(int markSize) {
digitalWrite(ledPin, HIGH);
delay(unitTimeMs * markSize);
}
static inline void doSpace() {
doGap(betweenWordsGap);
}
static inline void doChar(char c) {
c &= (0xFF-0x20); // capitalize
if (c == 0) {
doSpace();
return;
}
c -= 'A';
const char * code = codeTable[c];
while (*code != 0) {
if (*code == '-') { doMark(dashMark); }
else { doMark(dotMark); }
++code;
// make sure we don't set an intraChar gap at the end of the char
if (*code != 0) {
doGap(intraCharGap);
}
}
doGap(betweenLettersGap);
}
static inline void doTxt(const char *txt) {
while(*txt != 0) {
doChar (*txt);
++txt;
}
doSpace();
doSpace();
}
void loop() // run over and over again
{
doTxt(bhs);
}
Joost's version (serial)
/*
* Exercise: show "Hackerspace Brussels" in Morse code on the led on pin 13.
* Modification that uses the serial port.
*
*/
static const char *bhs = "hackerspace brussels";
static const int dotMark = 1;
static const int dashMark = 3;
static const int intraCharGap = 1;
static const int betweenLettersGap = 3;
static const int betweenWordsGap = 7;
static const int wordsPerMinute = 33; // 33;
static const int charsPerMinute = wordsPerMinute * 5;
static const int unitTimeMs = (1200*1000)/charsPerMinute;
static const char *codeTable[] = {
".-", // A
"-...", // B
"-.-.", // C
"-..", // D
".", // E
"..-.", // F
"--.", // G
"....", // H
"..", // I
".---", // J
"-.-", // K
".-..", // L
"--", // M
"-.", // N
"---", // O
".--.", // P
"--.-", // Q
".-.", // R
"...", // S
"-", // T
"..-", // U
"...-", // V
".--", // W
"-..-", // X
"-.--", // Y
"--..", // Z
};
static const int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
}
static inline void doGap(int gapSize) {
digitalWrite(ledPin, LOW);
delay(unitTimeMs * gapSize);
}
static inline void doMark(int markSize) {
digitalWrite(ledPin, HIGH);
delay(unitTimeMs * markSize);
}
static inline void doSpace() {
doGap(betweenWordsGap);
}
static inline void doChar(char c) {
c &= (0xFF-0x20); // capitalize
if (c == 0) {
doSpace();
return;
}
c -= 'A';
// make sure we ignore non-alpha chars
if (c > 25) return;
const char * code = codeTable[c];
while (*code != 0) {
if (*code == '-') { doMark(dashMark); }
else { doMark(dotMark); }
++code;
// make sure we don't set an intraChar gap at the end of the char
if (*code != 0) {
doGap(intraCharGap);
}
}
doGap(betweenLettersGap);
}
static inline void doTxt(const char *txt) {
while(*txt != 0) {
doChar (*txt);
++txt;
}
doSpace();
doSpace();
}
void loop() // run over and over again
{
if (Serial.available()) {
char c = Serial.read();
doChar(c);
Serial.print(c, BYTE);
}
}
int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
for(int foobar=0; foobar < 256; foobar++ )
{
analogWrite(ledPin, foobar);
analogWrite(3, foobar);
delay(10);
}
}