用光电开关测量电机速度,但是串口显示器一直没有数据,请各位看看程序该怎么写
#include "RoSys.h"#define L2 //定义引脚2为光电开关接口
RoDCMotor motor1(MA); //电机1
RoDCMotor motor2(MB); //电机2
uint8_t motorSpeed = 100; //设置电机的速度
floattime1;
int light;
floatk=0 ;
//定义变量
void setup()
{
Serial.begin(9600);
pinMode(L,INPUT);
}
void loop()
{
motor1.run(motorSpeed); //值: -255 - 255,负值代表反转
motor2.run(motorSpeed); //值: -255 - 255,负值代表反转
time1=millis();
light=digitalRead(L);
if(light==1)
{
k+=1;
}
if(time1==1000)
{
float s=k/time1*1000;
Serial.print("The speed is:");
Serial.print(s);
Serial.println("n/s");
Segment.display(s);
}
} millis函数单位是毫秒,说白了你的程序就只在1000ms的时候会有一次输出,如果1000ms的时候恰好你的程序在搞别的事情,比如执行motor1.run或者k+=1之类的,导致没有一次时间是1000,那么就会没有输出 搜下 Arduino 中断 不晓得是用的是什么库,millis是板子上电就开始计时,所以在你打开串口的所用的时间已经超过一秒了,可以在社区搜下测速程序,观察这些程序中对时间是怎么分配的,再来修改你的程序 ignore 发表于 2017-3-24 11:21
不晓得是用的是什么库,millis是板子上电就开始计时,所以在你打开串口的所用的时间已经超过一秒了,可以在 ...
多谢,我会再好好看看 幻生幻灭 发表于 2017-3-24 08:06
搜下 Arduino 中断
使用中断触发计数吗 仰望0星空 发表于 2017-3-26 20:03
使用中断触发计数吗
是
页:
[1]