程序是用来计车轮转的圈数。用串口监视的时候,手动车轮,可以很好的计数。但是通上电后,串口监视显示计数就不正确的了,计数到48就结束了。想请教一下,这是为什么?是车速的问题还是程序的问题?
const int EN2 = 5;// the required pins for motor drive
const int M2 = 4;
int n;
void setup()
{
pinMode(EN2,OUTPUT);// the pins of motor dive are both outputs
pinMode(M2,OUTPUT);
Serial.begin(9600);//initialize the serial port
attachInterrupt(0,count,RISING); //monitor the state of input
}
void loop()
{
while(n<=200)
{
goback();
}
}
void count()// the function of interrupt
{
n++;
Serial.println(n);
}
void goback (void)
{
analogWrite(EN2,191);
digitalWrite(M2,LOW);
} |