Nibbles and Bits
The Care and Feeding of My Pet Arduino


Teaching Arduino to Copy Morse Code

« 1 2 3  4  5 6 7 8 9 10 11 12 13 15 16 17 18 19 »

Section 4
We are going to introduce a Boolean variable to the sketch. We will use it to keep track if we have gone to a new line. Our goal is to do that only once after the key has been released. We will also add a very short delay to the part where the key is down, which will reduce the number of X's that are printed. We are declaring it at the start of the sketch so that we can adjust it here but use it later to weed out fake clicks and key bounce.

Copy the highlighted sections on the right and and paste them into your sketch in the places shown.

The line highlighted in orange should be deleted.

is now in the new If structure that you are pasting in during this step.

Compile and Run the sketch.

Notice the new line in the sketch that says:

if (!characterDone) {

characterDone is our Boolean variable. The ! means NOT which means the code in our curly brackets will only execute when characterDone is FALSE. As soon as we have printed a new line, we switch its value to TRUE so it only prints one 'line feed'. When you press your keydown again, we switch the value back to FALSE so it will go to a new line when you release the key.

Next Section »

int myKey=14;    // We are borrowing Analog Pin 0 and using it as digital
int speaker=11;  // Speaker will be hooked between pin 11 and ground

int val=0;       // A value for key up and down
int myTone=440;  // Freq. of our tone

boolean characterDone=true;
int myBounce=2;

void setup() {
  pinMode(myKey, INPUT);
  // initialize the serial communication:

 void loop() {
   if (val) keyIsDown();
   if (!val) keyIsUp();
 void keyIsDown() {
   Serial.print ('X');

 void keyIsUp() {
   if (!characterDone) {