Advertisement
dustinrobotics

Controller Arduino Firmware

Mar 29th, 2012
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 38.01 KB | None | 0 0
  1. /* Controller 2.0
  2. Created by Dustin Reynolds at Mecharobotics.wordpress.com
  3.  
  4. You may share this library with Creative Commons - Share Alike
  5. license 2.0.  
  6.  
  7. This is released via the Creative Commons Share Alike 2.0 license:
  8. http://creativecommons.org/licenses/by-sa/2.0/  
  9.  
  10. You can copy it and use it in whatever, just attribute my name,
  11. Dustin Reynolds, and my blog when you do.  
  12. I'll appreciate if you let me know that you've used it,
  13. so I can show off your project on my blog :)
  14.  
  15. Copyright 2012 Dustin Reynolds
  16. *****************************************************************************
  17.   Includes the following:
  18.   20x4 LCD
  19.   XBee Connected to pins 0 and 1 of ATMEGA328
  20.   BTM-182 connected to pins 7 and 7
  21.   PS2 connector
  22.   Nunchuck connector
  23.   microSD adapter (using old microSD to SD converter)
  24.  
  25.   Size comparisons:
  26. with SD enabled
  27.   Everything Enabled: 29024 Bytes
  28.   Only LCD debug: 27252 Bytes
  29.   Only NUN LCD debug, PS2 disabled: 22550
  30.   Only PS2 LCD debug, NUN disabled: 25900
  31.   Only PS2 & Nun, no debug: 24690
  32.   Only PS2, no debug: 24484
  33.   No ps2 or Nun: 20972
  34. With SD disabled
  35.   Nun and PS2 disabled:13488
  36. BT disabled + SD + NUN PS2
  37.   Xbee: 11910
  38. Everything Disabled:
  39.   11886 bytes
  40.  
  41. Recommended Flags enabled:
  42.   SD_enable, NUN_Enable, NUN_DBG_LCD
  43.     -ps2 library takes about 5k of space, nun takes 3.6k
  44.     -num can do plenty, can improvize with buttons + joystick
  45.    
  46.    
  47. Circuit:  Description of the circuit Textually:
  48.   3.3v VReg (MCP1702-3302E)
  49.     1-GND
  50.     2- + of battery, 0.1u to gnd*
  51.     3-3.3v, 0.1u*
  52.     *-1uF across 3.3V and gnd, 0.1 accross 3.3v and gnd
  53.   ATMEGA328
  54.     1-connected to Pin 6 of FTDI cable
  55.     2-Connected to pin 4 of FTDI Cable, To pin 2 of xbee (through a 1k resistor)
  56.     3-Connected to pin 5 of FTDI cable, to pin 3 of xbee (through a 1k resistor)
  57.             http://www.billporter.info/how-to-add-multiple-uart-connections/
  58.     4-NC
  59.     5-Pin 11 of shift register
  60.     6-Pin 14 of shift register
  61.     7-3.3V
  62.     8-gnd
  63.     9-left lead of 16MHz resonator (center to gnd)
  64.     10-right lead of 16MHz resonator(center to gnd)
  65.     11-Pin 12 of Shift register, 1uf cap to ground
  66.     12-Pin 9 of XBee
  67.     13-Pin 9 of BTM-182 BT serial module
  68.     14-Pin 10 of BTM-182 BT serial module  
  69.     15-Pin 6 of PS2 controller
  70.     16-Pin 1 of SD adapter
  71.     17-pin 2 of PS2 controller, Pin 2 of SD card
  72.     18-Pin 1 of PS2 controller, Pin 7 of SD card
  73.     19-Pin 7 of PS2 controller, pin 5 of SD card
  74.     20-3.3V
  75.     21-3.3V
  76.     22-GND
  77.     23-1k to gnd, one end of button, other is connected to 430ohm, connected to 3.3v
  78.     24-
  79.     25-
  80.     26-
  81.     27-2.2k to 3.3v, pin 1 of nunchuck(datapin)
  82.     28-2.2k to 3.3v, pin 2 of nunchuck(clk)
  83.   SD card (Notch to Left, not counting end connections)
  84.     1-pin 16 of cpu
  85.     2-pin 17 of cpu
  86.     3-gnd
  87.     4-3.3v
  88.     5-pin 19 of cpu
  89.     6-gnd
  90.     7-pin 18 of cpu
  91.   XBee Series 1 Module
  92.      X-CTU:http://www.digi.com/support/productdetail?pid=3352&osvid=0&type=utilities
  93.      Wireless dl:http://www.sparkfun.com/tutorials/122
  94.     1-3.3v
  95.     2-1k connected to pin 2 of mcu
  96.     3-1k connected to pin 3 of mcu
  97.     9-connected to pin 12 of mcu
  98.     10-gnd
  99.   BTM-182 BT serial module
  100.      http://www.sparkfun.com/products/9913
  101.      AT commands:http://www.robot-r-us.com/downloads/sparkfun/wigwrl/812-wrl09977-lm400-data-sheet-dss-ver-1-2/download.html
  102.     9-pin 13 of mcu
  103.     10-pin 14 of mcu
  104.     15-3.3v
  105.     16-gnd
  106.   Shift Register 74HC595
  107.      http://code.google.com/p/arduinoshiftreglcd/
  108.     1-nc
  109.     2-pin 4 of lcd
  110.     3-pin 11 of lcd
  111.     4-pin 12 of lcd
  112.     5-pin 13 of lcd
  113.     6-pin 14 of lcd
  114.     7-nc
  115.     8-gnd
  116.     9-nc
  117.     10-3.3v
  118.     11-pin 5 of mcu
  119.     12-pin 11 of mcu,0.1uF to gnd,pin 6 of lcd
  120.     13-gnd
  121.     14-pin 6 of mcu
  122.     15-nc
  123.     16-3.3v
  124.   LCD 3.3V NewHaven 20x4 character LCD
  125.     1-gnd
  126.     2-3.3V
  127.     3-middle pin of 10k pot, other pins at 3.3v and gnd
  128.     4-pin 2 of 74Hc595 shift reg
  129.     5-gnd
  130.     6-pin 12 of shift reg
  131.     7,8,9,10-nc
  132.     11-pin 3 of shift
  133.     12-pin 4 of shift
  134.     13-pin 5 of shift
  135.     14-pin 6 of shift
  136.     15-nc(can be conn to 3.3v for backlight
  137.     16-gnd
  138.   PS2 (round end down)
  139.      http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/
  140.     1-pin 18 of mcu
  141.     2-pin 17 of mcu
  142.     3-nc(for motors)
  143.     4-gnd
  144.     5-3.3v
  145.     6-pin 15 of mcu
  146.     7-pin 19 of mcu
  147.   Nunchuck - dealextreme knockoff < $7
  148.      (nunchuck conn facing you, looks like a U not a n)
  149.      http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273496638
  150.     1(upper left)-pin 27 of mcu, 2.2k pullup to 3.3v (clock)
  151.     2(upper right)-gnd
  152.     3(lower left)-3.3v
  153.     4(lower right)-pin 28 of mcu, 2.2k pullup to 3.3v (data)
  154.   Xbee is run using HW seria, BT using sw serial
  155.   PS2 run on SPI bus, SD on SPI bus
  156.   Nun run on I2C bus
  157.   LCD only uses 3 pins using shift reg
  158.   entire system at 3.3v
  159.  
  160. Notes:
  161.   How do I create a menu?
  162.     1. Add your menu in section 1. Edit the main menu to have your desired name in it.
  163.        Be sure to do yournameSUB and yournameLEN correctly, follow the examples.  Bakmain is
  164.        still counted, since this references the table in sec 4.
  165.     2. Add a yournameCONF to the list of confs, create a unique #define for each menu item.
  166.     3. Add a #define for your menu, set it to be the same as yournameSUB.  These are used in a
  167.        big switch statement in the select function
  168.     4. Add your prog_char's defined in step 1, to this table, in the correct space.
  169.     5. Goto select function, create a if statement in case: MAIN.  Create a case for your menu
  170.        name defined in step 3.
  171.   Dustin Reynolds
  172.   2012-03-24
  173. */
  174. #include <Wire.h>
  175. #include <Nunchuck.h>
  176. #include <PS2X_lib.h>  //for v1.6
  177. #include <SoftwareSerial.h>
  178. #include <ShiftRegLCD.h>
  179. #include <SD.h>
  180. #include <avr/pgmspace.h>
  181. #include <EEPROM.h>
  182.  
  183. //Comment out these to disable specific features.  Won't disable menus, since that is complicated
  184. //#define PS2_ENABLE
  185.   //#define PS2_DBG_SER
  186.   //#define PS2_DBG_LCD
  187. //#define NUN_ENABLE
  188.   //#define NUN_DBG_SER
  189.   //#define NUN_DBG_LCD
  190.  
  191. #define XBEE_ENABLE
  192. #define BT_ENABLE
  193.  
  194. //#define SD_ENABLE
  195.   #define FILENAME_OFFSET 20 //single byte stored in EEPROM
  196. #define HMENU 4
  197. #define WMENU 20
  198.  
  199. File logFile;
  200. #define SDCHIPSEL 10
  201. PS2X ps2x; // create PS2 Controller Class
  202. //#define DEBUG 1
  203.  
  204. ShiftRegLCD lcd(4, 3, 5, 4);  //we have a 20x4 LCD data clock enable #lines
  205. #ifdef BT_ENABLE
  206. SoftwareSerial btser(7, 8);
  207. #endif
  208. //want simple XBEE config
  209. #define XBEESLEEP 6  //Pin 12 for XBee sleep control, goes to pin 9 on xbee
  210.  
  211. #define BTNU                    (( uint8_t)13)
  212. #define BTND                    (( uint8_t)14)
  213. #define BTNL                    (( uint8_t)15)
  214. #define BTNR                    (( uint8_t)16)
  215. #define BTNA                    (( uint8_t)17)
  216. #define BTNB                    (( uint8_t)18)
  217. #define YES                     (( uint8_t)1)
  218. #define NO                      (( uint8_t)0)
  219. #define DEBOUNCE                200  //ms/sample
  220. #define PRESSHOLD               1000
  221.  
  222. #define UPARROW                 ((uint8_t)3)
  223. #define DOWNARROW               ((uint8_t)4)
  224. #define SELECT                  ((uint8_t)5)
  225. #define HEART                   ((uint8_t)0)
  226. #define SMILE                   ((uint8_t)2)
  227. #define KEY                     ((uint8_t)1)
  228. /***********************************SECTION 2 *****************************************/
  229. #define XBEECONF 0
  230. #define BTCONF   1
  231. #define NUNCONF  2
  232. #define PS2CONF  3
  233. #define SDCONF   4
  234. #define CONNCPU  5
  235. #define CONNGAR  6
  236. #define CONNSAB  7
  237.  
  238. #define BACKMAIN  0
  239. #define XBEEPPP   1
  240. #define XBEESM1   2
  241. #define XBEECLOSE 3
  242.  
  243. #define BTPPP   1
  244. #define BTATR0  2
  245. #define BTATH   3
  246. #define BTATF   4
  247. #define BTATD   5
  248. #define BTATA   6
  249. #define BTCLOSE 7
  250.  
  251. #define NUNPRES 1
  252. #define NUNINIT 2
  253. #define NUNCAL  3
  254. #define NUNDLCD 4
  255. #define NUNDSER 5
  256.  
  257. #define PS2PRES 1
  258. #define PS2INIT 2
  259. #define PS2DLCD 3
  260. #define PS2DSER 4
  261.  
  262. #define SDPRES 1
  263. #define SDCNGI 2
  264. #define SDPREV 3
  265. /*****************************END OF SECTION 2 *****************************************/
  266. /***********************************SECTION 1 *****************************************/
  267. #define MAINSUB 0
  268. #define MAINLEN 7 //actual - 1
  269. const prog_char PROGMEM MenuStr1[] = "XBee Configure";
  270. const prog_char PROGMEM MenuStr2[] = "BT Configure";
  271. const prog_char PROGMEM MenuStr3[] = "NunChuck Configure";
  272. const prog_char PROGMEM MenuStr4[] = "PS2 Configure";
  273. const prog_char PROGMEM MenuStr5[] = "SD Configure";
  274. const prog_char PROGMEM MenuStr6[] = "Connect to CPU";
  275. const prog_char PROGMEM MenuStr7[] = "Connect to Garden";
  276. const prog_char PROGMEM MenuStr8[] = "Connect to Sable";
  277. #define XBEESUB 8 //actual - 1
  278.  
  279. #define XBEELEN 3  //actual - 1
  280. const prog_char PROGMEM Bakmain[] = "Back to Main";
  281. const prog_char PROGMEM Xbee2[] = "+++";
  282. const prog_char PROGMEM Xbee3[] = "SM1,ST3e8,SO1";
  283. const prog_char PROGMEM Xbee4[] = "WN,CN";
  284. #define BTSUB 12
  285.  
  286. #define BTLEN 7 //actual - 1
  287. //const prog_char PROGMEM Bakmain[] = "Back to Main";
  288. const prog_char PROGMEM BTser2[] = "+++ <-not alneeded";
  289. const prog_char PROGMEM BTser3[] = "ATR0";
  290. const prog_char PROGMEM BTser4[] = "ATH (drops conn)";
  291. const prog_char PROGMEM BTser5[] = "ATF?";
  292. const prog_char PROGMEM BTser6[] = "ATD";
  293. const prog_char PROGMEM BTser7[] = "ATA";
  294. const prog_char PROGMEM BTser8[] = "ATO closes conn";
  295. #define NUNSUB 20
  296.  
  297. #define NUNLEN 5 //actual - 1
  298. //const prog_char PROGMEM Bakmain[] = "Back to Main";   <-count it, but only use one
  299. const prog_char PROGMEM NUN2[] = "Nunchuk Present?";
  300. const prog_char PROGMEM NUN3[] = "Init";
  301. const prog_char PROGMEM NUN4[] = "SetCal";
  302. const prog_char PROGMEM NUN5[] = "Disp LCD";
  303. const prog_char PROGMEM NUN6[] = "Disp Serial";
  304. #define PS2SUB 26
  305.  
  306. #define PS2LEN 4 //actual - 1
  307. //const prog_char PROGMEM Bakmain[] = "Back to Main";   <-count it, but only use one
  308. const prog_char PROGMEM PS22[] = "PS2 Ctl Present?";
  309. //const prog_char PROGMEM NUN3[] = "Init";
  310. const prog_char PROGMEM PS24[] = "Disp LCD";
  311. const prog_char PROGMEM PS25[] = "Disp Serial";
  312. #define SDSUB 31
  313.  
  314. #define SDLEN 3 //actual - 1
  315. //const prog_char PROGMEM Bakmain[] = "Back to Main";   <-count it, but only use one
  316. const prog_char PROGMEM uSD2[] = "SD Present?";
  317. const prog_char PROGMEM uSD3[] = "Change File Index";
  318. const prog_char PROGMEM uSD4[] = "Preview Top File";
  319. /*****************************END OF SECTION 1 *****************************************/
  320. /***********************************SECTION 3 *****************************************/
  321. //currentmenu
  322. #define MAIN   MAINSUB
  323. #define XBEE   XBEESUB
  324. #define BT     BTSUB
  325. #define NUN    NUNSUB
  326. #define PS2    PS2SUB
  327. #define uSD    SDSUB
  328. /*****************************END OF SECTION 3 *****************************************/
  329. /***********************************SECTION 4 *****************************************/
  330. PGM_P PROGMEM MenuTable[] = {
  331.   MenuStr1,
  332.   MenuStr2,
  333.   MenuStr3,
  334.   MenuStr4,
  335.   MenuStr5,
  336.   MenuStr6,
  337.   MenuStr7,
  338.   MenuStr8,
  339.   Bakmain,
  340.   Xbee2,
  341.   Xbee3,
  342.   Xbee4,
  343.   Bakmain,
  344.   BTser2,
  345.   BTser3,
  346.   BTser4,
  347.   BTser5,
  348.   BTser6,
  349.   BTser7,
  350.   BTser8,
  351.   Bakmain,
  352.   NUN2,
  353.   NUN3,
  354.   NUN4,
  355.   NUN5,
  356.   NUN6,
  357.   Bakmain,
  358.   PS22,
  359.   NUN3,
  360.   PS24,
  361.   PS25,
  362.   Bakmain,
  363.   uSD2,
  364.   uSD3,
  365.   uSD4,
  366. };
  367. /*****************************END OF SECTION 4 *****************************************/
  368. const prog_char PROGMEM ppplus[] = "+++";
  369. const prog_char PROGMEM sm1[] = "ATSM1,ST3E8,SO1";
  370. const prog_char PROGMEM wncn[] = "ATWN,CN";
  371. const prog_char PROGMEM atr0[] = "ATR0";
  372. const prog_char PROGMEM ath[] = "ATH";
  373. const prog_char PROGMEM atf[] = "ATF?";
  374. const prog_char PROGMEM atd[] = "ATD";
  375. const prog_char PROGMEM ata[] = "ATA";
  376. const prog_char PROGMEM ato[] = "ATO";
  377. const prog_char PROGMEM Timeout[] = "TimeOut";
  378. const prog_char PROGMEM Exit[] = "Exit";
  379.  
  380. //******************************************************************************
  381. //
  382. // Button Definitions
  383. //
  384. //******************************************************************************
  385. //2-dimensional array for asigning the buttons and there high and low values
  386. const uint16_t PROGMEM Button[21][3]  = {{1, 834, 845}, // button 1
  387.                      {2, 712, 721}, // button 2
  388.                      {3, 603, 613}, // button 3
  389.                      {4, 315, 326}, // button 4
  390.                      {5, 173, 185}, // button 5
  391.                      {6, 85, 97}, // button 6
  392.                      {7, 888, 898}, // button 1 + button 2
  393.                      {8, 872, 882}, // button 1 + button 3
  394.                      {9, 849, 858}, // button 1 + button 4
  395.                      {10, 844, 848}, // button 1 + button 5
  396.                      {11, 838, 843}, // button 1 + button 6
  397.                      {12, 805, 815}, // button 2 + button 3
  398.                      {13, 748, 758}, // button 2 + button 4
  399.                      {14, 729, 740}, // button 2 + button 5
  400.                      {15, 719, 728}, // button 2 + button 6
  401.                      {16, 668, 678}, // button 3 + button 4
  402.                      {17, 636, 646}, // button 3 + button 5
  403.                      {18, 619, 629}, // button 3 + button 6
  404.                      {19, 405, 415}, // button 4 + button 5
  405.                      {20, 359, 369}, // button 4 + button 6
  406.                      {21, 237, 247}}; // button 5 + button 6
  407.  
  408. byte Select[8]={  B01000,  B01100,  B00110,  B11111,  B11111,  B00110,  B01100,  B01000};
  409.  
  410. char menu = 0; //0 = main
  411. char menulen = MAINLEN;
  412. char menusel = 0;  //cursor
  413. char index[40];
  414. char currmenu = MAIN;  //current menu
  415. byte fileNum = 0;
  416. char file[20];
  417. int setup_ser(char ser, char * string, char init, char initcr,char justppp, unsigned long timeout, uint16_t analogpin);
  418. uint8_t buttonWait(uint16_t analogpin, uint8_t toggle, uint8_t hold);
  419. uint8_t buttonCheck( uint16_t analogpin);
  420.  
  421. void setup()
  422. {
  423.   //xbee
  424.   Serial.begin(19200);
  425.   pinMode(XBEESLEEP,OUTPUT); //6
  426.   digitalWrite(XBEESLEEP, HIGH);
  427. #ifdef BT_ENABLE
  428.   //bt
  429.   pinMode (7,INPUT);
  430.   pinMode (8,OUTPUT);
  431.   btser.begin(9600);
  432. #endif
  433. #ifdef SD_ENABLE
  434.   pinMode(10, OUTPUT);
  435. #endif
  436.   lcd.createChar(5, Select);
  437.  
  438.   for(int i=0; i<40; i++)
  439.     index[i] = i;
  440.   strncpy(file,"File",5);
  441.  
  442.   fileNum = EEPROM.read(FILENAME_OFFSET);
  443. }
  444. //good for numbers < 100
  445. void convert_string(char * string, byte n)
  446. {
  447.   if(n>=10)
  448.   {
  449.     convert_string(string++, n/10);
  450.     n = n%10;
  451.   }
  452.   *string=n+'0';
  453. }
  454.  
  455. //handle things here, not in loop
  456. void select(uint8_t cmenu, uint8_t menus)
  457. {
  458.   int error = 0;
  459.   switch(cmenu)
  460.   {
  461.     case MAIN:
  462.       if(menus == XBEECONF)
  463.       {
  464.         menu=XBEESUB;
  465.         menulen=XBEELEN;
  466.         currmenu = XBEE;
  467.         digitalWrite(XBEESLEEP, LOW);
  468.         menusel = 0;
  469.       }
  470.       else if(menus == BTCONF)
  471.       {
  472.         menu=BTSUB;
  473.         menulen=BTLEN;
  474.         currmenu = BT;
  475.         menusel = 0;
  476.       }
  477.       else if(menus == NUNCONF)
  478.       {
  479.         menu=NUNSUB;
  480.         menulen=NUNLEN;
  481.         currmenu = NUN;
  482.         menusel = 0;
  483.       }
  484.       else if(menus == PS2CONF)
  485.       {
  486.         menu=PS2SUB;
  487.         menulen=PS2LEN;
  488.         currmenu = PS2;
  489.         menusel = 0;
  490.       }
  491.       else if(menus == SDCONF)
  492.       {
  493.         menu=SDSUB;
  494.         menulen=SDLEN;
  495.         currmenu = uSD;
  496.         menusel = 0;
  497.       }
  498.       else
  499.         menu = MAINSUB;
  500.       break;
  501.     case XBEE:
  502.       if(menus == BACKMAIN)
  503.       {
  504.         menu=MAINSUB; ;
  505.         menulen=MAINLEN;
  506.         currmenu = MAIN;
  507.         digitalWrite(XBEESLEEP, HIGH);
  508.         menusel = 0;
  509.       }
  510. #ifdef XBEE_ENABLE
  511.       else if (menus == XBEEPPP)
  512.       {
  513.         //do code here, or move to other function?
  514.         lcd.clear();
  515.         setup_ser(XBEE,wncn,YES,NO,NO,10000,A0);
  516.         //delay(2000);
  517.       }
  518.       else if (menus == XBEECLOSE)
  519.       {
  520.         //do code here, or move to other function?
  521.         lcd.clear();
  522.         setup_ser(XBEE,wncn,YES,NO,NO,10000,A0);
  523.         //delay(2000);
  524.       }
  525.       else
  526.         menu = XBEESUB;
  527. #endif
  528.       break;
  529.     case BT:
  530.       if(menus == BACKMAIN)
  531.       {
  532.         menu=MAINSUB;
  533.         menulen=MAINLEN;
  534.         currmenu = MAIN;
  535.         menusel = 0;
  536.       }
  537. #ifdef BT_ENABLE
  538.       else if (menus == BTPPP)
  539.       {
  540.         //do code here, or move to other function?
  541.         lcd.clear();
  542.         setup_ser(BT,ppplus,YES,YES,YES,10000,A0);
  543.         //delay(2000);
  544.       }
  545.       else if (menus == BTATR0)
  546.       {
  547.         //do code here, or move to other function?
  548.         lcd.clear();
  549.         setup_ser(BT,atr0,NO,NO,NO,10000,A0);
  550.         //delay(2000);
  551.       }
  552.       else if (menus == BTATH)
  553.       {
  554.         //do code here, or move to other function?
  555.         lcd.clear();
  556.         setup_ser(BT,ath,NO,NO,NO,10000,A0);
  557.         //delay(2000);
  558.       }
  559.       else if (menus == BTATF)
  560.       {
  561.         //do code here, or move to other function?
  562.         lcd.clear();
  563.         setup_ser(BT,atf,NO,NO,NO,10000,A0);
  564.         //delay(2000);
  565.       }
  566.       else if (menus == BTATD)
  567.       {
  568.         //do code here, or move to other function?
  569.         lcd.clear();
  570.         setup_ser(BT,atd,NO,NO,NO,10000,A0);
  571.         //delay(2000);
  572.       }
  573.       else if (menus == BTATA)
  574.       {
  575.         //do code here, or move to other function?
  576.         lcd.clear();
  577.         setup_ser(BT,ata,NO,NO,NO,10000,A0);
  578.         //delay(2000);
  579.       }
  580.       else if (menus == BTCLOSE)
  581.       {
  582.         //do code here, or move to other function?
  583.         lcd.clear();
  584.         setup_ser(BT,ato,NO,NO,NO,10000,A0);
  585.         //delay(2000);
  586.       }
  587.       else
  588.         menu = BTSUB;
  589. #endif
  590.       break;
  591.     case NUN:
  592.       if(menus == BACKMAIN)
  593.       {
  594.         menu=MAINSUB;
  595.         menulen=MAINLEN;
  596.         currmenu = MAIN;
  597.         menusel = 0;
  598.       }
  599. #ifdef NUN_ENABLE
  600.       else if (menus == NUNPRES)
  601.       {
  602.         lcd.clear();
  603.         Wire.begin();
  604.         Wire.beginTransmission(0x52);
  605.         if( Wire.endTransmission() == 0)
  606.           lcd.print(F("Nunchuck Found"));
  607.         else
  608.           lcd.print((__FlashStringHelper *)Timeout);
  609.         delay(1000);
  610.       }
  611.       else if (menus == NUNINIT)
  612.       {
  613.         lcd.print("init starting");
  614.         nunchuk_init();
  615.         lcd.clear();
  616.         nunchuk_send_request();
  617.         nunchuk_get_data();
  618.       }
  619.       else if (menus == NUNDLCD || menus == NUNDSER || menus == NUNCAL)
  620.       {
  621.         lcd.clear();
  622.         digitalWrite(XBEESLEEP, LOW);
  623.         while(1)
  624.         {
  625.         nunchuk_send_request();
  626.         nunchuk_get_data();
  627.  
  628. #ifdef NUN_DBG_SER
  629.         //lcd.clear();
  630.         if( menus == NUNDSER)
  631.         {
  632.           Serial.print("Z ");
  633.           Serial.print((unsigned int)nunchuk_zbutton());
  634.           Serial.print("\tC ");
  635.           Serial.print((unsigned int)nunchuk_cbutton());
  636.           //rest of data
  637.           Serial.print("\tJx ");
  638.           Serial.print((unsigned int)nunchuk_joy_x());
  639.           Serial.print("\tJy ");
  640.           Serial.print((unsigned int)nunchuk_joy_y());
  641.  
  642.           Serial.print("\tcJx ");
  643.           Serial.print((signed int)nunchuk_cjoy_x());
  644.           Serial.print("\tcJy ");
  645.           Serial.print((signed int)nunchuk_cjoy_y());
  646.          
  647.           //accel
  648.           Serial.print("\tAcX ");
  649.           Serial.print((unsigned int)nunchuk_accelx());
  650.           Serial.print("\tAcY ");
  651.           Serial.print((unsigned int)nunchuk_accely());
  652.           Serial.print("\tAcZ ");
  653.           Serial.print((unsigned int)nunchuk_accelz());
  654.          
  655.           Serial.print("\tcAcX ");
  656.           Serial.print((signed int)nunchuk_caccelx());
  657.           Serial.print("\tcAcY ");
  658.           Serial.print((signed int)nunchuk_caccely());
  659.           Serial.print("\tcAcZ ");
  660.           Serial.println((signed int)nunchuk_caccelz());
  661.           delay(250);
  662.         }
  663. #endif
  664.         if( menus == NUNDLCD || menus == NUNCAL)
  665.         {
  666. #ifdef NUN_DBG_LCD
  667.           lcd.home();
  668.           lcd.print("Z ");
  669.           lcd.print((unsigned int)nunchuk_zbutton());
  670.           lcd.print("C ");
  671.           lcd.print((unsigned int)nunchuk_cbutton());
  672.           //rest of data
  673.           lcd.print("Jx   ");
  674.           lcd.setCursor(8,0);
  675.           lcd.print((unsigned int)nunchuk_joy_x());
  676.           lcd.setCursor(11,0);
  677.           lcd.print("Jy   ");
  678.           lcd.setCursor(13,0);
  679.           lcd.print((unsigned int)nunchuk_joy_y());
  680.          
  681.           //accel - non cal
  682.           lcd.setCursor(0,1);
  683.           lcd.print("aX    ");
  684.           lcd.setCursor(3,1);
  685.           lcd.print((unsigned int)nunchuk_accelx());
  686.           lcd.setCursor(6,1);
  687.           lcd.print("aY    ");
  688.           lcd.setCursor(9,1);
  689.           lcd.print((unsigned int)nunchuk_accely());
  690.           lcd.setCursor(12,1);
  691.           lcd.print("aZ    ");
  692.           lcd.setCursor(15,1);
  693.           lcd.print((unsigned int)nunchuk_accelz());
  694.          
  695.           //cal acc
  696.           lcd.setCursor(0,2);
  697.           lcd.print("cX    ");
  698.           lcd.setCursor(2,2);
  699.           lcd.print((signed int)nunchuk_caccelx());
  700.           lcd.setCursor(6,2);
  701.           lcd.print("cY    ");
  702.           lcd.setCursor(9,2);
  703.           lcd.print((signed int)nunchuk_caccely());
  704.           lcd.setCursor(12,2);
  705.           lcd.print("cZ    ");
  706.           lcd.setCursor(15,2);
  707.           lcd.print((signed int)nunchuk_caccelz());
  708.          
  709.           //cal joy
  710.           lcd.setCursor(0,3);
  711.           lcd.print("cJx    ");
  712.           lcd.setCursor(3,3);
  713.           lcd.print((signed int)nunchuk_cjoy_x());
  714.           lcd.setCursor(7,3);
  715.           lcd.print("cJy    ");
  716.           lcd.setCursor(10,3);
  717.           lcd.print((signed int)nunchuk_cjoy_y());
  718.  
  719.           //angle of joy
  720.           // lcd.setCursor(0,2);
  721.           // lcd.print("Ang       ");
  722.           // lcd.setCursor(4,2);
  723.           // lcd.print((signed int)nunchuk_joyangle());
  724.           // lcd.setCursor(10,2);
  725.           // lcd.print("rol       ");
  726.           // lcd.setCursor(14,2);
  727.           // lcd.print((signed int)nunchuk_rollangle());
  728.           // lcd.setCursor(0,3);
  729.           // lcd.print("pitch           ");
  730.           // lcd.setCursor(6,3);
  731.           // lcd.print((signed int)nunchuk_pitchangle());
  732.          
  733.           if(nunchuk_zbutton() == 1 && menus == NUNCAL)
  734.           {
  735.             nunchuk_calibrate_joy();
  736.             nunchuk_calibrate_accelxy();
  737.             nunchuk_calibrate_accelz();
  738.             lcd.print((__FlashStringHelper *)Exit);
  739.             while(nunchuk_zbutton() == 1){
  740.               nunchuk_send_request();
  741.               nunchuk_get_data();
  742.             }
  743.             break;
  744.           }
  745. #endif
  746.         }
  747.         if(analogRead(A0) > 70)
  748.         {
  749.           lcd.print((__FlashStringHelper *)Exit);
  750.           while(analogRead(A0) > 70){};
  751.           digitalWrite(XBEESLEEP, HIGH);
  752.           break;
  753.         }
  754.         }
  755.       }
  756.       else
  757.       {
  758.         menu = NUNSUB;
  759.       }
  760. #endif
  761.       break;
  762.     case PS2:
  763.       if(menus == BACKMAIN)
  764.       {
  765.         menu=MAINSUB;
  766.         menulen=MAINLEN;
  767.         currmenu = MAIN;
  768.         menusel = 0;
  769.       }
  770. #ifdef PS2_ENABLE
  771.       else if(menus == PS2PRES)
  772.       {
  773.         lcd.clear();
  774.         error = ps2x.config_gamepad(13,11,9,12, true, false);
  775.         if(error == 1)
  776.           lcd.print((__FlashStringHelper *)Timeout);
  777.         else
  778.           lcd.print(F("PS2 Ctl Found"));
  779.         lcd.print((char)error + '0');
  780.         delay(1000);
  781.       }
  782.       else if(menus == PS2INIT)
  783.       {
  784.         lcd.clear();
  785.         error = ps2x.config_gamepad(13,11,9,12, true, false);
  786.         lcd.print((char)error);
  787.         delay(1000);
  788.       }
  789.       else if(menus == PS2DLCD || menus == PS2DSER)
  790.       {
  791.         lcd.clear();
  792.         digitalWrite(XBEESLEEP, LOW);
  793.         error = ps2x.config_gamepad(13,11,9,12, true, false);
  794.         while(1){
  795.           ps2x.read_gamepad(false, 0);
  796.           if(menus == PS2DLCD)
  797.           {
  798.           //Joysticks
  799. #ifdef PS2_DBG_LCD
  800.           lcd.home();
  801.           lcd.print("rX   ");
  802.           lcd.setCursor(2,0);
  803.           lcd.print((unsigned int)ps2x.Analog(PSS_RX));
  804.           lcd.setCursor(5,0);
  805.           lcd.print("Y   ");
  806.           lcd.setCursor(6,0);
  807.           lcd.print((unsigned int)ps2x.Analog(PSS_RY));
  808.           lcd.setCursor(9,0);
  809.           lcd.print("lX   ");
  810.           lcd.setCursor(11,0);
  811.           lcd.print((unsigned int)ps2x.Analog(PSS_LX));
  812.           lcd.setCursor(14,0);
  813.           lcd.print("Y   ");
  814.           lcd.setCursor(15,0);
  815.           lcd.print((unsigned int)ps2x.Analog(PSS_LY));
  816.           lcd.setCursor(18,0);
  817.           lcd.print("3 ");
  818.           lcd.setCursor(19,0);
  819.           lcd.print((unsigned int)ps2x.Button(PSB_L3));
  820.        
  821.           lcd.setCursor(0,1);
  822.           lcd.print("X   ");
  823.           lcd.setCursor(1,1);
  824.           lcd.print((unsigned int)ps2x.Analog(PSAB_CROSS));
  825.           lcd.setCursor(4,1);
  826.           lcd.print("T   ");
  827.           lcd.setCursor(5,1);
  828.           lcd.print((unsigned int)ps2x.Analog(PSAB_TRIANGLE));
  829.           lcd.setCursor(8,1);
  830.           lcd.print("Sq   ");
  831.           lcd.setCursor(10,1);
  832.           lcd.print((unsigned int)ps2x.Analog(PSAB_SQUARE));
  833.           lcd.setCursor(13,1);
  834.           lcd.print("O   ");
  835.           lcd.setCursor(14,1);
  836.           lcd.print((unsigned int)ps2x.Analog(PSAB_CIRCLE));
  837.           lcd.setCursor(17,1);
  838.           lcd.print("R3 ");
  839.           lcd.setCursor(19,1);
  840.           lcd.print((unsigned int)ps2x.Button(PSB_R3));
  841.          
  842.           lcd.setCursor(0,2);
  843.           lcd.print("U   ");
  844.           lcd.setCursor(1,2);
  845.           lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_UP));
  846.           lcd.setCursor(4,2);
  847.           lcd.print("D   ");
  848.           lcd.setCursor(5,2);
  849.           lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_DOWN));
  850.           lcd.setCursor(8,2);
  851.           lcd.print("L    ");
  852.           lcd.setCursor(10,2);
  853.           lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_LEFT));
  854.           lcd.setCursor(13,2);
  855.           lcd.print("R   ");
  856.           lcd.setCursor(14,2);
  857.           lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_RIGHT));
  858.           lcd.setCursor(17,2);
  859.           lcd.print("ST ");
  860.           lcd.setCursor(19,2);
  861.           lcd.print((unsigned int)ps2x.Button(PSB_START));
  862.          
  863.           lcd.setCursor(0,3);
  864.           lcd.print("L   ");
  865.           lcd.setCursor(1,3);
  866.           lcd.print((unsigned int)ps2x.Analog(PSAB_L2));
  867.           lcd.setCursor(4,3);
  868.           lcd.print("R   ");
  869.           lcd.setCursor(5,3);
  870.           lcd.print((unsigned int)ps2x.Analog(PSAB_R2));
  871.           lcd.setCursor(8,3);
  872.           lcd.print("L1   ");
  873.           lcd.setCursor(10,3);
  874.           lcd.print((unsigned int)ps2x.Analog(PSAB_L1));
  875.           lcd.setCursor(13,3);
  876.           lcd.print("R   ");
  877.           lcd.setCursor(14,3);
  878.           lcd.print((unsigned int)ps2x.Analog(PSAB_R1));
  879.           lcd.setCursor(17,3);
  880.           lcd.print("Se ");
  881.           lcd.setCursor(19,3);
  882.           lcd.print((unsigned int)ps2x.Button(PSB_SELECT));
  883. #endif
  884.           }
  885.           else
  886.           {
  887. #ifdef PS2_DBG_SER
  888.           Serial.print("rX ");
  889.           Serial.print((unsigned int)ps2x.Analog(PSS_RX));
  890.           Serial.print("Y  ");
  891.           Serial.print((unsigned int)ps2x.Analog(PSS_RY));
  892.           Serial.print("lX ");
  893.           Serial.print((unsigned int)ps2x.Analog(PSS_LX));
  894.           Serial.print("Y  ");
  895.           Serial.print((unsigned int)ps2x.Analog(PSS_LY));
  896.           Serial.print("3  ");
  897.           Serial.print((unsigned int)ps2x.Button(PSB_L3));
  898.        
  899.           Serial.print("X  ");
  900.           Serial.print((unsigned int)ps2x.Analog(PSAB_CROSS));
  901.           Serial.print("T  ");
  902.           Serial.print((unsigned int)ps2x.Analog(PSAB_TRIANGLE));
  903.           Serial.print("Sq ");
  904.           Serial.print((unsigned int)ps2x.Analog(PSAB_SQUARE));
  905.           Serial.print("O  ");
  906.           Serial.print((unsigned int)ps2x.Analog(PSAB_CIRCLE));
  907.           Serial.print("R3 ");
  908.           Serial.print((unsigned int)ps2x.Button(PSB_R3));
  909.          
  910.           Serial.print("U  ");
  911.           Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_UP));
  912.           Serial.print("D  ");
  913.           Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_DOWN));
  914.           Serial.print("L  ");
  915.           Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_LEFT));
  916.           Serial.print("R  ");
  917.           Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_RIGHT));
  918.           Serial.print("ST ");
  919.           Serial.print((unsigned int)ps2x.Button(PSB_START));
  920.          
  921.           Serial.print("L  ");
  922.           Serial.print((unsigned int)ps2x.Analog(PSAB_L2));
  923.           Serial.print("R  ");
  924.           Serial.print((unsigned int)ps2x.Analog(PSAB_R2));
  925.           Serial.print("L1 ");
  926.           Serial.print((unsigned int)ps2x.Analog(PSAB_L1));
  927.           Serial.print("R  ");
  928.           Serial.print((unsigned int)ps2x.Analog(PSAB_R1));
  929.           Serial.print("Se ");
  930.           Serial.println((unsigned int)ps2x.Button(PSB_SELECT));
  931. #endif
  932.           }
  933.         if(analogRead(A0) > 70)
  934.         {
  935.           lcd.print((__FlashStringHelper *)Exit);
  936.           while(analogRead(A0) > 70){};
  937.           digitalWrite(XBEESLEEP, HIGH);
  938.           break;
  939.         }
  940.         }
  941.       }
  942. #endif
  943.       break;
  944.     case uSD:
  945.       if(menus == BACKMAIN)
  946.       {
  947.         menu=MAINSUB; ;
  948.         menulen=MAINLEN;
  949.         currmenu = MAIN;
  950.         menusel = 0;
  951.       }
  952. #ifdef SD_ENABLE
  953.       else if (menus == SDPRES)
  954.       {
  955.         if (!SD.begin(SDCHIPSEL))
  956.         {
  957.           lcd.clear();
  958.           lcd.print((__FlashStringHelper *)Timeout);
  959.         }
  960.         else
  961.         {
  962.           lcd.print(F("Card Found"));
  963.         }
  964.         delay(1000);
  965.       }
  966.       else if (menus == SDCNGI)
  967.       {//fileNum fileNum
  968.         //filename in array file
  969.         //idea is to write to array
  970.        
  971.         //if Nunchuck present, use it to scroll up and down on menu
  972.         //Wire.beginTransmission(0x52);
  973.         //if( Wire.endTransmission() == 0)
  974.        
  975.         nunchuk_init();
  976.         lcd.clear();
  977.         while(1)
  978.         {
  979.           nunchuk_send_request();
  980.           nunchuk_get_data();
  981.          
  982.           lcd.home();
  983.           lcd.print(F("Filename:File   "));
  984.           lcd.setCursor(13,0);
  985.           if(nunchuk_joy_y() > 170)
  986.             fileNum++;
  987.           if(nunchuk_joy_y() < 100)
  988.             fileNum--;
  989.           lcd.print((byte)fileNum );
  990.          
  991.          
  992.          
  993.           delay(100);
  994.          
  995.          
  996.           if(analogRead(A0) > 60)
  997.           {
  998.             lcd.setCursor(0,1);
  999.             //write changes
  1000.             EEPROM.write(FILENAME_OFFSET, fileNum);  //1 byte file name
  1001.             convert_string(&file[4], fileNum);
  1002.             file[8] = 0;
  1003.             lcd.print(file);
  1004.             lcd.print((__FlashStringHelper *)Exit);
  1005.             while(analogRead(A0) > 70){};
  1006.             break;
  1007.          
  1008.           }
  1009.         }
  1010.       }
  1011.       else if (menus == SDPREV)
  1012.       {
  1013.         //idea is to preview first page of file, or perhaps
  1014.         //let you scroll with a attached joystick.
  1015.         lcd.clear();
  1016.        
  1017.         //show file name
  1018.         lcd.print(file);
  1019.        
  1020.         //load file
  1021.         convert_string(&file[4], fileNum);
  1022.         file[8] = 0;  //make sure it is null terminated
  1023.        
  1024.         //card present, open file
  1025.         logFile = SD.open(file);
  1026.         delay(1000);
  1027.         if(logFile)
  1028.         {
  1029.           //logFile.println("testing");
  1030.           int i = 0;
  1031.           lcd.clear();
  1032.           while(logFile.available())
  1033.           {
  1034.             lcd.print((char)logFile.read());
  1035.             i++;
  1036.             if(i >= 40)
  1037.             {
  1038.             if(analogRead(A0) > 60)
  1039.             {
  1040.               lcd.print((__FlashStringHelper *)Exit);
  1041.               while(analogRead(A0) > 70){};
  1042.               break;
  1043.             }
  1044.             delay(2000);
  1045.             i=0;
  1046.             }
  1047.           }
  1048.           logFile.close();
  1049.           delay(2000);
  1050.         }
  1051.       }
  1052. #endif
  1053.       break;
  1054.     default:
  1055.       break;
  1056.   }
  1057.  
  1058. }
  1059.  
  1060. void loop()
  1061. {
  1062.   uint8_t button;
  1063.  
  1064.   lcd.clear();
  1065.   //simple menu here
  1066.   for(int i=0; i<HMENU && index[i+menu]-currmenu <= menulen ; i++)
  1067.   {
  1068.     lcd.setCursor(1,i);
  1069.     lcd.print((__FlashStringHelper *)MenuTable[index[i+menu]]);
  1070.    
  1071.   }
  1072.  
  1073.   //draw select
  1074.   lcd.setCursor(0,menusel);
  1075.   lcd.write(SELECT);
  1076. #ifdef DEBUG
  1077.   lcd.print((char)(menu + '0'));
  1078.   lcd.print(' ');
  1079.   lcd.print((char)(menusel + '0'));
  1080. #endif
  1081.   button = buttonWait(A0,NO,YES);
  1082.  
  1083.   if(button == BTND)
  1084.   {
  1085.     if(index[menu] - currmenu + HMENU-1  < menulen)  //space to potentially go down
  1086.       menu++;//increment menu
  1087.     else if(index[menusel + menu] - currmenu < menulen)
  1088.       menusel++;
  1089.     else
  1090.     {
  1091.       //at bottom of menu, go back to top
  1092.      menu = currmenu;
  1093.      menusel = 0;
  1094.     }
  1095.   }
  1096.   if(button == BTNB)
  1097.     select(currmenu, index[menusel+menu]-currmenu);
  1098. }
  1099.  
  1100. int setup_ser(char ser, PGM_P PROGMEM string, char init, char initcr, char justppp, unsigned long timeout,uint16_t analogpin)
  1101. {
  1102.   char state=0, temp;
  1103.   //unsigned long time1 = millis();
  1104. #ifdef BT_ENABLED
  1105.   if(ser == BT)
  1106.   {
  1107.   if(init){
  1108.     //delay(1000);
  1109.     lcd.print((__FlashStringHelper *)ppplus);
  1110.     btser.print((__FlashStringHelper *)ppplus);
  1111.     if(initcr)
  1112.       btser.write(0x0D);
  1113.     delay(1000);
  1114.   }
  1115.   else
  1116.     state = 3;
  1117.   while(1)
  1118.   {
  1119.     if(state == 3)
  1120.     {
  1121.       state++;
  1122.       if(justppp)
  1123.         continue;
  1124.       lcd.print((__FlashStringHelper *)string);
  1125.       btser.print((__FlashStringHelper *)string);
  1126.       btser.write(0x0D);
  1127.       delay(100);
  1128.     }
  1129.     if(btser.available())
  1130.     {
  1131.       temp = btser.read();
  1132.       if(temp == 0x0D)
  1133.       {
  1134.         state++;
  1135.         continue;
  1136.       }
  1137.       lcd.print((char)temp);
  1138.       //lcd.print((char)(state + '0'));
  1139.       if(temp == 'O')
  1140.         state++;
  1141.       if(temp == 'K')
  1142.         state++;
  1143.       //if(temp == 'E' || temp == 'R')
  1144.       //  return 0; //error
  1145.     }
  1146.     if(analogRead(analogpin) > 70)
  1147.     {
  1148.       lcd.print((char)temp);
  1149.       lcd.print((__FlashStringHelper *)Exit);
  1150.       while(analogRead(analogpin) > 70){};
  1151.       return 0;
  1152.     }
  1153.   }
  1154.   }
  1155. #endif
  1156. #ifdef XBEE_ENABLED
  1157.   #ifndef BT_ENABLED
  1158.   if
  1159.   #else
  1160.   else  //XBEE HW serial
  1161.   #endif
  1162. #endif
  1163. #ifdef XBEE_ENABLED
  1164.   {
  1165.     if(init){
  1166.     //delay(1000);
  1167.     lcd.print((__FlashStringHelper *)ppplus);
  1168.     Serial.print((__FlashStringHelper *)ppplus);
  1169.     if(initcr)
  1170.       Serial.write(0x0D);
  1171.     delay(1000);
  1172.   }
  1173.   else
  1174.     state = 3;
  1175.   while(state != 6)
  1176.   {
  1177.     if(state == 3)
  1178.     {
  1179.       state++;
  1180.       lcd.print((__FlashStringHelper *)string);
  1181.       Serial.print((__FlashStringHelper *)string);
  1182.       Serial.write(0x0D);
  1183.       delay(100);
  1184.     }
  1185.     if(Serial.available())
  1186.     {
  1187.       temp = Serial.read();
  1188.       if(temp == 0x0D)
  1189.       {
  1190.         state++;
  1191.         continue;
  1192.       }
  1193.       //lcd.print((char)(state + '0'));
  1194.       if(temp == 'O')
  1195.         state++;
  1196.       if(temp == 'K')
  1197.         state++;
  1198.       //if(temp == 'E' || temp == 'R')
  1199.       //  return 0; //error  
  1200.       lcd.print((char)temp);
  1201.     }
  1202.     if(analogRead(analogpin) > 70)
  1203.     {
  1204.       lcd.print((char)temp);
  1205.       lcd.print((__FlashStringHelper *)Exit);
  1206.       while(analogRead(analogpin) > 70){};
  1207.       return 0;
  1208.     }
  1209.   }
  1210.   }
  1211. #endif
  1212.  return 1;
  1213. }
  1214.  
  1215. uint8_t parse_buttons(uint16_t button)
  1216. {
  1217. //given the channel and button, return corresponding actual button
  1218. //hardware specific
  1219.   switch(button)
  1220.   {
  1221.       case 1:
  1222.           return BTNU;
  1223.       case 2:
  1224.           return BTND;
  1225.       case 3:
  1226.           return BTNL;
  1227.       case 4:
  1228.           return BTNR;
  1229.       case 5:
  1230.           return BTNA;
  1231.       case 6:
  1232.           return BTNB;
  1233.       case 12:
  1234.           return BTNB;
  1235.       default:
  1236.           break;
  1237.   };
  1238.   return 255;
  1239. }
  1240.  
  1241. uint8_t buttonCheck( uint16_t analogpin)
  1242. {
  1243.     uint16_t i=0, upper, lower;
  1244.     uint8_t button = 0;
  1245.     uint16_t analogval;
  1246.     analogval = analogRead(analogpin);
  1247.     //find what button it corresponds to
  1248.     for( uint16_t i = 0; i < 21; i++)
  1249.     {
  1250.         // checks the _analogval against the high and low vales in the array
  1251.         upper = pgm_read_word(&(Button[i][1]));
  1252.         lower = pgm_read_word(&(Button[i][2]));
  1253.        
  1254.         if(analogval >= upper && analogval <= lower)
  1255.         {
  1256.             // stores the button number to a variable
  1257.             button = pgm_read_word(&(Button[i][0]));
  1258.             return button;    
  1259.         }
  1260.     }
  1261.     return button;
  1262. }
  1263.  
  1264. uint8_t buttonWait(uint16_t analogpin, uint8_t toggle, uint8_t hold)
  1265. {  
  1266.     //if toggle set, wait until button comes back to 0 position
  1267.     uint16_t init_butt=0,state = 0, offset=0;
  1268.     uint32_t time;
  1269.     uint8_t buffer[10];
  1270.     uint8_t value;
  1271.     //consider sleep code here
  1272.  
  1273.     while(1){
  1274.         while(state == 0)
  1275.         {
  1276.           for( uint16_t j=0; j<10; j++)
  1277.           {
  1278.               buffer[j] = buttonCheck(analogpin);
  1279.               delay(1);
  1280.              
  1281.           }
  1282.           value = buffer[0];
  1283.           for( uint16_t j=1; j<10;j++)
  1284.           {
  1285.               if(value != buffer[j])
  1286.               {
  1287.                   //mismatch, discard
  1288.                   value =255;
  1289.                   break;
  1290.               }
  1291.           }
  1292.           if((value != 255) && (value != 0))
  1293.           {
  1294.               state = 2;
  1295.               init_butt = value;
  1296.               time = millis();
  1297.           }
  1298.         }
  1299.         if(toggle == YES)
  1300.         {
  1301.             state = 1;  
  1302.         }
  1303.         while(state == 1)
  1304.         {
  1305.             if(millis() - time > DEBOUNCE)
  1306.             {
  1307.                 return parse_buttons(init_butt);
  1308.             }
  1309.         }
  1310.         while (state == 2)
  1311.         {
  1312.             //wait for button fall
  1313.             for( uint16_t j=0; j<10; j++)
  1314.             {
  1315.                 buffer[j] = buttonCheck(analogpin);
  1316.             }
  1317.             value = buffer[0];
  1318.             for( uint16_t j=1; j<10;j++)
  1319.             {
  1320.                 if(value != buffer[j])
  1321.                 {
  1322.                     //mismatch, discard
  1323.                     value =254;
  1324.                     break;
  1325.                 }
  1326.             }
  1327.             if( hold == YES && millis() - time > PRESSHOLD)
  1328.             {
  1329.               offset = 10;
  1330.             }
  1331.             if( value == 0)
  1332.             {
  1333.                 return parse_buttons(offset+init_butt);
  1334.             }
  1335.         }
  1336.     }
  1337.     return 0;        
  1338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement