0

there is something wrong with this code logic and i can't put my hand on it so if any one can fix i would really appreciate it Basically the code is used to receive couple of char from Bluetooth or serial monitor combine them together in an array then print this array then search for ":" in this array and atoi them into two different integer IE (50:90) will be (50) and (90) in two different integer that are then used to control the direction and the speed of a motor

//motor a 
int enA = 9;
int in1 = 8;
int in2 = 7;

//motor b
int in3 = 5;
int in4 = 4;
int enB = 3;
char data[32]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(enA , OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB, OUTPUT);
  Serial.println("\n String conversion testing");
}


void loop() {

if(index < 31) // One less than the size of the array
      {for( int i = 0; i < sizeof(data);  ++i )
          data[i] = (char)0;

          inChar = Serial.read(); // Read a character

          data[index] = inChar; // Store it
          index++; // Increment where to write next
          for( int i = 0; i < sizeof(data);  ++i )        
          Serial.println (data);

      }
    char* command = strchr (data, ":");  
    if (command !=0)
    {
      *command = 0;
      int direc = atoi (data);
      ++command;
      int speeds = atoi (command);
      Serial.println(speeds);
      Serial.println(direc);
     int s;
     s = map(speeds, 0, 100, 0, 255);     
    if (direc >= 85 &&direc <=100 ){//odam
        digitalWrite(in1,HIGH);
        digitalWrite(in2,LOW);
        analogWrite(enA,s);
        digitalWrite(in3,HIGH);
        digitalWrite(in4,LOW);
        analogWrite(enB,s);
    }

      else if (direc >=260 && direc <=280){  //reverse
        digitalWrite(in1,LOW);
        digitalWrite(in2,HIGH);
        analogWrite(enA,s);
        digitalWrite(in3,LOW);
        digitalWrite(in4,HIGH);
        analogWrite(enB,s);
      }

     else if (direc >=350 && direc <=360 | direc <=10 && direc >=0){ // RIGHT
        digitalWrite(in1,HIGH);
        digitalWrite(in2,LOW);
        analogWrite(enA,s);
        digitalWrite(in3,LOW);
        digitalWrite(in4,HIGH);
        analogWrite(enB,s);
     }

      else if (direc >=170 && direc <=190){ // LEFT
        digitalWrite(in1,LOW);
        digitalWrite(in2,HIGH);
        analogWrite(enA,s);
        digitalWrite(in3,HIGH);   
        digitalWrite(in4,LOW);
        analogWrite(enB,s);
      }

      else if (direc >=120 && direc <=150){ //FORWARD LEFT
        digitalWrite(in1,LOW);
        digitalWrite(in2,HIGH);
        analogWrite(enA,s);
        digitalWrite(in3,LOW);
        digitalWrite(in4,LOW);
        analogWrite(enB,s);
      }

      else if (direc >=30 && direc <=60){ // FORWARD RIGHT
        digitalWrite(in1,LOW);
        digitalWrite(in2,LOW);
        analogWrite(enA,s);
        digitalWrite(in3,LOW);
        digitalWrite(in4,HIGH);
        analogWrite(enB,s);
      }

     else if (direc >=210 && direc <=259){ // BACKWARD LEFT
        digitalWrite(in1,HIGH);
        digitalWrite(in2,LOW);
        analogWrite(enA,s);
        digitalWrite(in3,LOW);
        digitalWrite(in4,LOW);
        analogWrite(enB,s);
     }

     else if (direc >=300 && direc <=340){ // BACKWARD RIGHT
        digitalWrite(in1,LOW);
        digitalWrite(in2,LOW);
        analogWrite(enA,s);
        digitalWrite(in3,HIGH);
        digitalWrite(in4,LOW);
        analogWrite(enB,s);
     }


      else{ //If bluetooth module receives any value not listed above, both motors turn off
        digitalWrite(in1,LOW);
        digitalWrite(in2,LOW);
        analogWrite(enA,0);
        digitalWrite(in3,LOW);
        digitalWrite(in4,LOW);
        analogWrite(enB,0);
      }
    }
  }
  • 1
    @user35247 - Please don't literally "fix" the code by editing the question. Post an answer instead. – Nick Gammon Aug 4 '17 at 6:13
0
      inChar = Serial.read(); // Read a character

      data[index] = inChar; // Store it

The above is almost certainly not going to work as expected. You need to check if there is serial data available before reading it. (ie. call Serial.available()).

I have a page about reading from serial that might help you with that.

For example, your loop might look like this:

void loop()
  {
  // if serial data available, process it
  while (Serial.available () > 0)
    processIncomingByte (Serial.read ());

  // do other stuff here like testing digital input (button presses) ...

  }  // end of loop

Notice that the Serial.read is only done if Serial.available is true?

  • what about this part of the code though inline char* command = strchr (data, ":"); if (command !=0) { *command = 0; int direc = atoi (data); ++command; int speeds = atoi (command); Serial.println(speeds); Serial.println(direc); – Abdallah Mohamed Aug 4 '17 at 6:33
  • What about it? You need to fix what I mentioned first, and probably add a debugging display to show what sort of string is being passed to that piece of code. – Nick Gammon Aug 4 '17 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.