针脚没有切换

时间:2017-06-16 17:32:58

标签: arduino

我正在尝试使引脚13,12,10和9在高电平和低电平之间切换。也就是说,当引脚13为高电平时,引脚12应为低电平,然后引脚10应为高电平,引脚9应为低电平。

代码:

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 = "";
}

当我测试时,没有切换,即向后移动时,引脚13始终为高电平,引脚12始终为低电平,引脚10为高电平,引脚9为低电平。

1 个答案:

答案 0 :(得分:0)

试试这个

unsigned char driver[4] ={13, 12, 10, 9};
String mystring = "";
String state = "S";
long lastSwitch;
long switchIntaval = 10;

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()){
    char mychar = Serial.read();
    if(mychar == '\0')
      break;
    mystring += mychar;
    delay(1);
  }
  if(state != "S" && state != "s") {
    if(millis()-lastSwitch >= switchIntaval) {
      lastSwitch = millis();
      for(int i = 0; i < 4; i++){
        digitalWrite(driver[i], !digitalRead(driver[i]));
        //Serial.println("Switching: " + String(i));
      }
    }
  }
  if(mystring == "F" || mystring == "f"){
    state = mystring;
    forward();
    mystring = "";
    Serial.println("moving forward!");
  }
  else if(mystring == "B" || mystring == "b"){
    state = mystring;
    backward();
    mystring = "";
    Serial.println("moving backward!");
  }
  else if(mystring == "S" || mystring == "s"){
    state = mystring;
    STOP();
    mystring = "";
    Serial.println("stopped!");
  }
  else
    mystring = "";
}