Eliminate Delay inArduino

// Free Tutorial Arduino
// www.ioisalman.com
// Sketch Eliminate Delay inArduino

// A0 pin is a pin Analog
// Pin 9 is pin PWM digital support
const int pinPot = A0;
const int pinLED = 9;

void setup () {
   pinMode (pinPot, INPUT);
   pinMode (pinLED, OUTPUT);
}

long lastTime = 0;
sensor int = 0;
int ledValue;

void loop () {
   // Read the value of A0 feet (sensor, potentiometer)
   sensor = analogRead (pinPot);
  
   // LED will be on / off duration = value sensor
   // If the value of the sensor 100, the duration is 100ms
   if (millis ()> lastTime + sensor) {
     if (ledValue == LOW) {
       ledValue = HIGH;
     } Else {
       ledValue = LOW;
     }
    
     // Set lastTime dg value of millis () new
     lastTime = millis ();
     digitalWrite (pinLED, ledValue);
   }
}


             Program on Sketch above have the same functionality as Sketch in previous posts, the difference, the Sketch on top of responsive version. So when the potentiometer is rotated, then the LED blink duration soon change. It works like this:

             When talking about the delay, then we are talking about time. In Sketch above, the time delay is dynamic because its parameters are present (drawn from millis ()). While the lag time for change is the change time + value of the potentiometer. To better understand what I mean, consider Figure below.

Cycle change of status LEDs and a potentiometer

Cycle change of status LEDs and a potentiometer


             In the picture above, the fourth stage from top to bottom it is the flame condition or not LED. LastTime value is calculated from the change in the potentiometer or endless pause based on the value of the sensor is specified.

Step 
  1. The first condition LED is ON, lastTime obtained from the last change or the last call millis (). When the sensor value (green) is up, then the LED will be OFF, millis () will be called and will be of value lastTime on the second step.
  2. Step two, LED OFF conditions during sensor values ​​millisecond, every time the value of millis () will be examined. The dashed line indicates the function call time millis () in conditioning

    if (millis ()> lastTime + sensor)

    if the value of millis () is greater than lastTime + sensor, the LED OFF time has run out and replaced with LED ON time.
  3. Step three shows that the value potensimeter raised so that the duration it takes to turn on the LED longer. Broken lines indicate that the process calling millis () in the IF is still halfway. But amid the journey, a potentiometer value diminished.
  4. After potentiometer turned down, then the duration of the first still half way, now just a little bit and LED are still in the ON state.

             Hopefully the above illustration to explain how the program works on the Sketch on top.

             But briefly, on line 15 lastTime not int (integer). LastTime data type is long (long integer). Any difference with integer without long? 

long lastTime = 0;


             To explain it, the following is a table of numbers and
capacity:

             Note that the long capacity greater than int. The table above is valid only for Arduino only, so if for no other computer can be different depending on the architecture and capabilities of the computer in the chopped.

Subscribe to receive free email updates:

0 Response to "Eliminate Delay inArduino"

Post a Comment