I can't thank you enough for your help! I feel bad asking for more but now I'm trying to add the morse decoder part of the project into the code and I'm not sure what I'm doing wrong. I tried Googling some of the errors but I didn't understand too well.
Here's the morse decoder I'm trying to integrate:
/*
PROGRAM TO DECIPHER MORSE CODE USING A PUSH BUTTON AND DISPLAY IT ON THE SERIAL MONITOR
DATE: 20 JANUARY 2017
AUTHORS: PINAKI SADHUKHAN AND PRIYANKA SADHUKHAN
*/
unsigned long signal_len,t1,t2; //time for which button is pressed
int inputPin = 2; //input pin for push button
int ledPin = 4; //outpu pin for LED
String code = ""; //string in which one alphabet is stored
void setup() {
Serial.begin(9600);
pinMode(inputPin, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit
pinMode(ledPin,OUTPUT);
}
void loop()
{
NextDotDash:
while (digitalRead(inputPin) == HIGH) {}
t1 = millis(); //time at button press
digitalWrite(ledPin, HIGH); //LED on while button pressed
while (digitalRead(inputPin) == LOW) {}
t2 = millis(); //time at button release
digitalWrite(ledPin, LOW); //LED off on button release
signal_len = t2 - t1; //time for which button is pressed
if (signal_len > 50) //to account for switch debouncing
{
code += readio(); //function to read dot or dash
}
while ((millis() - t2) < 500) //if time between button press greater than 0.5sec, skip loop and go to next alphabet
{
if (digitalRead(inputPin) == LOW)
{
goto NextDotDash;
}
}
convertor(); //function to decipher code into alphabet
}
char readio()
{
if (signal_len < 600 && signal_len > 50)
{
return '.'; //if button press less than 0.6sec, it is a dot
}
else if (signal_len > 600)
{
return '-'; //if button press more than 0.6sec, it is a dash
}
}
void convertor()
{
static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
};
int i = 0;
if (code == ".-.-.-")
{
Serial.print("."); //for break
}
else
{
while (letters[i] != "E") //loop for comparing input code with letters array
{
if (letters[i] == code)
{
Serial.print(char('A' + i));
break;
}
i++;
}
if (letters[i] == "E")
{
Serial.println("<Wrong input>"); //if input code doesn't match any letter, error
}
}
code = ""; //reset code to blank string
}
And here's the working chat bot code:
// Example 2 - Receive with an end-marker
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup()
{
Serial.begin(9600);
Serial.println("Waking up...");
delay(1000);
Serial.println("Snoozing the alarm clock...");
delay(1000);
Serial.println("Getting coffee...");
delay(1000);
Serial.println("Petting the dog...");
delay(2000);
Serial.println("Done!");
delay(1000);
Serial.println("Ava: Hi there! I'm Ava; the morse code training bot! I'm designed to help develop your skills in communicating with morse code through conversation. For a list of words or phrases I know, check the README.txt file");
}
void loop()
{
recvWithEndMarker();
if (newData == true)
{
if (strcmp(receivedChars, "Hello") == 0)
{
Serial.print("Me:");
Serial.println(receivedChars);
Serial.println("Ava: Hi there!");
}
else if (strcmp(receivedChars, "Hi") == 0) // ****** added else if to make a if-else if-else chain
{
Serial.print("Me:");
Serial.println(receivedChars);
Serial.println("Ava: Hello, how are you?");
}
else
{
Serial.println("Ava: Sorry, I don't know that one. Check the README.txt file for a list of words or phrases I know.");
}
newData = false;
}
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
// ignore carriage return
if (rc == '\r')
{
return;
}
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
Finally, here's my unsuccessful attempt to integrate it:
// Example 2 - Receive with an end-marker
//MORSE CODE SECTION
unsigned long signal_len,t1,t2; //time for which button is pressed
int inputPin = 2; //input pin for push button
int ledPin = 4; //outpu pin for LED
String code = ""; //string in which one alphabet is stored
// END OF MORSE CODE SECTION
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup()
{
Serial.begin(9600);
//MORSE CODE SECTION
pinMode(inputPin, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit
pinMode(ledPin,OUTPUT);
//END OF MORSE CODE SECTION
Serial.println("Waking up...");
delay(1000);
Serial.println("Snoozing the alarm clock...");
delay(1000);
Serial.println("Getting coffee...");
delay(1000);
Serial.println("Petting the dog...");
delay(2000);
Serial.println("Done!");
delay(1000);
Serial.println("Ava: Hi there! I'm Ava; the morse code training bot! I'm designed to help develop your skills in communicating with morse code through conversation. For a list of words or phrases I know, check the README.txt file");
}
void loop()
//MORSE CODE SECTION
{
NextDotDash:
while (digitalRead(inputPin) == HIGH) {}
t1 = millis(); //time at button press
digitalWrite(ledPin, HIGH); //LED on while button pressed
while (digitalRead(inputPin) == LOW) {}
t2 = millis(); //time at button release
digitalWrite(ledPin, LOW); //LED off on button release
signal_len = t2 - t1; //time for which button is pressed
if (signal_len > 50) //to account for switch debouncing
{
code += readio(); //function to read dot or dash
}
while ((millis() - t2) < 500) //if time between button press greater than 0.5sec, skip loop and go to next alphabet
{
if (digitalRead(inputPin) == LOW)
{
goto NextDotDash;
}
}
{ convertor(); //function to decipher code into alphabet
}
{
if (signal_len < 600 && signal_len > 50)
{
return '.'; //if button press less than 0.6sec, it is a dot
}
else if (signal_len > 600)
{
return '-'; //if button press more than 0.6sec, it is a dash
}
}
char readio()
{
if (signal_len < 600 && signal_len > 50)
{
return '.'; //if button press less than 0.6sec, it is a dot
}
else if (signal_len > 600)
{
return '-'; //if button press more than 0.6sec, it is a dash
}
}
//END OF MORSE CODE SECTION
recvWithEndMarker();
if (newData == true)
{
if (strcmp(receivedChars, "Hello") == 0)
{
Serial.print("Me:");
Serial.println(receivedChars);
Serial.println("Ava: Hi there!");
}
else if (strcmp(receivedChars, "Hi") == 0)
{
Serial.print("Me:");
Serial.println(receivedChars);
Serial.println("Ava: Hello, how are you?");
}
else
{
Serial.println("Ava: Sorry, I don't know that one. Check the README.txt file for a list of words or phrases I know.");
}
newData = false;
}
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
// ignore carriage return
if (rc == '\r')
{
return;
}
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
//MORSE COURSE SECTION
void convertor()
{
static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
};
int i = 0;
if (code == ".-.-.-")
{
Serial.print("."); //for break
}
else
{
while (letters[i] != "E") //loop for comparing input code with letters array
{
if (letters[i] == code)
{
Serial.print(char('A' + i));
break;
}
i++;
}
if (letters[i] == "E")
{
Serial.println("<Wrong input>"); //if input code doesn't match any letter, error
}
}
code = ""; //reset code to blank string
}
// END OF MORSE CODE SECTION
Thanks again for any help you're willing to provide. I think I'm slowly understanding more how it works.