为什么我的串口在Arduino IDE上通过串口监视器打印两次?

时间:2017-05-01 10:27:44

标签: c++ arduino

我只输入了一个'打印命令'但是我得到了两个打印读数。

该程序驱动两个步进电机。 moveSteps值= 48

当程序开始运行时,电机短暂停止并打印'48',然后在程序结束前触发if时再打印'48'。

只能打印一个'48'。任何想法为什么会这样?

 /*
  Precise movement with stop 

  Moves the robot 20mm forwards and 20mm backwards 

  Rob Miles (edited by Dileepa Ranawake)

  April 2017
  Version 1.0

 */

int motorDelay;

byte left1,left2,left3,left4;

byte right1,right2,right3,right4;

float wheelDiameter = 68.5;
float stepsPerRevolution = 512;
float mmsPerStep = (wheelDiameter * 3.1416) / stepsPerRevolution;

int moveCount;
int moveSteps;  // number of steps the motor is to move 

void leftForwards()
{
  left1=7; left2=6; left3=5; left4=4;
}

void leftReverse()
{
  left1=4; left2=5; left3=6; left4=7;
}

void rightForwards()
{
  right1=8; right2=9; right3=10; right4=11;
}

void rightReverse()
{
  right1=11; right2=10; right3=9; right4=8;
}

int calculateDistanceSteps(float distanceInMM)
{
  return distanceInMM / mmsPerStep + 0.5;
}

void setup() {
  leftForwards();  
  rightForwards();

  pinMode(left1,OUTPUT);
  pinMode(left2,OUTPUT);
  pinMode(left3,OUTPUT);
  pinMode(left4,OUTPUT);
  digitalWrite(left1,HIGH);

  pinMode(right1,OUTPUT);
  pinMode(right2,OUTPUT);
  pinMode(right3,OUTPUT);
  pinMode(right4,OUTPUT);
  digitalWrite(right1,HIGH);

  motorDelay=1200;

  moveCount=0;
  moveSteps = calculateDistanceSteps(20);
  Serial.begin(9800);

}

void loop() {

  moveCount = moveCount + 1;

  if (moveCount==moveSteps) 
  {
  digitalWrite(left1,LOW);
  digitalWrite(right1,LOW);
  Serial.println(moveCount);
  exit(0);
  }

  digitalWrite(left2,HIGH);
  digitalWrite(right2,HIGH);
  delayMicroseconds(motorDelay);
  digitalWrite(left1,LOW);
  digitalWrite(right1,LOW);
  delayMicroseconds(motorDelay);
  digitalWrite(left3,HIGH);
  digitalWrite(right3,HIGH);
  delayMicroseconds(motorDelay);
  digitalWrite(left2,LOW);
  digitalWrite(right2,LOW);
  delayMicroseconds(motorDelay);
  digitalWrite(left4,HIGH);
  digitalWrite(right4,HIGH);
  delayMicroseconds(motorDelay);
  digitalWrite(left3,LOW);
  digitalWrite(right3,LOW);
  delayMicroseconds(motorDelay);
  digitalWrite(left1,HIGH);
  digitalWrite(right1,HIGH);
  delayMicroseconds(motorDelay);
  digitalWrite(left4,LOW);
  digitalWrite(right4,LOW);
  delayMicroseconds(motorDelay);

}

Serial Monitor打印出4848

我也注意到只需打开串行监视器就可以使步进电机移动!

1 个答案:

答案 0 :(得分:1)

exit()与Arduino一起使用并不标准。它基本上禁用所有中断并进入无限循环。您可以像这样重组loop()以避免它:

void loop()
{
    // Still moving?
    if (moveCount < moveSteps) {
        moveCount = moveCount + 1;

        // Move complete
        if (moveCount == moveSteps) 
        {
            digitalWrite(left1,LOW);
            digitalWrite(right1,LOW);
            Serial.println(moveCount);
        }
        else {
            digitalWrite(left2,HIGH);
            digitalWrite(right2,HIGH);
            //etc.....
        }
    }
}

此外,您的循环延迟1200μs8x。那只是1200×8 =9600μs= 9.6 ms。如果moveSteps = 48那么整个循环只需要460.8 ms。程序在打开串行监视器之前运行一次,然后再运行一次。如果在打开串行监视器后按下复位按钮会发生什么?

您是否考虑过使用内置Stepper Library的Arduino?

最后,考虑将来发布[arduino.se]