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;
}