ScienceGeyser

Arduino Timer Sample Code

Aug 17th, 2021
1,932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<LiquidCrystal.h>            //LCD display library
  2. #define ledPin 7
  3. LiquidCrystal lcd(8,9,10,11,12,13);
  4.  
  5. float value = 3035;                   //Preload timer value (3035 for 4 seconds)
  6.  
  7. void setup()
  8. {
  9.   lcd.begin(16,2);
  10.   lcd.setCursor(0,0);
  11.   lcd.print("ARDUINO TIMERS");
  12.   delay(2000);
  13.   lcd.clear();
  14.  
  15.   pinMode(ledPin, OUTPUT);
  16.   pinMode(2,INPUT);
  17.   pinMode(4,INPUT);
  18.  
  19.   noInterrupts();                       // disable all interrupts
  20.  
  21.   TCCR1A = 0;
  22.   TCCR1B = 0;
  23.  
  24.   TCNT1 = value;                        // preload timer
  25.   TCCR1B |= (1 << CS10)|(1 << CS12);    // 1024 prescaler
  26.   TIMSK1 |= (1 << TOIE1);               // enable timer overflow interrupt ISR
  27.   interrupts();                         // enable all interrupts
  28. }
  29.  
  30. ISR(TIMER1_OVF_vect)                    // interrupt service routine for overflow
  31. {
  32.   TCNT1 = value;                                // preload timer
  33.   digitalWrite(ledPin, digitalRead(ledPin) ^ 1);  //Turns LED ON and OFF
  34. }
  35.  
  36. void loop()
  37. {
  38.   if(digitalRead(2) == HIGH)
  39.   {
  40.     value = value+10;             //Incement preload value
  41.   }
  42.   if(digitalRead(4)== HIGH)
  43.   {
  44.     value = value-10;            //Decrement preload value
  45.   }
  46.   lcd.setCursor(0,0);
  47.   lcd.print(value);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment