Hi, I'm trying to control a 9v-12v, DC motor. I got the motor to run. But when I transferred that same code into my voice commanding code it wouldn't spin.
First i will post the original code:
//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
//This will run only one time.
void setup(){
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4
//This code will turn Motor A clockwise for 2 sec.
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10000);
My voice activated 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);
}
Here is a schematic of my wiring:
