Moortiii

Untitled

Mar 5th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. volatile int resistorValue = 10;
  2. volatile int timerValue = 0;
  3. volatile byte STATE = HIGH;
  4.  
  5. void setup()
  6. {
  7. Serial.begin(9600);
  8. pinMode(A0, INPUT);
  9. pinMode(9, OUTPUT);
  10.  
  11. // Enable internal pullup on INT0 / PIN2
  12. pinMode(2, INPUT_PULLUP);
  13. delay(1);
  14.  
  15. // Disable all interrupts
  16. noInterrupts();
  17.  
  18. // Configure timer
  19. TCCR1A = 0; // Turn off `Compare Output Mode`
  20. TCCR1B = 0; // Turn off `Timer`, Reset Clock
  21. TCNT1 = 0; // Reset `Clock Count`
  22.  
  23. // Set the prescaler to 1024
  24. TCCR1B |= (1 << CS12) | (1 << CS10);
  25.  
  26. // Set compare value (16 000 000 / 1 024)
  27. OCR1A = 15625 / 1000;
  28.  
  29. // Configure interrupt to happen on falling edge
  30. EICRA |= (1 << ISC01);
  31.  
  32. // Enable external interrupt 0 (pin 2);
  33. EIMSK |= (1 << INT0);
  34.  
  35. // CTC = Clear Timer On Compare
  36. TCCR1B |= (1 << WGM12);
  37.  
  38. // Turn on `Output Compare Interrupt`
  39. TIMSK1 |= (1 << OCIE1A);
  40.  
  41. // Enable interrupts
  42. interrupts();
  43. }
  44.  
  45. void loop()
  46. {
  47. delay(100);
  48. // Here goes nothing
  49. }
  50.  
  51. ISR(TIMER1_COMPA_vect) {
  52. timerValue++;
  53.  
  54. if(timerValue >= resistorValue) {
  55. STATE = !STATE;
  56. digitalWrite(9, STATE);
  57. timerValue = 0;
  58. }
  59. }
  60.  
  61. ISR(INT0_vect) {
  62. resistorValue = analogRead(A0);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment