-1

I am trying to make pins 13, 12, 10 and 9 to switch between high and low. That is, when pin 13 is high, pin 12 should be low, then pin 10 should be high and pin 9 should be low.

Code:

 unsigned char driver[4] ={13, 12, 10, 9};
 String mystring = "";
 void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   for(int i = 0; i < 4; i++){
     pinMode(driver[i], OUTPUT);
   }
 }

 void backward(){
   digitalWrite(driver[0], HIGH);
   digitalWrite(driver[1], LOW);
   digitalWrite(driver[2], HIGH);
   digitalWrite(driver[3], LOW);
 }

 void forward(){
   digitalWrite(driver[0], LOW);
   digitalWrite(driver[1], HIGH);
   digitalWrite(driver[2], LOW);
   digitalWrite(driver[3], HIGH);  
 }

 void STOP(){
   digitalWrite(driver[0], LOW);
   digitalWrite(driver[1], LOW);
   digitalWrite(driver[2], LOW);
   digitalWrite(driver[3], LOW);   
 }

 void loop() {
   // put your main code here, to run repeatedly:
   while(Serial.available()){
     delay(10);
     char mychar = Serial.read();
     if(mychar == '\0')
       break;
     mystring += mychar;
   }

   if(mystring == "F" || mystring == "f"){
     forward();
     mystring = "";
     Serial.println("moving forward!");
   }

   else if(mystring == "B" || mystring == "b"){
     backward();
     mystring = "";
     Serial.println("moving backward!");
   }

   else if(mystring == "S" || mystring == "s"){
     STOP();
     mystring = "";
     Serial.println("stopped!");
   }

   else
     mystring = "";
   }

When I test it, there is no switching, that is when moving backward, pin 13 is high throughout, pin 12 is low throughout, pin 10 is high throughout, and pin 9 is low throughout

0

What line ending are you using in your IDE? Because if it is other than 'no line ending', you are also sending a new line, carriage return or both. Those control caracter get included in mystring.

Better to be safe and filter out control caracter. Try this:

String mystring = "";

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

void loop() {

  while (Serial.available()) {
    delay(10);
    char mychar = Serial.read();
    if (mychar == '\0')
      break;

    if (isAlpha(mychar)) {
      //  Doesn't include control caracters in mystring
      mystring += mychar;
    }
  }

  if (mystring != "") {
    Serial.print("***"); Serial.print(mystring); Serial.println("***");
    if (mystring == "F") {
      Serial.println("mystring is equal to 'F'");
    } else {
      Serial.println("mystring is not equal to 'F'");
    }
    mystring = "";
  }
}
  • Yes, but at that point, why even bother wiht the expensive String concatenation and comparison when you can simply compare the individual characters one at a time as received? Carry out the appropriate actions on the condition of expected ones, and ignore the others. – Chris Stratton Jun 29 '17 at 18:56
  • You are right; no need for string concatenation, but I want to show the minimal change that solve the problem. – user31481 Jul 1 '17 at 8:43

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.