Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __BUTTONS_H
- #define __BUTTONS_H
- #include <avr/pgmspace.h>
- #include <inttypes.h>
- #include <avr/io.h>
- #include <stdio.h>
- #include "Arduino.h"
- #define BTNU (( uint8_t)13)
- #define BTND (( uint8_t)14)
- #define BTNL (( uint8_t)15)
- #define BTNR (( uint8_t)16)
- #define BTNA (( uint8_t)17)
- #define BTNB (( uint8_t)18)
- #define YES (( uint8_t)1)
- #define NO (( uint8_t)0)
- #define DEBOUNCE 200 //ms/sample
- #define PRESSHOLD 1000
- //******************************************************************************
- //
- // Button Definitions
- //
- //******************************************************************************
- //2-dimensional array for asigning the buttons and there high and low values
- const uint16_t PROGMEM Button[21][3] = {{1, 834, 845}, // button 1
- {2, 712, 721}, // button 2
- {3, 603, 613}, // button 3
- {4, 315, 326}, // button 4
- {5, 173, 185}, // button 5
- {6, 85, 97}, // button 6
- {7, 888, 898}, // button 1 + button 2
- {8, 872, 882}, // button 1 + button 3
- {9, 849, 858}, // button 1 + button 4
- {10, 844, 848}, // button 1 + button 5
- {11, 838, 843}, // button 1 + button 6
- {12, 805, 815}, // button 2 + button 3
- {13, 748, 758}, // button 2 + button 4
- {14, 729, 740}, // button 2 + button 5
- {15, 719, 728}, // button 2 + button 6
- {16, 668, 678}, // button 3 + button 4
- {17, 636, 646}, // button 3 + button 5
- {18, 619, 629}, // button 3 + button 6
- {19, 405, 415}, // button 4 + button 5
- {20, 359, 369}, // button 4 + button 6
- {21, 237, 247}}; // button 5 + button 6
- uint8_t parse_buttons(uint16_t button)
- {
- //given the channel and button, return corresponding actual button
- //hardware specific
- switch(button)
- {
- case 1:
- return BTNU;
- case 2:
- return BTND;
- case 3:
- return BTNL;
- case 4:
- return BTNR;
- case 5:
- return BTNA;
- case 6:
- return BTNB;
- case 12:
- return BTNB;
- default:
- break;
- };
- return 255;
- }
- uint8_t buttonCheck( uint16_t analogpin)
- {
- uint16_t i=0, upper, lower;
- uint8_t button = 0;
- uint16_t analogval;
- analogval = analogRead(analogpin);
- //find what button it corresponds to
- for( uint16_t i = 0; i < 21; i++)
- {
- // checks the _analogval against the high and low vales in the array
- upper = pgm_read_word(&(Button[i][1]));
- lower = pgm_read_word(&(Button[i][2]));
- if(analogval >= upper && analogval <= lower)
- {
- // stores the button number to a variable
- button = pgm_read_word(&(Button[i][0]));
- return button;
- }
- }
- return button;
- }
- uint8_t buttonWait(uint16_t analogpin, uint8_t toggle, uint8_t hold)
- {
- //if toggle set, wait until button comes back to 0 position
- uint16_t init_butt=0,state = 0, offset=0;
- uint32_t time;
- uint8_t buffer[10];
- uint8_t value;
- //consider sleep code here
- while(1){
- while(state == 0)
- {
- for( uint16_t j=0; j<10; j++)
- {
- buffer[j] = buttonCheck(analogpin);
- delay(1);
- }
- value = buffer[0];
- for( uint16_t j=1; j<10;j++)
- {
- if(value != buffer[j])
- {
- //mismatch, discard
- value =255;
- break;
- }
- }
- if((value != 255) && (value != 0))
- {
- state = 2;
- init_butt = value;
- time = millis();
- }
- }
- if(toggle == YES)
- {
- state = 1;
- }
- while(state == 1)
- {
- if(millis() - time > DEBOUNCE)
- {
- return parse_buttons(init_butt);
- }
- }
- while (state == 2)
- {
- //wait for button fall
- for( uint16_t j=0; j<10; j++)
- {
- buffer[j] = buttonCheck(analogpin);
- }
- value = buffer[0];
- for( uint16_t j=1; j<10;j++)
- {
- if(value != buffer[j])
- {
- //mismatch, discard
- value =254;
- break;
- }
- }
- if( hold == YES && millis() - time > PRESSHOLD)
- {
- offset = 10;
- }
- if( value == 0)
- {
- return parse_buttons(offset+init_butt);
- }
- }
- }
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement