Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include "Arduino.h"
  2. #define ROT_A PD2
  3. #define ROT_A_MASK _BV(ROT_A)
  4. #define ROT_B PD3
  5. #define ROT_B_MASK _BV(ROT_B)
  6.  
  7. #define VOL_DOWN PB1
  8. #define VOL_DOWN_MASK _BV(VOL_DOWN)
  9. #define VOL_UP PB2
  10. #define VOL_UP_MASK _BV(VOL_UP)
  11.  
  12. static boolean r          = false;
  13. char a, d                 = 0;
  14. volatile unsigned long l  = 0;
  15. unsigned long p           = 0;
  16. unsigned char s           = 0;
  17.  
  18. void vol_set(char direction) {
  19.   if (d != direction){
  20.     s = 0;
  21.   }
  22.   if (s > 6){
  23.     if (direction < 0) {
  24.       Serial.println("VOLUME DOWN");
  25.     } else {
  26.       Serial.println("VOLUME UP");
  27.     }
  28.     s = 0;
  29.   }
  30.   d = direction;
  31.   s++;
  32. }
  33.  
  34. ISR(INT0_vect) {
  35.   r = true;
  36.   l = millis();
  37. }
  38.  
  39. void setup() {
  40.   DDRD |= (!ROT_A_MASK | !ROT_B_MASK);
  41.   DDRB |= (VOL_DOWN_MASK | VOL_DOWN_MASK);
  42.   PORTD |= (ROT_A_MASK | ROT_B_MASK);
  43.   EICRA |= _BV(ISC00);
  44.   EIMSK |= _BV(INT0);
  45.   sei();
  46.   Serial.begin(9600);
  47. }
  48. void loop() {
  49.   unsigned long c = millis();
  50.  
  51.   if((c-l > 1200) && s !=0){
  52.     s = 0;
  53.   }
  54.  
  55.   while (r) {
  56.     uint8_t x = 0;
  57.  
  58.     x = (bit_is_clear(PIND, ROT_A) << 1) | bit_is_clear(PIND, ROT_B);
  59.  
  60.     if (x == 0 && (a == 2 || a == 3)) {
  61.       vol_set(-1);
  62.     } else if (x == 1 && (a == 2 || a == 3)) {
  63.       vol_set(1);
  64.     } else if (x == 2 && (a == 0 || a == 1)) {
  65.       vol_set(1);
  66.     } else if (x == 3 && (a == 0 || a == 1)) {
  67.       vol_set(-1);
  68.     } else {
  69.       //Serial.println("debounced");
  70.     }
  71.     a = x;
  72.     r = false;
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement