完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
跟教授做的一个课题,Arduino Uno R3连接增量编码器,现在的语句使用的是一个外部中断,条件是测量A端的一个上行信号,通过计算得到编码器的转速,结合传动比和其他特征算出器材本身的运行速度,这个目的已经达到。
现在希望加的一个要求是,机器停止时,Arduino 连接的显示屏上的示数显示为0. 考虑了 1:如果A端和B端的信号不再发生变化,都处在LOW的状态时,使显示示数为0 但是平时运行的时候,也是时而会出现两个端口信号都LOW的状态,不能在停止时保证,正好达成条件,示数为 零。 2:也曾编写语句,通过判断A端和B端信号,使得平时运行速度显示为0,但停止时,显示了运行速度。 希望能有大神指个方向,我如何才能正确约束条件,使机器在停止时,Arduino能正确识别到信号,执行速度为零的语句。 不胜感谢。 源代码在下面和附件中 //备注里是我的思路,但是不能达到目标 #include // LiquidCrystal display with: // rs on pin 12 // rw on pin 11 // enable on pin 10 // d4, d5, d6, d7 on pins 4, 5, 6, 7 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); /* Incremental Encoder Read an Incremental Encoder with interrupts ENCODER_A_PIN to pin 2 ENCODER_B_PIN to pin 3 get the revolutions in a duration, calculate revolutions per minute use the pull-up resistor */ //setup our pins #define ENCODER_A_PIN 2 #define ENCODER_B_PIN 3 int rpm = 0; //revolutions per minute long position = 0; //long old_position = 0; long speed; long time; long old_time = 0; void setup() { pinMode(ENCODER_A_PIN, INPUT_PULLUP); pinMode(ENCODER_B_PIN, INPUT_PULLUP); lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner lcd.begin(16, 2); //Set up the LCD's number of columns and rows: lcd.setCursor(0, 0); attachInterrupt(0, Inkrementalgeber, RISING); } void loop() { /* if(old_position != position) { rpm = 60000000 / (time - old_time); speed= 0.1 * rpm * 10; // the grade of screw arbor is 1 mm = 0.1cm } else { speed= 0; }*/ rpm = 60000000 / (time - old_time); speed= 0.1 * rpm * 10; // the grade of screw arbor is 1 mm = 0.1cm lcd.setCursor(0, 0); //move to the begining of the second line lcd.print("position="); lcd.setCursor(9, 0); lcd.print(" "); //make sure the rest of the display is clean lcd.setCursor(9, 0); lcd.print(position,DEC); lcd.setCursor(0, 1); //move to the begining of the second line lcd.print("speed="); lcd.setCursor(6, 1); lcd.print(" "); //make sure the rest of the display is clean lcd.setCursor(6, 1); lcd.print(speed / 10); lcd.print("."); lcd.print(speed % 10); //get the decimal number lcd.print("cm/min "); //show the unit of the speed delay(100); //waits for 200 milliseconds } void Inkrementalgeber() { //old_position= position; position++; if(position >=3125) //the new revolution begin { position = 0; old_time = time; //get the new round time time = micros(); } }
|
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
WIO Terminal +MCP2515 实现车辆OBD的速度监控
10822 浏览 0 评论
60660 浏览 77 评论
6419 浏览 3 评论
一块扩展板完成Arduino的10类37项实验(代码+图形+仿真)
41364 浏览 219 评论
10329 浏览 0 评论
774浏览 0评论
275浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 03:04 , Processed in 0.738473 second(s), Total 73, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
2132