Code not properly executing

Hi, im trying to create a voice activated swing. My code is reading the bytes correctly but my if statements are not being read properly. My code is below and i will print my serial port as well.
The incoming byte is bieng read but isn't bieng used properly in the statement.
I am using a arduino mega and i will upload a picture of my


wiring as well
Serial port:
Turn off SuccessfulReceived data = 40
Turn off SuccessfulReceived data = 50
Turn off SuccessfulReceived data = 00
Turn off SuccessfulReceived data = 00
Turn off SuccessfulReceived data = 10
Turn off SuccessfulReceived data = 20
Turn off SuccessfulReceived data = 30
Turn off SuccessfulReceived data = 00
Turn off SuccessfulReceived data = 10
Turn off SuccessfulReceived data = 30

#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; 
int MotorToggle = 30;

//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)
 {
  Serial.print("Test Successful");
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    incomingByte = 0;
 }
  if(incomingByte == 10)
 {
  Serial.print("Turn off Successful");
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    incomingByte = 0;
    
 }

You are already using Serial.print() for many things. Use it to print the values being used by your "if" and then use it to print the steps taken as a result of the "if".
Paul

can you please write it for me?

Write what? You are already writing lots of Serial.print(), so write some to display the values being compared. Not very difficult.
Paul

Here?

You have to provide more information:

the receiving part of your code

  if (Serial2.available() > 1) {

means there have to be more than 1 byte in the receive-buffer
you are checking for > 1 bigger than one
then you read in two bytes

    incomingByte = Serial2.read();
    incomingByte1 = Serial2.read();

and print two bytes

    Serial.print ("Received data = ");
    Serial.print(int(incomingByte) , DEC);
    Serial.println(int(incomingByte1) , DEC);

what you have posted shows only one byte

Turn off SuccessfulReceived data = 30
are you sure you have uploaded that code-version to your microcontroller that you posted the serial output from?

to see in more detail what is going on in your code you should extent on the serial output to clearly show what serial output is what

This if-condition would show up in the serial monitor as many many lines all printing

    if (incomingByte == 0)

Test Successful
Test Successful
Test Successful
Test Successful
Test Successful

if there is a byte with value 10 there would be one line inbetween
Turn off Successful
and as you assign

incomingByte = 0 at the end of this block of code

if (incomingByte == 10)
{
  Serial.print("Turn off Successful");
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  **incomingByte = 0;**
}

at the end there would be a lot of lines
Turn off Successful
Turn off Successful
Turn off Successful
...
right afterwards
I doubt that the serial-output you posted is from that version of code that you have posted

So her is a code-version with additional serial output that makes it easy to see which output is what through titels and through a leading and a trailing character "#" to indicate start and end of values

and to show you the content of variable incomingByte and how the value of incomingByte might change while your code executes towards the lower end of loop

#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;
int MotorToggle = 30;

//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 =incomingByte #");
    Serial.print(int(incomingByte) , DEC);
    Serial.print("#");
    Serial.print("  incomingByte1=#");
    Serial.print(int(incomingByte1) , DEC);
    Serial.print("#");
    Serial.println();


    Serial.print("right before if (incomingByte == 0)");
    Serial.print("incomingByte #");
    Serial.print(incomingByte);
    Serial.println("#");
    if (incomingByte == 0)
    {
      Serial.print("Test Successful");
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, HIGH);
      digitalWrite(motorPin3, LOW);
      digitalWrite(motorPin4, LOW);
      incomingByte = 0;
    }

    Serial.print("right before if (incomingByte == 10)");
    Serial.print("incomingByte #");
    Serial.print(incomingByte);
    Serial.println("#");
    if (incomingByte == 10)
    {
      Serial.print("Turn off Successful");
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, LOW);
      digitalWrite(motorPin3, LOW);
      digitalWrite(motorPin4, LOW);
      incomingByte = 0;
    }
  }
}

best regards Stefan

This is what i received now when I uploaded that code. This makes me more confused than i was Before. Please explain what this outcome means.

The rest of my code is in comments so i don't think it processes.
Therefore i didn't post it.
But the reason why the 30, 40, and 50 come up is because i recorded numbers 1 to 10 in my SpeakUp so they exist.

You should post serial-output as code-section
click into the serial-output-window mark the characters you want to copy
press Ctrl-C to copy the marked into the clipboard

change to forum click on the </>-Button
press Ctrl-V to insert clipboard

as a general hint:
except you quote one or two lines of code post the entire sketch.
From the very first line to the very last using this method
Just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

There are so many many cases that the cause of the bug is in another part of the code
and for everybody who joins in later it is much easier to follow if you always post your full sketch as a code-section. The codesections height is limited there is still overview.
And second reason if you post the full sketch as a code-section it can be copied by a single mouse-click

What the serial printing means
fixed text received data=incomingByte #
content of the variable just named "incomingByte" left of the "="sign: 0

fixed text: # incomingByte1=#
content of the variable just named incomingByte1 left of the "="sign: 0

So the first line indicates variable incomingByte and incomingByte1 contain both zero

Line starting with received data = ...

shows
value of variable incomingByte is 1
value of variable incomingByte1 is 0

There is a BIG difference if you write Serial.print("double-hyphen create fixed text")
and a serial.print(myVariable) without double-hyhens which means print the content of the variable

printing the decimal value in the last two lines shows a small square
In this case it will be better to print the hex-value

best regards Stefan

Here is my full code

#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; 
int MotorToggle = 30;

//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)
 {
  Serial.print("Test Successful");
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
 }
  if(incomingByte == 1)
 {
  Serial.print("Turn off Successful");
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
    
 }
//   if(incomingByte == 2)
// {
//  Serial.print("Swing on Successful");
//    digitalWrite(motorPin1, LOW);
//    digitalWrite(motorPin2, HIGH);
//    digitalWrite(motorPin3, LOW);
//    digitalWrite(motorPin4, LOW);
//    incomingByte = 0;
//    Serial.println("I received: ");
//    Serial.println(incomingByte, DEC);

//    int inByte = Serial.read();
//    Serial1.print(inByte, BYTE); 
//
//  }
//  // read from port 1, send to port 0:
//  //if (Serial1.available()) {
//    //int inByte = Serial1.read();
//    Serial.print(inByte, BYTE); 
  }
}
 //// 
// if (SwingToggle) { // Turn the swing ON 
// Serial.println ("Command: Nine"); 
// digitalWrite(Swing, HIGH);
// 
// } 
// //// 
// if (incomingByte == 1) { // Turn the swing off 
// Serial.println ("Command: Ten"); 
// digitalWrite(Swing, LOW); 
// 
// } 
// //// 
// if (incomingByte == 2) { // Turn the music on 
// Serial.println ("Command: Three"); 
// digitalWrite(Speaker, HIGH); 
// 
// } 
// //// 
// if (incomingByte == 3) { // Turn the music off 
// Serial.println ("Command: Four"); 
// digitalWrite(Speaker, LOW); 
// incomingByte=0; 
// } 
// //// 
// if (incomingByte == 4) { // Make swing faster 
// Serial.println ("Command: Five"); 
// digitalWrite(RedLED, 255); 
// } 
// //// 
// if (incomingByte == 5) { // Make the swing slower 
// Serial.println ("Command: Six"); 
// digitalWrite(RedLED, 0); 
// } 
// //// 
// if (MotorToggle = 1) { // Turn swinging motion on 
// Serial.println ("Command: One"); 
// digitalWrite(motorPin1, LOW); 
// digitalWrite(motorPin2, HIGH); 
// 
// } 
// //// 
// if (MotorToggle = 0) { // Turn swinging motion off 
// Serial.println ("Command: Two"); 
// digitalWrite(motorPin1, LOW); 
// digitalWrite(motorPin2, LOW); 
// } 
// //// 
// if (incomingByte == 8) { // Turn the mobile toys on 
// Serial.println ("Command: Seven"); 
// digitalWrite(motorPin3, HIGH); 
// digitalWrite(motorPin4, LOW); 
// } 
// //// 
// if (incomingByte == 9) { //Turn mobile toys off 
// Serial.println ("Command: Eight"); 
// digitalWrite(motorPin3, LOW); 
// digitalWrite(motorPin4, LOW); 
// }

When I switched it to HEX, it still shows the small sqaures

I don't have a second serial send function handy so I changed receiving from serial2 to serial
change it back for your code this version shows how to output HEX-values

#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;
int MotorToggle = 30;

//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 (Serial.available() > 1) {
    // read the incoming byte:
    incomingByte = Serial.read(); // change back to your serial port
    incomingByte1 = Serial.read();// change back to your serial port
    Serial.print ("Received data =incomingByte #");
    Serial.print(incomingByte , HEX);
    Serial.print("#");
    Serial.print("  incomingByte1=#");
    Serial.print(incomingByte1 , HEX);
    Serial.print("#");
    Serial.println();

    Serial.print("right before if (incomingByte == 0)");
    Serial.print("incomingByte #");
    Serial.print(incomingByte,HEX);
    Serial.println("#");
    if (incomingByte == 0)
    {
      Serial.print("Test Successful");
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, HIGH);
      digitalWrite(motorPin3, LOW);
      digitalWrite(motorPin4, LOW);
      incomingByte = 0;
    }
    //   if(incomingByte == 2)
    // {
    //  Serial.print("Swing on Successful");
    //    digitalWrite(motorPin1, LOW);
    //    digitalWrite(motorPin2, HIGH);
    //    digitalWrite(motorPin3, LOW);
    //    digitalWrite(motorPin4, LOW);
    //    incomingByte = 0;
    //    Serial.println("I received: ");
    //    Serial.println(incomingByte, DEC);

    //    int inByte = Serial.read();
    //    Serial1.print(inByte, BYTE);
    //
    //  }
    //  // read from port 1, send to port 0:
    //  //if (Serial1.available()) {
    //    //int inByte = Serial1.read();
    //    Serial.print(inByte, BYTE);
  }
}
////
// if (SwingToggle) { // Turn the swing ON
// Serial.println ("Command: Nine");
// digitalWrite(Swing, HIGH);
//
// }
// ////
// if (incomingByte == 1) { // Turn the swing off
// Serial.println ("Command: Ten");
// digitalWrite(Swing, LOW);
//
// }
// ////
// if (incomingByte == 2) { // Turn the music on
// Serial.println ("Command: Three");
// digitalWrite(Speaker, HIGH);
//
// }
// ////
// if (incomingByte == 3) { // Turn the music off
// Serial.println ("Command: Four");
// digitalWrite(Speaker, LOW);
// incomingByte=0;
// }
// ////
// if (incomingByte == 4) { // Make swing faster
// Serial.println ("Command: Five");
// digitalWrite(RedLED, 255);
// }
// ////
// if (incomingByte == 5) { // Make the swing slower
// Serial.println ("Command: Six");
// digitalWrite(RedLED, 0);
// }
// ////
// if (MotorToggle = 1) { // Turn swinging motion on
// Serial.println ("Command: One");
// digitalWrite(motorPin1, LOW);
// digitalWrite(motorPin2, HIGH);
//
// }
// ////
// if (MotorToggle = 0) { // Turn swinging motion off
// Serial.println ("Command: Two");
// digitalWrite(motorPin1, LOW);
// digitalWrite(motorPin2, LOW);
// }
// ////
// if (incomingByte == 8) { // Turn the mobile toys on
// Serial.println ("Command: Seven");
// digitalWrite(motorPin3, HIGH);
// digitalWrite(motorPin4, LOW);
// }
// ////
// if (incomingByte == 9) { //Turn mobile toys off
// Serial.println ("Command: Eight");
// digitalWrite(motorPin3, LOW);
// digitalWrite(motorPin4, LOW);
// }

best regards Stefan