Using Speak up click to control motors

My code is written below. It compiles and uploads but doesn't process properly. If i say a command nothing happens. And serial port just keeps saying "command: Turn On" over and over?
The motors won't turn on
Please help!!!

#include <HardwareSerial.h>
#include <Arduino.h>
#include <Wire.h>
extern HardwareSerial Serial1;
extern HardwareSerial Serial2;
char incomingByte;
char incomingByte1;

//Speaker
int Speaker = 15;
int Swing = 16;
int RedLED = 12;
//L293D
//Swing Motor (Big motor)
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Toy Motor (Small Motor)
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293

int EN1 = 3; // Pin 1 of L293D IC, D6 Pin of NodeMCU (controls the speed)

void setup(){

//Set motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);

pinMode(EN1, OUTPUT); //Where the motor is connected to

Serial.begin(115200);
Serial2.begin(115200);
Serial.println("Ready for commands");
}

void loop() {
/////

if (Serial2.available() > 1) {
// read the incoming byte:
incomingByte = Serial2.read();
incomingByte1 = Serial2.read();
Serial.print ("Received data = ");
Serial.print(int(incomingByte) ,DEC);
Serial.println(int(incomingByte1) ,DEC);

}

////
if (incomingByte == 0){ // Turn the swing ON
Serial.println ("Command: Turn On");
digitalWrite(Swing,HIGH);
}

////
if (incomingByte == 1){ // Turn the swing off
Serial.println ("Command: Turn Off");
digitalWrite(Swing,LOW);
}
////
if (incomingByte == 2){ // Turn the music on
Serial.println ("Command: Music ON");
digitalWrite(Speaker,HIGH);
}

////
if (incomingByte == 3){ // Turn the music off
Serial.println ("Command: Music off");
digitalWrite(Speaker,LOW);
}

////
if (incomingByte == 4){ // Make swing faster
Serial.println ("Command: Swing Fast");
digitalWrite(RedLED,255);
}

////
if (incomingByte == 5){ // Make the swing slower
Serial.println ("Command: Swing SLOW");
digitalWrite(RedLED,0);

            }                

////
if (incomingByte == 6){ // Turn swinging motion on
Serial.println ("Command: Swing On");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(1000);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);

            }               

////
if (incomingByte == 7){ // Turn swinging motion off
Serial.println ("Command: Swing OFF");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}

////
if (incomingByte == 8){ // Turn the mobile toys on
Serial.println ("Command: Toys ON");
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}

////
if (incomingByte == 9){ //Turn mobile toys off
Serial.println ("Command: Toys OFF");
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}

}

Please help helpers reading code, not text, and use the IDE autoformat and code tags when pasting.

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Additional information would be welcome.
What Arduino board?
Post data sheets of components. What is receiving the speech?
Post a schematic showing all components and power supplies showing how the project should be wired.
Post photos of the project so we can see how it is wired.

1 Like

You have a load of debug prints - what do they tell you (and us)?

1 Like

I am using the Elegoo mega2560 I am attaching a drawing of my wiring

I get the impression you are expecting that long chain of if (incomingByte == statements to come into play only if you have received an incoming byte.

But as you have written the code, they are executed every time through loop().

So, unless something has been received and stored in incomingByte, then incomingByte will contain zero, and that is why every time round loop() it finds that incomingByte is indeed zero, thus prints out "Command: Turn On".

right
so how would i change the incomingByte?

You need to post your code properly so that it is easy to follow. But in any case, this isn't somewhere you can bring your code and get it debugged for free. You need to read and understand the comments, then try to work out for yourself how to fix the problem.

Meanwhile, just to get you started, see the closing brace I've marked:

That terminates the if (Serial2.available() > 1) statement, which you don't want to do because then the if (incomingByte == 0) statement, and all the statements that follow, will execute, regardless of whether there was an incoming byte.

So move that closing brace down to just before the brace that closes the loop() function.
That should stop the continuous printing of "Command: Turn On".

Are you actually receiving any bytes? Are you seeing anything from those Serial.print(int(incomingByte) ,DEC) statements?

And don't forget: we need to see the properly formatted code.

I tried to upload the correct code but couldnt figure it out

This worked just the speak up click doesn't process the right commands. Like if i say Toys OFF it says Music OFF

You've been told how already. See Answer 3.

OK, so think about it. How will you approach finding out what the problem is? Exactly how are your commands encoded into two bytes?

When you say 'Toys OFF', what exactly does incomingByte contain? In fact, do this: send each command in turn, and write down what incomingByte and incomingByte1 contain each time. By observing what incomingByte contains for each command, you will then see what response the code will give for each command.

Think it through, step by step. Write the result of every step down as you work through the code. This will shine a light on why it isn't behaving as you want it to.

IncomingByte1 never shows up

OK, good - you've made a start. Keep going. Find out what is in incomingByte for each command.

I figured it out, the incominbyte is the number assigned to it when a certian command is called in DEC. but i believe my issue is with the actual speak up
I dont think it can differentiate between each of the commands.

It also won't process the commands