当前位置:首页 > 电子信息 > 正文内容

线性时钟源程序

admin1个月前 (04-08)电子信息2

线性时钟源程序:

#include <AFMotor.h>#include <WString.h>// These set up the motors.The values here depend on how they've been wired up.AF_Stepper minuteHand(20, 1); // minutesint const minutesIncrement = BACKWARD;int const minutesDecrement = FORWARD;AF_Stepper hourHand(20, 2); // hoursint const hoursIncrement = FORWARD;int const hoursDecrement = BACKWARD;int const stepType = DOUBLE;int const motorRPM = 150;int second=0, minute=0, hour=0; // declare TIme variables// Minutes setupint startMinutePos = 0;// current minute posiTIon.This gets updated all the TIme.float currentMinutePos = startMinutePos;// the current posiTIon of the indicators.This only gets updated when the hands moveint displayMinutePos = startMinutePos;// Hours setupint startHourPos = 0;float currentHourPos = startHourPos;int displayHourPos = startHourPos;// These are the actual time, in seconds minutes and hours.// doTick() writes these values, and renderTime() reads them.int currentSeconds = 0;int currentMinutes = 50;int currentHours = 11;int const millisPerSecond = 200;int const stepsPerClock = 592;float const stepsPerMinute = stepsPerClock/60.0;float const stepsPerHourMinute = stepsPerClock/720.0;float const stepsPerHour = stepsPerClock/12.0;void setup() {Serial.begin(9600); // set up Serial library at 9600 bpsSerial.println("LINEAR CLOCK!");hourHand.setSpeed(motorRPM);// 10 rpmminuteHand.setSpeed(motorRPM);// 10 rpm }void loop() {static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second// (static variables are initialized once and keep their values between function calls)// move forward one second every 1000 millisecondsunsigned long time = millis();unsigned long sinceLastTick = time - lastTick;if (sinceLastTick >=millisPerSecond){unsigned int secondsMissed = sinceLastTick / millisPerSecond;lastTick = millis();for (int i = 0; i < secondsMissed; i++){doTick();}}renderTime();}void doTick(){currentSeconds++;if (currentSeconds > 59){currentSeconds = 0;currentMinutes++;if (currentMinutes > 59){currentMinutes = 0;currentHours++;if (currentHours > 11){currentHours = 0;}}}Serial.print(millis());Serial.print(" - tick!");Serial.print(currentHours);Serial.print(":");Serial.print(currentMinutes);Serial.print(":");Serial.println(currentSeconds);}void renderTime(){setTime(currentHours, currentMinutes);}void setTime(int hour, int minute){setHour(hour, minute);setMinute(minute);}void setHour(int hour, int minute){// work out the new position and set it globally eg time 4:25.// first hours// eg 0.2055 * 4 * 60 = 49.333float justHours = stepsPerHourMinute * hour * 60;// eg 0.2055 * 25 = 5.13888float justMinutes = stepsPerHourMinute * minute;// stick em together: position is 54.472 (4:25)currentHourPos = justHours + justMinutes;// round to integer// eg 54int newPos = currentHourPos;// save the previous actual position so we can check if the hands need moving// eg 52int lastPos = displayHourPos;// now see if the hand position needs to change// eg 54 - 52 = 2int stepsToChange = newPos - lastPos;if (stepsToChange != 0){Serial.print("sethour:");Serial.println(hour);// update the global variabledisplayHourPos = newPos;changeHours(stepsToChange);}}void changeHours(int steps){Serial.print("Incrementing hours position:");Serial.println(steps);// make it positiveint absSteps = abs(steps);if (steps < 0){// if it's negative, then DECREMENTif (absSteps >= 1){// only step if it's a full stephourHand.step(absSteps, hoursDecrement, stepType);}}else if (steps > 0){// if it's positive then INCREMENTif (absSteps >= 1){// only step if it's a full stephourHand.step(absSteps, hoursIncrement, stepType);}}Serial.print("Actual current hour position:");Serial.println(displayHourPos);hourHand.release();}void setMinute(int minute){// work out the new position and set it globally// eg 2.467 * 25 = 61.675currentMinutePos = stepsPerMinute * minute;// round to integer// eg 61int newPos = currentMinutePos;// save the previous actual position so we can check if the hands need moving// eg 59int lastPos = displayMinutePos;// now see if the hand position needs to change// eg 61 - 59 = 2int stepsToChange = newPos - lastPos;if (stepsToChange != 0){Serial.print("setmin:");Serial.println(minute);// update the global variabledisplayMinutePos = newPos;changeMinutes(stepsToChange);}}void changeMinutes(int steps){Serial.print("Incrementing minutes position:");Serial.println(steps);// make it positiveint absSteps = abs(steps);if (steps < 0){// if it's negative, then DECREMENTif (absSteps >= 1){// only step if it's a full stepminuteHand.step(absSteps, minutesDecrement, stepType);}}else if (steps > 0){// if it's positive then INCREMENTif (absSteps >= 1){// only step if it's a full stepminuteHand.step(absSteps, minutesIncrement, stepType);}}Serial.print("Actual current minute position:");Serial.println(displayMinutePos);minuteHand.release();} 

扫描二维码推送至手机访问。

版权声明:本文由机电信息网发布,如需转载请注明出处。

分享给朋友:

相关文章

三菱电机公开薄膜硅型太阳能电池的开发情况

三菱电机公开薄膜硅型太阳能电池的开发情况             &n...

“铁电池”名不副实比亚迪汽车发展存疑

“铁电池”名不副实比亚迪汽车发展存疑2007年比亚迪最大的新闻点就是“铁电池”,具体这种电池是怎样的技术呢?某电池业内的知情人士表示:“比亚迪的‘铁电池’是磷酸铁锂电池,可能是出于宣传上的需要,简称为...

变压器空载运行时一侧高压该如何处理?怎样设计一个脉冲变压电路

变压器空载运行时一侧高压该如何处理?怎样设计一个脉冲变压电路

 变压器空载运行时一侧高压该如何处理? 当空载变压器高压侧缺相运行时,有极大的可能性激发起断线谐振,特别时当高压电源用电缆接入或断线点离变压器距离较远时,过电压倍数可以达2倍以上,称断线过电压,原因是...

电解电容器的漏电阻测量方法

电解电容器的漏电阻测量方法

 电解电容器是指在铝、钽、铌、钛等金属的表面采用阳极氧化法生成一薄层氧化物作为电介质,以电解质作为阴极而构成的电容器。本文主要介绍了电解电容器的漏电阻测量方法及过程详解和注意的事项。 电解电容器的用途...

光电二极管,什么是光电二极管

光电二极管光电二极管(也叫光敏二极管)是将光信号变成电信号的半导体器件,与光敏电阻相比具有灵敏度高、高频性能好,可靠性好、体积小、使用方便等优点。它的核心部分也是一个PN 结,和普通二极管相比,在结构...

锂电池电解液对人体有什么危害

锂电池电解液对人体有什么危害

锂电池主要使用的电解质有高氯酸锂、六氟磷酸锂等。但用高氯酸锂制成的电池低温效果不好,有爆炸的危险,日本和美国已禁止使用。而用含氟锂盐制成的电池性能好,无爆炸危险,适用性强,特别是用六氟磷酸锂制成的电...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。