Before getting my hands dirty, I read the first chapter of the Arduino Projects Book to give me an overview of what am I getting myself into. I was hooked! After quickly absorbing the types of components, I read about Series & Parallel circuits.
My first project was lighting up an LED by hooking up a resister, an LED to the 5v power supply. I admit I also tried the variation with a switch – depress the switch to get the LED going - from the Arduino book.
At this point, I fired up the IDE and ran the ‘Blink’ sample. I immediately understood the idea of Arduino’s digital pins. The pins represent 0/1 (On/off) state by sending / stopping electric current through them. Cool!
I realized I understood enough to start my first project! Yes, it’s going to be something I come up with on my own! It was going to be a Morse Messenger!
I will have my circuit blink out a message in Morse Code! Of course, what am I good at? Writing programs
Didn’t realize my C++ was so rusty. How the heck do you write Jagged arrays? I’m sorry, Mr. Kanetkar, I have forgotten all my pointer lessons.
Though I was using Visual Micro, I hadn’t yet figured out how to debug with break points. Subject of another blog post I guess. For now, I was able to make do with “Serial.print*” statements.
- // A dot is a '1', A dash '0'
- int letterMorse[][4] = {
- {1, 0, -1, -1},
- {0, 1, 1, 1},
- {0, 1, 0, 1},
- {0, 1, 1, -1},
- {1, -1, -1, -1},
- {1, 1, 0, 1},
- {0, 0, 1, -1},
- {1, 1, 1, 1},
- {1, 1, -1, -1},
- {1, 0, 0, 0},
- {0, 1, 0, -1},
- {1, 0, 1, 1},
- {0, 0, -1, -1},
- {0, 1, -1, -1},
- {0, 0, 0, -1},
- {1, 0, 0, 1},
- {0, 0, 1, 0},
- {1, 0, 1, -1},
- {1, 1, 1, -1},
- {0, -1, -1, -1},
- {1, 1, 0, -1},
- {1, 1, 1, 0},
- {1, 0, 0, -1},
- {0, 1, 1, 0},
- {0, 1, 0, 0},
- {0, 0, 1, 1}
- };
- int numberMorse[][5] = {
- { 0, 0, 0, 0, 0 },
- { 1, 0, 0, 0, 0 },
- { 1, 1, 0, 0, 0 },
- { 1, 1, 1, 0, 0 },
- { 1, 1, 1, 1, 0 },
- { 1, 1, 1, 1, 1 },
- { 0, 1, 1, 1, 1 },
- { 0, 0, 1, 1, 1 },
- { 0, 0, 0, 1, 1 },
- { 0, 0, 0, 0, 1 }
- };
- int led = 10;
- const int standardPauseInMilliseconds = 500;
- void setup() {
- // put your setup code here, to run once:
- doMorse("I LOVE ARDUINO");
- }
- void loop() {
- }
- void doMorse(String message)
- {
- // put your main code here, to run repeatedly:
- for (int i = 0; i < message.length(); i++)
- {
- char letter = message.charAt(i);
- Serial.println(letter);
- if (letter == ' ')
- {
- //Words are separated by a space equal to 7 dots
- delay(standardPauseInMilliseconds * 7);
- continue;
- }
- int value = ((int)letter) - 65;
- bool isNumber = value < 0;
- Serial.println(value);
- int *component = isNumber ? numberMorse[value] : letterMorse[value];
- for (int j = 0; j < 4; j++)
- {
- int number = component[j];
- if (number < 0) continue;
- bool isDot = (number);
- Serial.print(isDot ? "Dot- " : "Dash- ");
- //For the duration of a dash (0) is 3 times the duration of a dot (1)
- blink(isDot ? standardPauseInMilliseconds : standardPauseInMilliseconds * 3);
- beep(isDot ? standardPauseInMilliseconds : standardPauseInMilliseconds * 3);
- //Each dot / dash is followed by a short silence, equal to the dot duration
- delay(standardPauseInMilliseconds);
- }
- Serial.println(' ');
- //The space between letters is three units
- delay(standardPauseInMilliseconds * 3);
- }
- }
- void blink(int delayInMilliseconds)
- {
- pinMode(led, OUTPUT);
- //Turn on the light
- digitalWrite(led, HIGH);
- //For the duration of a dash (0) is 3 times the duration of a dot (1)
- delay(delayInMilliseconds);
- digitalWrite(led, LOW);
- }
- void beep(int delayInMilliseconds)
- {
- pinMode(led, OUTPUT);
- analogWrite(led, 20);
- delay(delayInMilliseconds);
- analogWrite(led, 0);
- }
The first version of the circuit did not have the Piezo. However, I realized I was not able to read the message (despite the fact I was trying to decipher Morse Code for the first time in my life ), and I needed an audible cue to distinguish dashes from dots. Lo & behold: The parallel circuit with both LED & Piezo.
It’s interesting to note that Arduino allows the use of the same pin (10) for controlling both Piezo (analog) & LED (digital).
No comments:
Post a Comment