Hello everyone!
I am writing a simple game that randomly selects the word "SOS" or "HELP" and blinks an LED in Morse Code.
If the word is "SOS" you have to press the Red button, and if it is "HELP" you press the Blue one.
I am using MorseCodeMachine library, but i have tried others too and same thing happens:
While the LED is blinking, the buttons are not working.
#include <MorseCodeMachine.h>
int morse_LED = 4;
int redButton = 51;
int blueButton = 53;
int error = 0;
int game = 0;
int morseWord;
void setup()
{
randomSeed( analogRead(A0) );
pinMode(morse_LED, OUTPUT);
Serial.begin(9600);
}
void loop(){
int morseWord = random(2); //generate a random number for Morse Word
while (game==0 && error==0){
redButton = digitalRead(51);
blueButton = digitalRead(53);
switch(morseWord) {
case 0: sendMorse("sos", ledDelay, ledDot, ledDash);break;
case 1: sendMorse("help", ledDelay, ledDot, ledDash);break;}
if (redButton==0) { // If red button is pressed
if (morseWord==0) {game=1;}
else {error=1;}}
else if (blueButton==0) { // If blue button is pressed
if (morseWord==1) {game=1;}
else {error=1;}}
}
if (game==1) {Serial.println("WIN");}
else if (error==1) {Serial.println("LOST");}
}
//Morse Code parameters
void ledDelay()
{
//Wait for a small amount of time.
delay(200);
}
//Create a dot function and give it to the sendMorse function so that the
//sendMorse function can create dots.
//You can give this function any name you want.
void ledDot()
{
//Turn on the LED for a small amount of time, then turn off.
digitalWrite(morse_LED, HIGH);
ledDelay();
digitalWrite(morse_LED, LOW);
}
//Create a dash function and give it to the sendMorse function so that the
//sendMorse function can create dashes.
//You can give this function any name you want.
void ledDash()
{
//Turn on the LED three times as long as the dot, then turn off.
digitalWrite(morse_LED, HIGH);
ledDelay();
ledDelay();
ledDelay();
digitalWrite(morse_LED, LOW);
}