Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. /* Arduino Multitasking
  2. Author : CircuitDigest (circuitdigest.com)
  3. */
  4.  
  5. int led1 = 6; // led1 connected at pin 6
  6. int led2 = 7; // led1 connected at pin 7
  7. int toggleLed = 5; // push button controlled led connected at pin 5
  8. int pushButton = 2; // push butoon connected at pin 2 which is also interrupt pin
  9.  
  10. int ledState1 = LOW; // to determine the states of led1 and led2
  11. int ledState2 = LOW;
  12.  
  13. unsigned long previousMillis1 = 0; //store last time LED1 was blinked
  14. const long period1 = 1000; // period at which led1 blinks in ms
  15.  
  16. unsigned long previousMillis2 = 0; //store last time LED2 was blinked
  17. const long period2 = 200; // period at which led1 blinks in ms
  18.  
  19. int debouncePeriod = 20; // debounce delay of 20ms
  20. int debounceMillis = 0; // similar to previousMillis
  21.  
  22. bool buttonPushed = false; // interrupt routine button status
  23. int ledChange = LOW; // to track the led status last
  24. int lastState = HIGH; // to track last button state
  25.  
  26. void setup() {
  27. pinMode(led1, OUTPUT); // define pins as input or output
  28. pinMode(led2, OUTPUT);
  29. pinMode(toggleLed, OUTPUT);
  30. pinMode(pushButton, INPUT);
  31. attachInterrupt(digitalPinToInterrupt(pushButton), pushButton_ISR, CHANGE); // use interrupt pin2
  32. }
  33.  
  34. void pushButton_ISR()
  35. {
  36. buttonPushed = true; // ISR should be as short as possible
  37. }
  38.  
  39. void loop() {
  40. unsigned long currentMillis = millis(); // store the current time
  41.  
  42. if (currentMillis - previousMillis1 >= period1) { // check if 1000ms passed
  43. previousMillis1 = currentMillis; // save the last time you blinked the LED
  44. if (ledState1 == LOW) { // if the LED is off turn it on and vice-versa
  45. ledState1 = HIGH; //change led state for next iteration
  46. } else {
  47. ledState1 = LOW;
  48. }
  49. digitalWrite(led1, ledState1); //set LED with ledState to blink again
  50. }
  51.  
  52. if (currentMillis - previousMillis2 >= period2) { // check if 1000ms passed
  53. previousMillis2 = currentMillis; // save the last time you blinked the LED
  54. if (ledState2 == LOW) { // if the LED is off turn it on and vice-versa
  55. ledState2 = HIGH;
  56. } else {
  57. ledState2 = LOW;
  58. }
  59. digitalWrite(led2, ledState2);//set LED with ledState to blink again
  60. }
  61.  
  62. if (buttonPushed = true) // check if ISR is called
  63. {
  64. if ((currentMillis - debounceMillis) > debouncePeriod && buttonPushed) // generate 20ms debounce delay to avoid multiple presses
  65. {
  66. debounceMillis = currentMillis; // save the last debounce delay time
  67. if (digitalRead(pushButton) == LOW && lastState == HIGH) // change the led after push button is pressed
  68. {
  69. ledChange = ! ledChange;
  70. digitalWrite(toggleLed, ledChange);
  71. lastState = LOW;
  72. }
  73. else if (digitalRead(pushButton) == HIGH && lastState == LOW)
  74. {
  75. lastState = HIGH;
  76. }
  77. buttonPushed = false;
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement