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