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