Arduino Serial.read()没有显示正确的输出

时间:2014-10-31 00:40:11

标签: serial-port arduino switch-statement

我正在为arduino编写这个程序来控制几个步进电机。我从串口读取所有数据并将其放入数组中。现在这对于粉丝来说效果非常好,但是当我在“case”语句中输入等于第二个值的值时,它会发挥作用。其他任何工作正常。

i.e.)from serial input: "F"
                 output: "F" and the fan turns on/off
but              input:  "a100" 
                 output: "a1"  and nothing the serial com freezes

Aduino Mega 2500 R3:

Arduino IDE 1.0.6

 #include <Stepper.h>
 #define FAN_PIN 9                                // the fan is hooked up to pin 9


 const int stepsPerRevolution = 200;              // change this to fit the number of steps per             
 revolution for your motor
 const int RPM = 30;                               // much over thirty is too fast to respond to      
 int step_mode = 4;                               // 4 = full, 8 = half
 int yPosition = 0;                               // number of steps the motor has taken
 byte byte_step;                                  // serial only reads in bytes
 long previous = stepsPerRevolution/2;            // This may come in handy eventually idk
 char dataIn[20];
 char data;
 int step_size[20];
 int count = 0;
 int count_new;
 int i;
 int num_steps = 0;
 boolean neg = false;                            // Default to positive steps
 boolean FAN = false;                            // Default to fan off

 // initialize the stepper library on pins :
 Stepper myStepper(stepsPerRevolution, 60,61,56,57, step_mode);             
 //Stepper(number_of_steps, motor_pin_1, motor_pin_2, motor_pin_3, motor_pin_4)   


void loop() {
  if (Serial.available()>0){
      command = Serial.read();
      while(data != 13) {    
          data = Serial.read();
          if(data == '-') neg = true;                     //    case you receive a negative, defaults to false (positive)
           else{
              dataIn[count] = data;
              Serial.println(count);
              count++;                 // count is NOT!!!!! equal to the number of relevant elements within the array
           delay(1);
     }
  }   

 switch(command){
    case 'F':                                      // case to turn the fan on/off
       if (FAN == false){
           FAN = true;
           analogWrite(FAN_PIN,90);
       }                                          //   WRITE(FAN_PIN, HIGH); PWM because we have a 5V fan on a 12V pin
       else if(FAN == true){
           FAN = false;
           digitalWrite(FAN_PIN,LOW);               //   turn the fan off
       }
       break;

    case 'G':                               // the case of motor "a"       
        for(i=2; count-1; i++){
                 num_steps = num_steps *10 + ;    //convert it to a base 10 number
           }
        break;   
  }                     
  if(neg == true){
      num_steps = num_steps*-1;    // Make it negative then default it back to positive
      neg = false; 
  }
  myStepper.step(num_steps);
  yPosition = yPosition + num_steps;
  Serial.println(yPosition);
  for(i=0; i<20; i++){           //clear out the data
      dataIn[i] = 0;
  }
  count = 0;
  num_steps = 0;

}
}

0 个答案:

没有答案