Advertisement
Seelenkind

Arduino Blink without delay

Apr 21st, 2020
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. int oldmillis, currentmillis, oldmillis2, currentmillis2, oldmillis3, currentmillis3, interval = 1000, interval2 = 2000, interval3 = 3000;
  3. bool time_past = LOW, pin = LOW, time_past2 = LOW, pin2 = LOW, time_past3 = LOW, pin3 = LOW;
  4.  
  5. // use #define to save memory instead of variables (byte LED = 1) for pins that don't change anyway
  6. #define LED 1
  7. #define LED2 2
  8. #define LED3 3
  9.  
  10. void setup() {
  11.   Serial.begin(9600);
  12.   pinMode(LED, OUTPUT);
  13.   pinMode(LED2, OUTPUT);
  14.   pinMode(LED3, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18.  
  19.   // This part is executed every 1 seconds
  20.   // *********** BEGIN *******************
  21.   if (time_past == LOW) // set oldmillis with the current value of millis () only if time_past is LOW
  22.   {
  23.     oldmillis = millis();
  24.     time_past = HIGH;
  25.   }
  26.   currentmillis = millis(); // get current millis () and save it in the variable currentmillis
  27.   if ((currentmillis - oldmillis) > interval) // only do the following if currrentmillis-oldmillis is greater than interval (1 second has passed)
  28.   {
  29.     time_past = LOW; //Set status for time_past to LOW so that it can be reset
  30.     pin = !pin; // negate the value of pin, make LOW to HIGH, make High to LOW
  31.     digitalWrite(LED, pin); //write the new value of pin to the digital output
  32.     if (pin) Serial.println("LED 1= ON");
  33.     if (!pin) Serial.println("LED 1= OFF");
  34.   }
  35.   // ************ END *******************
  36.  
  37.  
  38.   // This part is executed every 2 seconds
  39.   // *********** BEGIN *******************
  40.   if (time_past2 == LOW) // set oldmillis with the current value of millis () only if time_past is LOW
  41.   {
  42.     oldmillis2 = millis();
  43.     time_past2 = HIGH;
  44.   }
  45.   currentmillis2 = millis(); // get current millis () and save it in the variable currentmillis
  46.   if ((currentmillis2 - oldmillis2) > interval2) // only do the following if currrentmillis-oldmillis is greater than interval2 (2 second has passed)
  47.   {
  48.     time_past2 = LOW; //Set status for time_past to LOW so that it can be reset
  49.     pin2 = !pin2; // negate the value of pin, make LOW to HIGH, make High to LOW
  50.     digitalWrite(LED2, pin2); //write the new value of pin to the digital output
  51.     if (pin2) Serial.println("LED 2= ON");
  52.     if (!pin2) Serial.println("LED 2= OFF");
  53.   }
  54.   // ************ END *******************
  55.  
  56.   // This part is executed every 3 seconds
  57.   // *********** BEGIN *******************
  58.   if (time_past3 == LOW) // set oldmillis with the current value of millis () only if time_past is LOW
  59.   {
  60.     oldmillis3 = millis();
  61.     time_past3 = HIGH;
  62.   }
  63.   currentmillis3 = millis(); // get current millis () and save it in the variable currentmillis
  64.   if ((currentmillis3 - oldmillis3) > interval3) // only do the following if currrentmillis-oldmillis is greater than interval3 (3 second has passed)
  65.   {
  66.     time_past3 = LOW; //Set status for time_past to LOW so that it can be reset
  67.     pin3 = !pin3; // negate the value of pin, make LOW to HIGH, make High to LOW
  68.     digitalWrite(LED3, pin3); //write the new value of pin to the digital output
  69.     if (pin3) Serial.println("LED 3= ON");
  70.     if (!pin3) Serial.println("LED 3= OFF");
  71.   }
  72.   // ************ END *******************
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement