c - Arduino creating delay function -
i trying make own delay function. briefly mydelay
function calling toggled
function every secs
seconds. code not written, know (this first version). trying work properly. refactor it. have unexpected bugs. first time loop in x
function working properly. printing "i in while" 1 second , prints "im ending mydelay" behaviour want. after finishing loop in x
. second time when loops. enters mydelay
function (that ok). not printing "i in while" @ all. prints "im ending mydelay" not good.
here code:
#include <arduino.h> int led = 7; void setup() { serial.begin(9600); pinmode(led, output); } void loop() { x(); serial.println("im ending main loop"); } void x() { (int = 0; <= 10; i++) { mydelay(led, 0, 1); mydelay(led, 1, 1); } } void mydelay(int pin, int hol, int secs) { int starttime = millis(); while ((millis() - starttime) <= (secs * 1000)) serial.println("i in while"); toggled(pin, hol); serial.println("im ending mydelay"); } void toggled(int pin, int hol) { digitalwrite(led, hol); }
change int starttime = millis();
unsigned long starttime = millis();
. might problem because if using int
, program go crazy after 32 seconds. problem because int
can hold number going -32,768 32,767 .
also, might try this:
while ((millis() - starttime) <= (secs * 1000)) { serial.println("i in while"); }
Comments
Post a Comment