Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #define ROT_A PD2
- #define ROT_A_MASK _BV(ROT_A)
- #define ROT_B PD3
- #define ROT_B_MASK _BV(ROT_B)
- #define VOL_DOWN PB1
- #define VOL_DOWN_MASK _BV(VOL_DOWN)
- #define VOL_UP PB2
- #define VOL_UP_MASK _BV(VOL_UP)
- static boolean r = false;
- char a, d = 0;
- volatile unsigned long l = 0;
- unsigned long p = 0;
- unsigned char s = 0;
- void vol_set(char direction) {
- if (d != direction){
- s = 0;
- }
- if (s > 6){
- if (direction < 0) {
- Serial.println("VOLUME DOWN");
- } else {
- Serial.println("VOLUME UP");
- }
- s = 0;
- }
- d = direction;
- s++;
- }
- ISR(INT0_vect) {
- r = true;
- l = millis();
- }
- void setup() {
- DDRD |= (!ROT_A_MASK | !ROT_B_MASK);
- DDRB |= (VOL_DOWN_MASK | VOL_DOWN_MASK);
- PORTD |= (ROT_A_MASK | ROT_B_MASK);
- EICRA |= _BV(ISC00);
- EIMSK |= _BV(INT0);
- sei();
- Serial.begin(9600);
- }
- void loop() {
- unsigned long c = millis();
- if((c-l > 1200) && s !=0){
- s = 0;
- }
- while (r) {
- uint8_t x = 0;
- x = (bit_is_clear(PIND, ROT_A) << 1) | bit_is_clear(PIND, ROT_B);
- if (x == 0 && (a == 2 || a == 3)) {
- vol_set(-1);
- } else if (x == 1 && (a == 2 || a == 3)) {
- vol_set(1);
- } else if (x == 2 && (a == 0 || a == 1)) {
- vol_set(1);
- } else if (x == 3 && (a == 0 || a == 1)) {
- vol_set(-1);
- } else {
- //Serial.println("debounced");
- }
- a = x;
- r = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement