I'm using Serial monitor to control motors

I have the code for a H bridge and the code to use serial but i dont know how to combine them so like if i send FORWARD to serial the motors will rotate. here is my serial code:

void setup(){
  Serial.begin(9600);
}

void loop(){
  //check to see if anything is available in the serial recive buffer
  while (Serial.available() > 0)
  {
  // create a place to hold the incomeing message
  static char message[MAX_MESSAGE_LENGTH];
  static unsigned int message_pos = 0;
  //Read the next avalible byte in the serial buffer
  char inByte = Serial.read();
  //Message comeing in (check not terminarteing char)
  if (inByte != '\n' &&(message_pos < MAX_MESSAGE_LENGTH - 1))
  {
    message[message_pos] = inByte;
    message_pos++;
  }
  else 
  {
  message[message_pos] = '\0';
  Serial.println(message);
  message_pos = 0;
  }
  
  }
  }



  

What Arduino board?

What motor driver do you have and how is it connected?

Can you post a schematic or wiring diagram?

It is much easier to work with single letter commands ('f' for forward) than whole words.

you didn't post any code to control the motors

look this over
fill in the code to control the motors

a case for handling digits could be added to set a numeric value to control speed

Serial.readBytesUntil() can be used to read a multi-character string

void back    () { Serial.println ("back"); }
void forward () { Serial.println ("forward"); }
void left    () { Serial.println ("left"); }
void stop    () { Serial.println ("stop"); }
void right   () { Serial.println ("right"); }

void loop ()
{
    if (Serial.available() > 0) {
        char c = Serial.read ();

        switch (c) {
        case 'b':
            back ();
            break;
        case 'f':
            forward ();
            break;
        case 'l':
            left ();
            break;
        case 's':
            stop ();
            break;
        case 'r':
            right ();
            break;
        default:
            break;
        }
    }
}


void setup(){
    Serial.begin(9600);
}

thank you. i was using l29N dual H bridge so thank you. i will put my code in to it THANK YOU SO MUCH

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.