Advertisement
dustinrobotics

Buttons.h

Mar 24th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #ifndef __BUTTONS_H
  2. #define __BUTTONS_H
  3.  
  4. #include <avr/pgmspace.h>
  5. #include <inttypes.h>
  6. #include <avr/io.h>
  7. #include <stdio.h>
  8.  
  9. #include "Arduino.h"
  10.  
  11.  
  12. #define BTNU                    (( uint8_t)13)
  13. #define BTND                    (( uint8_t)14)
  14. #define BTNL                    (( uint8_t)15)
  15. #define BTNR                    (( uint8_t)16)
  16. #define BTNA                    (( uint8_t)17)
  17. #define BTNB                    (( uint8_t)18)
  18. #define YES                     (( uint8_t)1)
  19. #define NO                      (( uint8_t)0)
  20. #define DEBOUNCE                200  //ms/sample
  21. #define PRESSHOLD               1000
  22.  
  23. //******************************************************************************
  24. //
  25. // Button Definitions
  26. //
  27. //******************************************************************************
  28. //2-dimensional array for asigning the buttons and there high and low values
  29. const uint16_t PROGMEM Button[21][3]  = {{1, 834, 845}, // button 1
  30.                      {2, 712, 721}, // button 2
  31.                      {3, 603, 613}, // button 3
  32.                      {4, 315, 326}, // button 4
  33.                      {5, 173, 185}, // button 5
  34.                      {6, 85, 97}, // button 6
  35.                      {7, 888, 898}, // button 1 + button 2
  36.                      {8, 872, 882}, // button 1 + button 3
  37.                      {9, 849, 858}, // button 1 + button 4
  38.                      {10, 844, 848}, // button 1 + button 5
  39.                      {11, 838, 843}, // button 1 + button 6
  40.                      {12, 805, 815}, // button 2 + button 3
  41.                      {13, 748, 758}, // button 2 + button 4
  42.                      {14, 729, 740}, // button 2 + button 5
  43.                      {15, 719, 728}, // button 2 + button 6
  44.                      {16, 668, 678}, // button 3 + button 4
  45.                      {17, 636, 646}, // button 3 + button 5
  46.                      {18, 619, 629}, // button 3 + button 6
  47.                      {19, 405, 415}, // button 4 + button 5
  48.                      {20, 359, 369}, // button 4 + button 6
  49.                      {21, 237, 247}}; // button 5 + button 6
  50.  
  51. uint8_t parse_buttons(uint16_t button)
  52. {
  53. //given the channel and button, return corresponding actual button
  54. //hardware specific
  55.   switch(button)
  56.   {
  57.       case 1:
  58.           return BTNU;
  59.       case 2:
  60.           return BTND;
  61.       case 3:
  62.           return BTNL;
  63.       case 4:
  64.           return BTNR;
  65.       case 5:
  66.           return BTNA;
  67.       case 6:
  68.           return BTNB;
  69.       case 12:
  70.           return BTNB;
  71.       default:
  72.           break;
  73.   };
  74.   return 255;
  75. }
  76.  
  77. uint8_t buttonCheck( uint16_t analogpin)
  78. {
  79.     uint16_t i=0, upper, lower;
  80.     uint8_t button = 0;
  81.     uint16_t analogval;
  82.     analogval = analogRead(analogpin);
  83.     //find what button it corresponds to
  84.     for( uint16_t i = 0; i < 21; i++)
  85.     {
  86.         // checks the _analogval against the high and low vales in the array
  87.         upper = pgm_read_word(&(Button[i][1]));
  88.         lower = pgm_read_word(&(Button[i][2]));
  89.        
  90.         if(analogval >= upper && analogval <= lower)
  91.         {
  92.             // stores the button number to a variable
  93.             button = pgm_read_word(&(Button[i][0]));
  94.             return button;    
  95.         }
  96.     }
  97.     return button;
  98. }
  99.  
  100. uint8_t buttonWait(uint16_t analogpin, uint8_t toggle, uint8_t hold)
  101. {  
  102.     //if toggle set, wait until button comes back to 0 position
  103.     uint16_t init_butt=0,state = 0, offset=0;
  104.     uint32_t time;
  105.     uint8_t buffer[10];
  106.     uint8_t value;
  107.     //consider sleep code here
  108.  
  109.     while(1){
  110.         while(state == 0)
  111.         {
  112.           for( uint16_t j=0; j<10; j++)
  113.           {
  114.               buffer[j] = buttonCheck(analogpin);
  115.               delay(1);
  116.              
  117.           }
  118.           value = buffer[0];
  119.           for( uint16_t j=1; j<10;j++)
  120.           {
  121.               if(value != buffer[j])
  122.               {
  123.                   //mismatch, discard
  124.                   value =255;
  125.                   break;
  126.               }
  127.           }
  128.           if((value != 255) && (value != 0))
  129.           {
  130.               state = 2;
  131.               init_butt = value;
  132.               time = millis();
  133.           }
  134.         }
  135.         if(toggle == YES)
  136.         {
  137.             state = 1;  
  138.         }
  139.         while(state == 1)
  140.         {
  141.             if(millis() - time > DEBOUNCE)
  142.             {
  143.                 return parse_buttons(init_butt);
  144.             }
  145.         }
  146.         while (state == 2)
  147.         {
  148.             //wait for button fall
  149.             for( uint16_t j=0; j<10; j++)
  150.             {
  151.                 buffer[j] = buttonCheck(analogpin);
  152.             }
  153.             value = buffer[0];
  154.             for( uint16_t j=1; j<10;j++)
  155.             {
  156.                 if(value != buffer[j])
  157.                 {
  158.                     //mismatch, discard
  159.                     value =254;
  160.                     break;
  161.                 }
  162.             }
  163.             if( hold == YES && millis() - time > PRESSHOLD)
  164.             {
  165.               offset = 10;
  166.             }
  167.             if( value == 0)
  168.             {
  169.                 return parse_buttons(offset+init_butt);
  170.             }
  171.         }
  172.     }
  173.     return 0;        
  174. }
  175.  
  176.  
  177. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement