Bluetooth/L293D Sketch help

Hello there, I am currently working on a new sketch (Old Sketch laptop crashed lost all my data... had to re-write)

I am stuck with errors through my sketch that I am not 100% sure how to fix. I am a beginner when it comes to this so any help would be much appreciated.

Right now I am using a bluetooth APP that is very simple

Forward = 1 Right = 2 Left = 3

Reverse = 4

And here is my code

char data = 0; //Variable for storing received data void setup() { Serial.begin(9600); //Sets the baud for serial data transmission //Left Motor const int motorPin1 = 5; // Pin 14 of L293 const int motorPin2 = 6; // Pin 10 of L293 //Right Motor const int motorPin3 = 10; // Pin 7 of L293 const int motorPin4 = 9; // Pin 2 of L293

} void loop() { if(Serial.available() > 0) // Send data only when you receive data: { data = Serial.read(); //Read the incoming data & store into data Serial.print(data); //Print Value inside data in Serial monitor Serial.print("\n");

// ------------------------------------------------Button 1 Forward ----------------------- if(data == '1') // Checks whether value of data is equal to 1 digitalWrite(motorPin1, HIGH); // Left Motor Forward digitalWrite(motorPin2, LOW); // Left Motor Forward digitalWrite(motorPin3, HIGH); // Right Motor Forward digitalWrite(motorPin4, LOW); // Right Motor Forward delay(1); digitalWrite(motorPin1, LOW); // Left Motor OFF digitalWrite(motorPin3, LOW); // Right Motor OFF // ------------------------------------------------Button 2 Right----------------------- if(data == '2') // Checks whether value of data is equal to 2 digitalWrite(motorPin1, HIGH); //Left Motor Forward digitalWrite(motorPin2, LOW); //Left Motor Forward digitalWrite(motorPin3, LOW); //Right Motor Reverse digitalWrite(motorPin4, HIGH; //Right Motor Reverse delay(1); digitalWrite(motorPin2, LOW); // Left Motor OFF digitalWrite(motorPin4, LOW); // Right Motor OFF // ------------------------------------------------Button 3 Left----------------------- if(data == '3') // Checks whether value of data is equal to 3 digitalWrite(motorPin1, LOW); //Left Motor Reverse digitalWrite(motorPin2, HIGH); //Left Motor Reverse digitalWrite(motorPin3, HIGH); //Right Motor Forward digitalWrite(motorPin4, LOW; //Right Motor Forward delay(1); digitalWrite(motorPin2, LOW); // Left Motor OFF digitalWrite(motorPin4, LOW); // Right Motor OFF // ------------------------------------------------Button 4 Reverse----------------------- if(data == '4') // Checks whether value of data is equal to 4 digitalWrite(motorPin1, LOW); //Left Motor Reverse digitalWrite(motorPin2, HIGH); //Left Motor Reverse digitalWrite(motorPin3, LOW); //Right Motor Reverse digitalWrite(motorPin4, HIGH; //Right Motor Reverse delay(1); digitalWrite(motorPin2, LOW); // Left Motor OFF digitalWrite(motorPin4, LOW); // Right Motor OFF

} }

What errors?

Did you really intend to run the motors for only 1/1000 of a second?

You need braces {} around the code connected with the "if" statements.

 if(data == '1')              // Checks whether value of data is equal to 1
digitalWrite(motorPin1, HIGH);   // Left Motor Forward
digitalWrite(motorPin2, LOW);    // Left Motor Forward
digitalWrite(motorPin3, HIGH);  // Right Motor Forward
digitalWrite(motorPin4, LOW);   // Right Motor Forward
  delay(1);             
  digitalWrite(motorPin1, LOW);    // Left Motor OFF
   digitalWrite(motorPin3, LOW);  // Right Motor OFF

Right now that first digitalWrite is all that depends on the if statement. The rest will run no matter what data equals. If you want to group those statements so that they all depend on the if then you need some braces.

 if(data == '1')   // Checks whether value of data is equal to 1
{           
    digitalWrite(motorPin1, HIGH);   // Left Motor Forward
    digitalWrite(motorPin2, LOW);    // Left Motor Forward
    digitalWrite(motorPin3, HIGH);  // Right Motor Forward
    digitalWrite(motorPin4, LOW);   // Right Motor Forward
    delay(1);             
    digitalWrite(motorPin1, LOW);    // Left Motor OFF
    digitalWrite(motorPin3, LOW);  // Right Motor OFF
}

