I am new to Arduino, and am loving it. Many times you need to inject a pause in your sketch (like to flash a LED for a certain period of time). Using the delay() function cause your entire sketch to 'freeze' until the delay value expires. This can cause issues like needing to continuously read an input source like a trimmer pot. To get around this, I found a good example on the net on using the millis() and have incorporated it into my sketches. Below is my slightly modified version:
// Set a speed interval in mS
unsigned long SpeedInterval = 1000;
// Get the current timestamp
unsigned long currentMillis = millis();
// Set a timestamp mark
previousMillis = currentMillis;
// Keep looking until more milliseconds have passed
// than the desired interval
// This is a better way instead of using delay()
// since delay() halts execution and this method
// allows for continued processing
while((currentMillis - previousMillis) < SpeedInterval){
// Set a timestamp
currentMillis = millis();
// Do stuff in the loop like:
// potSpeedVal = analogRead(potSpeedPin);
// digitalWrite(outPins[n], LOW);
// whatever you like to be executed during this time period
}
Tuesday, April 12, 2011
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment