Motor programming

Hey there. I am very new to Arduino and to programming in general, so I don't know how to solve my problem and I am struggling with it for over a month.

This is the basic set up. I have a old RC car base with two motors ( A for steering, B for forward or reverse drive). Connected to motors Arduino UNO with Arduino motor shield and HC06 bluetooth module. Everything is powered with two 9volt batteries. Now I cant get the car to run with my code, I have made the simple app in appbuilder, which pairs with arduino, and from my phone i can send commands like "forward","reverse" and so on.

Now how do I get motors to work, from coding stand point of view (because circuitry work fine)?

The problem is, when i send command from my phone, the motor starts to spin, and when i change direction it stops, and does not react to commands anymore.

Can you guys explain whats wrong with my code? Also could it be a problem with android side of code?

char val;         // variable to receive data from the serial port
String readString;

void setup(){
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //A brake

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel B pin
  

  Serial.begin(9600);      

}

void loop() {

   while( Serial.available()){
   delay(1);
    char c = Serial.read();         // read it and store it in 'c'
   readString +=c;
  }
  
  if (readString.length() >0) {
        //stop
     digitalWrite(9, HIGH);
     digitalWrite(8,HIGH);
    
    if (readString == "forward"){
       digitalWrite(13, HIGH); //Establishes forward direction of Channel B
       digitalWrite(8, LOW);   //Disengage the Brake for Channel B
       analogWrite(11, 255);   //Spins the motor on Channel B at full speed
       delay (1000); 
       
       digitalWrite(8, HIGH);   //Engage the Brake for Channel B

}
  
  if(readString == "reverse" ){
    digitalWrite(13, LOW); //Establishes backwords direction of Channel B
    digitalWrite(8, LOW);   //Disengage the Brake for Channel B
    analogWrite(11, 255);   //Spins the motor on Channel B at full speed 
    delay (1000);    
    
           digitalWrite(8, HIGH);   //Engage the Brake for Channel B
  }
  
if(readString == "right"){
   digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 255);    //Spins the motor on Channel A at half speed
       digitalWrite(13, HIGH); //Establishes forward direction of Channel B
       digitalWrite(8, LOW);   //Disengage the Brake for Channel B
       analogWrite(11, 255);   //Spins the motor on Channel B at full speed

  delay (1000);}

if(readString == "left"){
   digitalWrite(12, LOW);  //Establishes backward direction of Channel A
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 255);    //Spins the motor on Channel A at half speed
       digitalWrite(13, HIGH); //Establishes forward direction of Channel B
       digitalWrite(8, LOW);   //Disengage the Brake for Channel B
       analogWrite(11, 255);   //Spins the motor on Channel B at full speed

  delay (1000);}
  
    }
}

What sort of 9v batteries? The small PP3 batteries cannot provide enough current with the result that the Arduino will reset when the motor causes the voltage to drop.

...R

9 volt alkaline batteries. "durasel industrial". One battery connected to Arduino VCC and GND, Second one drives the motors. I suppose it is sufficient juice to power this kind of set up? Motors ran on 6 x 1,5V batteries originally.

I suppose it is sufficient juice to power this kind of set up? Motors ran on 6 x 1,5V batteries originally.

What you suppose and reality are two quite different things.

PaulS:

I suppose it is sufficient juice to power this kind of set up? Motors ran on 6 x 1,5V batteries originally.

What you suppose and reality are two quite different things.

That is helpful.. Could you tell me more about required power?

Ipritas: 9 volt alkaline batteries. "durasel industrial".

That does not tell me much. How about posting a picture - or at least giving the type code such as PP3, AA, AAA etc.

...R

Could you tell me more about required power?

If your car needs a new battery, do you go to the store and grab the first flashlight battery you see? Of course not. You get a replacement battery that is the same size, voltage, and current capacity as the one you are replacing.

The important factors are voltage and current. Size matters only in that the battery needs to fit.

The batteries for the motor are no different. You MUST supply enough current. If you don't know how much is enough, then don't just guess. Use the same size and type as what the original design used.

Sorry, for bad replies, I just understood, that battery in my native language and understanding is a bit different, than in English. Nevermind. I use two of these: http://media.professional.duracell.com/downloads//datasheets/product/Industrial/ID_9V_ID1604.pdf

Still waiting to hear suggestions about code, also any tutorial would be helpful.

Those are generally known as PP3 batteries and are quite unsuitable.

...R

The graphs in the datasheet tell the story of why the batteries are not suited to your application. At a steady current of only 250mA the battery is rated to last less than an hour and I would regard that as optimistic. I would also expect that your motors will draw considerably more than 250mA. Can you see why the batteries are not suitable ?

As to your code. Put some Serial.prints in it at salient points so that you can see what portions of code are being executed and the value of relevant variables. The value of readstring would be particularly interesting to see in the while loop at the start of the loop() function and after the while loop is exited.

There is a problem with your program

If you were to print readString to the serial and monitor it in your computer you will see the problem.

You initialise it to NULLs (not specifically, but the compiler does that for you) when you defined it. From then on, you only add to it. You never set it back to NULLs (empty string) before you do the next read.

So, the first time there is data, you put that into readString, then readString.length()>0 so you stop and then find a match and do that bit of code.

But, the second time there is data, you add that to the previous value, and readString.length()>0 so you stop, but it does not match any of the other bits of code so it does nothing but stop.

You need to reset readString to an empty string when you're done with it the first time.

You will then find that the program probably still doesn't do what you want, but it might be closer.

Well what's going to happen is that the 9V battery has a very large impedance and even if it can hold voltage while the system is operating it is not the best choice for longevity. You will be changing very often.

You are better using some lithium 8.4V Batteries or something with lower internal impedance that a 9V battery.