Same issue with the other if statements.

You also have an issue with scope. Since these:

const int motorPin1  = 5;  // Pin 14 of L293
const int motorPin2  = 6;  // Pin 10 of L293
//Right Motor
const int motorPin3  = 10; // Pin  7 of L293
const int motorPin4  = 9;  // Pin  2 of L293

are defined inside the setup function, they are only available there. Any use of them in loop will get you the “not declared in this scope” error. Move these lines up above the setup function so that the are global variables and are available to all functions.

Also, please take a minute to read the forum rules. Pay particular attention to point 7 about how to properly format and post your code.

Thank you all so much so far my sketch now passes verify!

the small victories…

is the delay(1) all I need to ensure motors turn off when button is released? or is there something else I could use?

delay(1) has nothing to do with turning the motors off. It just tells the micro to wait for a millisecond before it goes on to the next line. If it were me, I'd have an option '5' for telling it to stop and let the sending code send that whenever the button isn't pressed anymore.

would that allow me to remove the whole

  delay(1);                      
  digitalWrite(motorPin1, LOW);    // Left Motor OFF
   digitalWrite(motorPin4, LOW);  // Right Motor OFF

from each function?

sorry I am pretty new at all of this :(

Yes it would.

Delta_G: delay(1) has nothing to do with turning the motors off.

In the program, as it stands, it has everything to do with it. The length of the delay controls how long the motors are on.

Delta_G: delay(1) has nothing to do with turning the motors off. It just tells the micro to wait for a millisecond before it goes on to the next line. If it were me, I'd have an option '5' for telling it to stop and let the sending code send that whenever the button isn't pressed anymore.

what would the code be? I tried several attempts then gave up and went with the delay(1) which worked for first button then once I had all 4 it stopped working.

reallyfastlearner: what would the code be? I tried several attempts then gave up and went with the delay(1) which worked for first button then once I had all 4 it stopped working.

The code be for what? You have to define how you want it to behave before you can code it, or discuss it productively.

I am trying to write it so that when the button is released the motor stops as well that is why I put the delay. it seems that the app I am using sends a "1" but I am not sure how to program my sketch to realize it needs to stop once button is released.

reallyfastlearner: I am trying to write it so that when the button is released the motor stops as well that is why I put the delay. it seems that the app I am using sends a "1" but I am not sure how to program my sketch to realize it needs to stop once button is released.

The serial monitor only sends a key value once when it is pressed. The arduino never receives any indication of when you release the key.

How does your Bluetooth app behave in that regard? It would need to send some kind of stop code.

The Arduino has no way to know what you're pressing on the app unless the app sends something to the Arduino to tell it. We don't know anything about the app, but I'm guessing that it sends the 1 constantly while you hold the button down. If that's the case then you could code the app to send a different number when no buttons are pressed, or you could have the Arduino code recognize that it hasn't got a signal in a certain amount of time and that would signal it to stop. Without knowing more about the app you're sending from it's hard to tell which would be easier. It would certainly be fairly simple to track millis every time you get a signal from the app and when the current time is more than a few milliseconds from the last time it received a signal to stop the motors. That way you don't have to constantly start and stop the motors over and over like you do now.

As soon as you combine the use of millis() and serial input, you will have to learn the technique of "doing more than one thing at a time" with non-blocking code.

So really the only way it sounds like this would work if the app itself sent a "Off" or "0" command once the button is released.

Any suggestions on a work around for this other than

      if(data == '0')        
      {
digitalWrite(motorPin1, LOW);  
digitalWrite(motorPin2, LOW); 
digitalWrite(motorPin3, LOW);  
digitalWrite(motorPin4, LOW);

reallyfastlearner: So really the only way it sounds like this would work if the app itself sent a "Off" or "0" command once the button is released.

No, you can use a time out as suggested in reply #15. But sending a stop code would also work.

What is your objection to the code example that you just showed?

Here is a picture of the app

https://cdn.instructables.com/F28/TCSK/INT6ELHQ/F28TCSKINT6ELHQ.MEDIUM.jpg?width=614

Going to try a timeout command (never done one before other than a Delay)