All controller functions functional! Now I need some remote projects!

This slideshow requires JavaScript.

So I have gotten to a point at which everything for the controller works. SD card, Xbee, BT serial, Shift LCD, PS2, and NunChuck. Pretty awesome. But if all of that code is enabled at once, I would have less than 1k bytes to write actual code to interface with things. In the code I have debugging via serial, debugging via LCD (for the controllers). I do like having debugging for LCD, but there is a problem: the PS2 library takes up 5k bytes. So when I actually build the controller, I don’t think I’ll include a PS2 connector, at least initially.

Breakdown of Sizes
with SD enabled
Everything Enabled: 29024 Bytes
Only LCD debug: 27252 Bytes
Only NUN LCD debug, PS2 disabled: 22550
Only PS2 LCD debug, NUN disabled: 25900
Only PS2 & Nun, no debug: 24690
Only PS2, no debug: 24484
No ps2 or Nun: 20972
With SD disabled
Nun and PS2 disabled:13488
BT disabled + SD + NUN PS2
Xbee: 11910
Everything Disabled:
11886 bytes

I think the optimal configuration would be to use the Nunchuck with LCD debugging information, with SD, XBEE and BT, coming at a total of 22550 bytes, giving me about 7.5k to write my code for all of my interfaced devices.

This has led me to the idea, that my interfaced devices should be giving the controller, the menus it should show. I’m thinking of an initial packet that is exchanged between the two, which has a good amount of strings, that are then saved into flash on the controller, and once data starts arriving, those ‘labels’ of the data will make the data make sense. Another idea is to use the SD card to carry firmware for remote projects, and remotely flash them using the controller on the go, so I can update code for my cars without having to take my laptop down there.

Well here it is: http://pastebin.com/JudSCJNN

Ugly code below, I paste this in case pastebin ever goes down 😛 They have been up since 2002, but I have seen too many dead links on the web…

/* Controller 2.0
Created by Dustin Reynolds at Mecharobotics.wordpress.com

You may share this library with Creative Commons – Share Alike
license 2.0.

This is released via the Creative Commons Share Alike 2.0 license:
http://creativecommons.org/licenses/by-sa/2.0/

You can copy it and use it in whatever, just attribute my name,
Dustin Reynolds, and my blog when you do.
I’ll appreciate if you let me know that you’ve used it,
so I can show off your project on my blog 🙂

Copyright 2012 Dustin Reynolds
*****************************************************************************
Includes the following:
20×4 LCD
XBee Connected to pins 0 and 1 of ATMEGA328
BTM-182 connected to pins 7 and 7
PS2 connector
Nunchuck connector
microSD adapter (using old microSD to SD converter)

Size comparisons:
with SD enabled
Everything Enabled: 29024 Bytes
Only LCD debug: 27252 Bytes
Only NUN LCD debug, PS2 disabled: 22550
Only PS2 LCD debug, NUN disabled: 25900
Only PS2 & Nun, no debug: 24690
Only PS2, no debug: 24484
No ps2 or Nun: 20972
With SD disabled
Nun and PS2 disabled:13488
BT disabled + SD + NUN PS2
Xbee: 11910
Everything Disabled:
11886 bytes

Recommended Flags enabled:
SD_enable, NUN_Enable, NUN_DBG_LCD
-ps2 library takes about 5k of space, nun takes 3.6k
-num can do plenty, can improvize with buttons + joystick

Circuit: Description of the circuit Textually:
3.3v VReg (MCP1702-3302E)
1-GND
2- + of battery, 0.1u to gnd*
3-3.3v, 0.1u*
*-1uF across 3.3V and gnd, 0.1 accross 3.3v and gnd
ATMEGA328
1-connected to Pin 6 of FTDI cable
2-Connected to pin 4 of FTDI Cable, To pin 2 of xbee (through a 1k resistor)
3-Connected to pin 5 of FTDI cable, to pin 3 of xbee (through a 1k resistor)
http://www.billporter.info/how-to-add-multiple-uart-connections/
4-NC
5-Pin 11 of shift register
6-Pin 14 of shift register
7-3.3V
8-gnd
9-left lead of 16MHz resonator (center to gnd)
10-right lead of 16MHz resonator(center to gnd)
11-Pin 12 of Shift register, 1uf cap to ground
12-Pin 9 of XBee
13-Pin 9 of BTM-182 BT serial module
14-Pin 10 of BTM-182 BT serial module
15-Pin 6 of PS2 controller
16-Pin 1 of SD adapter
17-pin 2 of PS2 controller, Pin 2 of SD card
18-Pin 1 of PS2 controller, Pin 7 of SD card
19-Pin 7 of PS2 controller, pin 5 of SD card
20-3.3V
21-3.3V
22-GND
23-1k to gnd, one end of button, other is connected to 430ohm, connected to 3.3v
24-
25-
26-
27-2.2k to 3.3v, pin 1 of nunchuck(datapin)
28-2.2k to 3.3v, pin 2 of nunchuck(clk)
SD card (Notch to Left, not counting end connections)
1-pin 16 of cpu
2-pin 17 of cpu
3-gnd
4-3.3v
5-pin 19 of cpu
6-gnd
7-pin 18 of cpu
XBee Series 1 Module
X-CTU:http://www.digi.com/support/productdetail?pid=3352&osvid=0&type=utilities
Wireless dl:http://www.sparkfun.com/tutorials/122
1-3.3v
2-1k connected to pin 2 of mcu
3-1k connected to pin 3 of mcu
9-connected to pin 12 of mcu
10-gnd
BTM-182 BT serial module
http://www.sparkfun.com/products/9913
AT commands:http://www.robot-r-us.com/downloads/sparkfun/wigwrl/812-wrl09977-lm400-data-sheet-dss-ver-1-2/download.html
9-pin 13 of mcu
10-pin 14 of mcu
15-3.3v
16-gnd
Shift Register 74HC595
http://code.google.com/p/arduinoshiftreglcd/
1-nc
2-pin 4 of lcd
3-pin 11 of lcd
4-pin 12 of lcd
5-pin 13 of lcd
6-pin 14 of lcd
7-nc
8-gnd
9-nc
10-3.3v
11-pin 5 of mcu
12-pin 11 of mcu,0.1uF to gnd,pin 6 of lcd
13-gnd
14-pin 6 of mcu
15-nc
16-3.3v
LCD 3.3V NewHaven 20×4 character LCD
1-gnd
2-3.3V
3-middle pin of 10k pot, other pins at 3.3v and gnd
4-pin 2 of 74Hc595 shift reg
5-gnd
6-pin 12 of shift reg
7,8,9,10-nc
11-pin 3 of shift
12-pin 4 of shift
13-pin 5 of shift
14-pin 6 of shift
15-nc(can be conn to 3.3v for backlight
16-gnd
PS2 (round end down)
http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/
1-pin 18 of mcu
2-pin 17 of mcu
3-nc(for motors)
4-gnd
5-3.3v
6-pin 15 of mcu
7-pin 19 of mcu
Nunchuck – dealextreme knockoff < $7
(nunchuck conn facing you, looks like a U not a n)
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273496638
1(upper left)-pin 27 of mcu, 2.2k pullup to 3.3v (clock)
2(upper right)-gnd
3(lower left)-3.3v
4(lower right)-pin 28 of mcu, 2.2k pullup to 3.3v (data)
Xbee is run using HW seria, BT using sw serial
PS2 run on SPI bus, SD on SPI bus
Nun run on I2C bus
LCD only uses 3 pins using shift reg
entire system at 3.3v

Notes:
How do I create a menu?
1. Add your menu in section 1. Edit the main menu to have your desired name in it.
Be sure to do yournameSUB and yournameLEN correctly, follow the examples. Bakmain is
still counted, since this references the table in sec 4.
2. Add a yournameCONF to the list of confs, create a unique #define for each menu item.
3. Add a #define for your menu, set it to be the same as yournameSUB. These are used in a
big switch statement in the select function
4. Add your prog_char's defined in step 1, to this table, in the correct space.
5. Goto select function, create a if statement in case: MAIN. Create a case for your menu
name defined in step 3.
Dustin Reynolds
2012-03-24
*/
#include
#include
#include //for v1.6
#include
#include
#include
#include
#include

//Comment out these to disable specific features. Won’t disable menus, since that is complicated
//#define PS2_ENABLE
//#define PS2_DBG_SER
//#define PS2_DBG_LCD
//#define NUN_ENABLE
//#define NUN_DBG_SER
//#define NUN_DBG_LCD

#define XBEE_ENABLE
#define BT_ENABLE

//#define SD_ENABLE
#define FILENAME_OFFSET 20 //single byte stored in EEPROM
#define HMENU 4
#define WMENU 20

File logFile;
#define SDCHIPSEL 10
PS2X ps2x; // create PS2 Controller Class
//#define DEBUG 1

ShiftRegLCD lcd(4, 3, 5, 4); //we have a 20×4 LCD data clock enable #lines
#ifdef BT_ENABLE
SoftwareSerial btser(7, 8);
#endif
//want simple XBEE config
#define XBEESLEEP 6 //Pin 12 for XBee sleep control, goes to pin 9 on xbee

#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

#define UPARROW ((uint8_t)3)
#define DOWNARROW ((uint8_t)4)
#define SELECT ((uint8_t)5)
#define HEART ((uint8_t)0)
#define SMILE ((uint8_t)2)
#define KEY ((uint8_t)1)
/***********************************SECTION 2 *****************************************/
#define XBEECONF 0
#define BTCONF 1
#define NUNCONF 2
#define PS2CONF 3
#define SDCONF 4
#define CONNCPU 5
#define CONNGAR 6
#define CONNSAB 7

#define BACKMAIN 0
#define XBEEPPP 1
#define XBEESM1 2
#define XBEECLOSE 3

#define BTPPP 1
#define BTATR0 2
#define BTATH 3
#define BTATF 4
#define BTATD 5
#define BTATA 6
#define BTCLOSE 7

#define NUNPRES 1
#define NUNINIT 2
#define NUNCAL 3
#define NUNDLCD 4
#define NUNDSER 5

#define PS2PRES 1
#define PS2INIT 2
#define PS2DLCD 3
#define PS2DSER 4

#define SDPRES 1
#define SDCNGI 2
#define SDPREV 3
/*****************************END OF SECTION 2 *****************************************/
/***********************************SECTION 1 *****************************************/
#define MAINSUB 0
#define MAINLEN 7 //actual – 1
const prog_char PROGMEM MenuStr1[] = “XBee Configure”;
const prog_char PROGMEM MenuStr2[] = “BT Configure”;
const prog_char PROGMEM MenuStr3[] = “NunChuck Configure”;
const prog_char PROGMEM MenuStr4[] = “PS2 Configure”;
const prog_char PROGMEM MenuStr5[] = “SD Configure”;
const prog_char PROGMEM MenuStr6[] = “Connect to CPU”;
const prog_char PROGMEM MenuStr7[] = “Connect to Garden”;
const prog_char PROGMEM MenuStr8[] = “Connect to Sable”;
#define XBEESUB 8 //actual – 1

#define XBEELEN 3 //actual – 1
const prog_char PROGMEM Bakmain[] = “Back to Main”;
const prog_char PROGMEM Xbee2[] = “+++”;
const prog_char PROGMEM Xbee3[] = “SM1,ST3e8,SO1”;
const prog_char PROGMEM Xbee4[] = “WN,CN”;
#define BTSUB 12

#define BTLEN 7 //actual – 1
//const prog_char PROGMEM Bakmain[] = “Back to Main”;
const prog_char PROGMEM BTser2[] = “+++ <-not alneeded";
const prog_char PROGMEM BTser3[] = "ATR0";
const prog_char PROGMEM BTser4[] = "ATH (drops conn)";
const prog_char PROGMEM BTser5[] = "ATF?";
const prog_char PROGMEM BTser6[] = "ATD";
const prog_char PROGMEM BTser7[] = "ATA";
const prog_char PROGMEM BTser8[] = "ATO closes conn";
#define NUNSUB 20

#define NUNLEN 5 //actual – 1
//const prog_char PROGMEM Bakmain[] = "Back to Main"; <-count it, but only use one
const prog_char PROGMEM NUN2[] = "Nunchuk Present?";
const prog_char PROGMEM NUN3[] = "Init";
const prog_char PROGMEM NUN4[] = "SetCal";
const prog_char PROGMEM NUN5[] = "Disp LCD";
const prog_char PROGMEM NUN6[] = "Disp Serial";
#define PS2SUB 26

#define PS2LEN 4 //actual – 1
//const prog_char PROGMEM Bakmain[] = "Back to Main"; <-count it, but only use one
const prog_char PROGMEM PS22[] = "PS2 Ctl Present?";
//const prog_char PROGMEM NUN3[] = "Init";
const prog_char PROGMEM PS24[] = "Disp LCD";
const prog_char PROGMEM PS25[] = "Disp Serial";
#define SDSUB 31

#define SDLEN 3 //actual – 1
//const prog_char PROGMEM Bakmain[] = "Back to Main"; <-count it, but only use one
const prog_char PROGMEM uSD2[] = "SD Present?";
const prog_char PROGMEM uSD3[] = "Change File Index";
const prog_char PROGMEM uSD4[] = "Preview Top File";
/*****************************END OF SECTION 1 *****************************************/
/***********************************SECTION 3 *****************************************/
//currentmenu
#define MAIN MAINSUB
#define XBEE XBEESUB
#define BT BTSUB
#define NUN NUNSUB
#define PS2 PS2SUB
#define uSD SDSUB
/*****************************END OF SECTION 3 *****************************************/
/***********************************SECTION 4 *****************************************/
PGM_P PROGMEM MenuTable[] = {
MenuStr1,
MenuStr2,
MenuStr3,
MenuStr4,
MenuStr5,
MenuStr6,
MenuStr7,
MenuStr8,
Bakmain,
Xbee2,
Xbee3,
Xbee4,
Bakmain,
BTser2,
BTser3,
BTser4,
BTser5,
BTser6,
BTser7,
BTser8,
Bakmain,
NUN2,
NUN3,
NUN4,
NUN5,
NUN6,
Bakmain,
PS22,
NUN3,
PS24,
PS25,
Bakmain,
uSD2,
uSD3,
uSD4,
};
/*****************************END OF SECTION 4 *****************************************/
const prog_char PROGMEM ppplus[] = "+++";
const prog_char PROGMEM sm1[] = "ATSM1,ST3E8,SO1";
const prog_char PROGMEM wncn[] = "ATWN,CN";
const prog_char PROGMEM atr0[] = "ATR0";
const prog_char PROGMEM ath[] = "ATH";
const prog_char PROGMEM atf[] = "ATF?";
const prog_char PROGMEM atd[] = "ATD";
const prog_char PROGMEM ata[] = "ATA";
const prog_char PROGMEM ato[] = "ATO";
const prog_char PROGMEM Timeout[] = "TimeOut";
const prog_char PROGMEM Exit[] = "Exit";

//******************************************************************************
//
// 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

byte Select[8]={ B01000, B01100, B00110, B11111, B11111, B00110, B01100, B01000};

char menu = 0; //0 = main
char menulen = MAINLEN;
char menusel = 0; //cursor
char index[40];
char currmenu = MAIN; //current menu
byte fileNum = 0;
char file[20];
int setup_ser(char ser, char * string, char init, char initcr,char justppp, unsigned long timeout, uint16_t analogpin);
uint8_t buttonWait(uint16_t analogpin, uint8_t toggle, uint8_t hold);
uint8_t buttonCheck( uint16_t analogpin);

void setup()
{
//xbee
Serial.begin(19200);
pinMode(XBEESLEEP,OUTPUT); //6
digitalWrite(XBEESLEEP, HIGH);
#ifdef BT_ENABLE
//bt
pinMode (7,INPUT);
pinMode (8,OUTPUT);
btser.begin(9600);
#endif
#ifdef SD_ENABLE
pinMode(10, OUTPUT);
#endif
lcd.createChar(5, Select);

for(int i=0; i<40; i++)
index[i] = i;
strncpy(file,"File",5);

fileNum = EEPROM.read(FILENAME_OFFSET);
}
//good for numbers =10)
{
convert_string(string++, n/10);
n = n%10;
}
*string=n+’0′;
}

//handle things here, not in loop
void select(uint8_t cmenu, uint8_t menus)
{
int error = 0;
switch(cmenu)
{
case MAIN:
if(menus == XBEECONF)
{
menu=XBEESUB;
menulen=XBEELEN;
currmenu = XBEE;
digitalWrite(XBEESLEEP, LOW);
menusel = 0;
}
else if(menus == BTCONF)
{
menu=BTSUB;
menulen=BTLEN;
currmenu = BT;
menusel = 0;
}
else if(menus == NUNCONF)
{
menu=NUNSUB;
menulen=NUNLEN;
currmenu = NUN;
menusel = 0;
}
else if(menus == PS2CONF)
{
menu=PS2SUB;
menulen=PS2LEN;
currmenu = PS2;
menusel = 0;
}
else if(menus == SDCONF)
{
menu=SDSUB;
menulen=SDLEN;
currmenu = uSD;
menusel = 0;
}
else
menu = MAINSUB;
break;
case XBEE:
if(menus == BACKMAIN)
{
menu=MAINSUB; ;
menulen=MAINLEN;
currmenu = MAIN;
digitalWrite(XBEESLEEP, HIGH);
menusel = 0;
}
#ifdef XBEE_ENABLE
else if (menus == XBEEPPP)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(XBEE,wncn,YES,NO,NO,10000,A0);
//delay(2000);
}
else if (menus == XBEECLOSE)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(XBEE,wncn,YES,NO,NO,10000,A0);
//delay(2000);
}
else
menu = XBEESUB;
#endif
break;
case BT:
if(menus == BACKMAIN)
{
menu=MAINSUB;
menulen=MAINLEN;
currmenu = MAIN;
menusel = 0;
}
#ifdef BT_ENABLE
else if (menus == BTPPP)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,ppplus,YES,YES,YES,10000,A0);
//delay(2000);
}
else if (menus == BTATR0)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,atr0,NO,NO,NO,10000,A0);
//delay(2000);
}
else if (menus == BTATH)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,ath,NO,NO,NO,10000,A0);
//delay(2000);
}
else if (menus == BTATF)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,atf,NO,NO,NO,10000,A0);
//delay(2000);
}
else if (menus == BTATD)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,atd,NO,NO,NO,10000,A0);
//delay(2000);
}
else if (menus == BTATA)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,ata,NO,NO,NO,10000,A0);
//delay(2000);
}
else if (menus == BTCLOSE)
{
//do code here, or move to other function?
lcd.clear();
setup_ser(BT,ato,NO,NO,NO,10000,A0);
//delay(2000);
}
else
menu = BTSUB;
#endif
break;
case NUN:
if(menus == BACKMAIN)
{
menu=MAINSUB;
menulen=MAINLEN;
currmenu = MAIN;
menusel = 0;
}
#ifdef NUN_ENABLE
else if (menus == NUNPRES)
{
lcd.clear();
Wire.begin();
Wire.beginTransmission(0x52);
if( Wire.endTransmission() == 0)
lcd.print(F(“Nunchuck Found”));
else
lcd.print((__FlashStringHelper *)Timeout);
delay(1000);
}
else if (menus == NUNINIT)
{
lcd.print(“init starting”);
nunchuk_init();
lcd.clear();
nunchuk_send_request();
nunchuk_get_data();
}
else if (menus == NUNDLCD || menus == NUNDSER || menus == NUNCAL)
{
lcd.clear();
digitalWrite(XBEESLEEP, LOW);
while(1)
{
nunchuk_send_request();
nunchuk_get_data();

#ifdef NUN_DBG_SER
//lcd.clear();
if( menus == NUNDSER)
{
Serial.print(“Z “);
Serial.print((unsigned int)nunchuk_zbutton());
Serial.print(“\tC “);
Serial.print((unsigned int)nunchuk_cbutton());
//rest of data
Serial.print(“\tJx “);
Serial.print((unsigned int)nunchuk_joy_x());
Serial.print(“\tJy “);
Serial.print((unsigned int)nunchuk_joy_y());

Serial.print(“\tcJx “);
Serial.print((signed int)nunchuk_cjoy_x());
Serial.print(“\tcJy “);
Serial.print((signed int)nunchuk_cjoy_y());

//accel
Serial.print(“\tAcX “);
Serial.print((unsigned int)nunchuk_accelx());
Serial.print(“\tAcY “);
Serial.print((unsigned int)nunchuk_accely());
Serial.print(“\tAcZ “);
Serial.print((unsigned int)nunchuk_accelz());

Serial.print(“\tcAcX “);
Serial.print((signed int)nunchuk_caccelx());
Serial.print(“\tcAcY “);
Serial.print((signed int)nunchuk_caccely());
Serial.print(“\tcAcZ “);
Serial.println((signed int)nunchuk_caccelz());
delay(250);
}
#endif
if( menus == NUNDLCD || menus == NUNCAL)
{
#ifdef NUN_DBG_LCD
lcd.home();
lcd.print(“Z “);
lcd.print((unsigned int)nunchuk_zbutton());
lcd.print(“C “);
lcd.print((unsigned int)nunchuk_cbutton());
//rest of data
lcd.print(“Jx “);
lcd.setCursor(8,0);
lcd.print((unsigned int)nunchuk_joy_x());
lcd.setCursor(11,0);
lcd.print(“Jy “);
lcd.setCursor(13,0);
lcd.print((unsigned int)nunchuk_joy_y());

//accel – non cal
lcd.setCursor(0,1);
lcd.print(“aX “);
lcd.setCursor(3,1);
lcd.print((unsigned int)nunchuk_accelx());
lcd.setCursor(6,1);
lcd.print(“aY “);
lcd.setCursor(9,1);
lcd.print((unsigned int)nunchuk_accely());
lcd.setCursor(12,1);
lcd.print(“aZ “);
lcd.setCursor(15,1);
lcd.print((unsigned int)nunchuk_accelz());

//cal acc
lcd.setCursor(0,2);
lcd.print(“cX “);
lcd.setCursor(2,2);
lcd.print((signed int)nunchuk_caccelx());
lcd.setCursor(6,2);
lcd.print(“cY “);
lcd.setCursor(9,2);
lcd.print((signed int)nunchuk_caccely());
lcd.setCursor(12,2);
lcd.print(“cZ “);
lcd.setCursor(15,2);
lcd.print((signed int)nunchuk_caccelz());

//cal joy
lcd.setCursor(0,3);
lcd.print(“cJx “);
lcd.setCursor(3,3);
lcd.print((signed int)nunchuk_cjoy_x());
lcd.setCursor(7,3);
lcd.print(“cJy “);
lcd.setCursor(10,3);
lcd.print((signed int)nunchuk_cjoy_y());

//angle of joy
// lcd.setCursor(0,2);
// lcd.print(“Ang “);
// lcd.setCursor(4,2);
// lcd.print((signed int)nunchuk_joyangle());
// lcd.setCursor(10,2);
// lcd.print(“rol “);
// lcd.setCursor(14,2);
// lcd.print((signed int)nunchuk_rollangle());
// lcd.setCursor(0,3);
// lcd.print(“pitch “);
// lcd.setCursor(6,3);
// lcd.print((signed int)nunchuk_pitchangle());

if(nunchuk_zbutton() == 1 && menus == NUNCAL)
{
nunchuk_calibrate_joy();
nunchuk_calibrate_accelxy();
nunchuk_calibrate_accelz();
lcd.print((__FlashStringHelper *)Exit);
while(nunchuk_zbutton() == 1){
nunchuk_send_request();
nunchuk_get_data();
}
break;
}
#endif
}
if(analogRead(A0) > 70)
{
lcd.print((__FlashStringHelper *)Exit);
while(analogRead(A0) > 70){};
digitalWrite(XBEESLEEP, HIGH);
break;
}
}
}
else
{
menu = NUNSUB;
}
#endif
break;
case PS2:
if(menus == BACKMAIN)
{
menu=MAINSUB;
menulen=MAINLEN;
currmenu = MAIN;
menusel = 0;
}
#ifdef PS2_ENABLE
else if(menus == PS2PRES)
{
lcd.clear();
error = ps2x.config_gamepad(13,11,9,12, true, false);
if(error == 1)
lcd.print((__FlashStringHelper *)Timeout);
else
lcd.print(F(“PS2 Ctl Found”));
lcd.print((char)error + ‘0’);
delay(1000);
}
else if(menus == PS2INIT)
{
lcd.clear();
error = ps2x.config_gamepad(13,11,9,12, true, false);
lcd.print((char)error);
delay(1000);
}
else if(menus == PS2DLCD || menus == PS2DSER)
{
lcd.clear();
digitalWrite(XBEESLEEP, LOW);
error = ps2x.config_gamepad(13,11,9,12, true, false);
while(1){
ps2x.read_gamepad(false, 0);
if(menus == PS2DLCD)
{
//Joysticks
#ifdef PS2_DBG_LCD
lcd.home();
lcd.print(“rX “);
lcd.setCursor(2,0);
lcd.print((unsigned int)ps2x.Analog(PSS_RX));
lcd.setCursor(5,0);
lcd.print(“Y “);
lcd.setCursor(6,0);
lcd.print((unsigned int)ps2x.Analog(PSS_RY));
lcd.setCursor(9,0);
lcd.print(“lX “);
lcd.setCursor(11,0);
lcd.print((unsigned int)ps2x.Analog(PSS_LX));
lcd.setCursor(14,0);
lcd.print(“Y “);
lcd.setCursor(15,0);
lcd.print((unsigned int)ps2x.Analog(PSS_LY));
lcd.setCursor(18,0);
lcd.print(“3 “);
lcd.setCursor(19,0);
lcd.print((unsigned int)ps2x.Button(PSB_L3));

lcd.setCursor(0,1);
lcd.print(“X “);
lcd.setCursor(1,1);
lcd.print((unsigned int)ps2x.Analog(PSAB_CROSS));
lcd.setCursor(4,1);
lcd.print(“T “);
lcd.setCursor(5,1);
lcd.print((unsigned int)ps2x.Analog(PSAB_TRIANGLE));
lcd.setCursor(8,1);
lcd.print(“Sq “);
lcd.setCursor(10,1);
lcd.print((unsigned int)ps2x.Analog(PSAB_SQUARE));
lcd.setCursor(13,1);
lcd.print(“O “);
lcd.setCursor(14,1);
lcd.print((unsigned int)ps2x.Analog(PSAB_CIRCLE));
lcd.setCursor(17,1);
lcd.print(“R3 “);
lcd.setCursor(19,1);
lcd.print((unsigned int)ps2x.Button(PSB_R3));

lcd.setCursor(0,2);
lcd.print(“U “);
lcd.setCursor(1,2);
lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_UP));
lcd.setCursor(4,2);
lcd.print(“D “);
lcd.setCursor(5,2);
lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_DOWN));
lcd.setCursor(8,2);
lcd.print(“L “);
lcd.setCursor(10,2);
lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_LEFT));
lcd.setCursor(13,2);
lcd.print(“R “);
lcd.setCursor(14,2);
lcd.print((unsigned int)ps2x.Analog(PSAB_PAD_RIGHT));
lcd.setCursor(17,2);
lcd.print(“ST “);
lcd.setCursor(19,2);
lcd.print((unsigned int)ps2x.Button(PSB_START));

lcd.setCursor(0,3);
lcd.print(“L “);
lcd.setCursor(1,3);
lcd.print((unsigned int)ps2x.Analog(PSAB_L2));
lcd.setCursor(4,3);
lcd.print(“R “);
lcd.setCursor(5,3);
lcd.print((unsigned int)ps2x.Analog(PSAB_R2));
lcd.setCursor(8,3);
lcd.print(“L1 “);
lcd.setCursor(10,3);
lcd.print((unsigned int)ps2x.Analog(PSAB_L1));
lcd.setCursor(13,3);
lcd.print(“R “);
lcd.setCursor(14,3);
lcd.print((unsigned int)ps2x.Analog(PSAB_R1));
lcd.setCursor(17,3);
lcd.print(“Se “);
lcd.setCursor(19,3);
lcd.print((unsigned int)ps2x.Button(PSB_SELECT));
#endif
}
else
{
#ifdef PS2_DBG_SER
Serial.print(“rX “);
Serial.print((unsigned int)ps2x.Analog(PSS_RX));
Serial.print(“Y “);
Serial.print((unsigned int)ps2x.Analog(PSS_RY));
Serial.print(“lX “);
Serial.print((unsigned int)ps2x.Analog(PSS_LX));
Serial.print(“Y “);
Serial.print((unsigned int)ps2x.Analog(PSS_LY));
Serial.print(“3 “);
Serial.print((unsigned int)ps2x.Button(PSB_L3));

Serial.print(“X “);
Serial.print((unsigned int)ps2x.Analog(PSAB_CROSS));
Serial.print(“T “);
Serial.print((unsigned int)ps2x.Analog(PSAB_TRIANGLE));
Serial.print(“Sq “);
Serial.print((unsigned int)ps2x.Analog(PSAB_SQUARE));
Serial.print(“O “);
Serial.print((unsigned int)ps2x.Analog(PSAB_CIRCLE));
Serial.print(“R3 “);
Serial.print((unsigned int)ps2x.Button(PSB_R3));

Serial.print(“U “);
Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_UP));
Serial.print(“D “);
Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_DOWN));
Serial.print(“L “);
Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_LEFT));
Serial.print(“R “);
Serial.print((unsigned int)ps2x.Analog(PSAB_PAD_RIGHT));
Serial.print(“ST “);
Serial.print((unsigned int)ps2x.Button(PSB_START));

Serial.print(“L “);
Serial.print((unsigned int)ps2x.Analog(PSAB_L2));
Serial.print(“R “);
Serial.print((unsigned int)ps2x.Analog(PSAB_R2));
Serial.print(“L1 “);
Serial.print((unsigned int)ps2x.Analog(PSAB_L1));
Serial.print(“R “);
Serial.print((unsigned int)ps2x.Analog(PSAB_R1));
Serial.print(“Se “);
Serial.println((unsigned int)ps2x.Button(PSB_SELECT));
#endif
}
if(analogRead(A0) > 70)
{
lcd.print((__FlashStringHelper *)Exit);
while(analogRead(A0) > 70){};
digitalWrite(XBEESLEEP, HIGH);
break;
}
}
}
#endif
break;
case uSD:
if(menus == BACKMAIN)
{
menu=MAINSUB; ;
menulen=MAINLEN;
currmenu = MAIN;
menusel = 0;
}
#ifdef SD_ENABLE
else if (menus == SDPRES)
{
if (!SD.begin(SDCHIPSEL))
{
lcd.clear();
lcd.print((__FlashStringHelper *)Timeout);
}
else
{
lcd.print(F(“Card Found”));
}
delay(1000);
}
else if (menus == SDCNGI)
{//fileNum fileNum
//filename in array file
//idea is to write to array

//if Nunchuck present, use it to scroll up and down on menu
//Wire.beginTransmission(0x52);
//if( Wire.endTransmission() == 0)

nunchuk_init();
lcd.clear();
while(1)
{
nunchuk_send_request();
nunchuk_get_data();

lcd.home();
lcd.print(F(“Filename:File “));
lcd.setCursor(13,0);
if(nunchuk_joy_y() > 170)
fileNum++;
if(nunchuk_joy_y() 60)
{
lcd.setCursor(0,1);
//write changes
EEPROM.write(FILENAME_OFFSET, fileNum); //1 byte file name
convert_string(&file[4], fileNum);
file[8] = 0;
lcd.print(file);
lcd.print((__FlashStringHelper *)Exit);
while(analogRead(A0) > 70){};
break;

}
}
}
else if (menus == SDPREV)
{
//idea is to preview first page of file, or perhaps
//let you scroll with a attached joystick.
lcd.clear();

//show file name
lcd.print(file);

//load file
convert_string(&file[4], fileNum);
file[8] = 0; //make sure it is null terminated

//card present, open file
logFile = SD.open(file);
delay(1000);
if(logFile)
{
//logFile.println(“testing”);
int i = 0;
lcd.clear();
while(logFile.available())
{
lcd.print((char)logFile.read());
i++;
if(i >= 40)
{
if(analogRead(A0) > 60)
{
lcd.print((__FlashStringHelper *)Exit);
while(analogRead(A0) > 70){};
break;
}
delay(2000);
i=0;
}
}
logFile.close();
delay(2000);
}
}
#endif
break;
default:
break;
}

}

void loop()
{
uint8_t button;

lcd.clear();
//simple menu here
for(int i=0; i<HMENU && index[i+menu]-currmenu <= menulen ; i++)
{
lcd.setCursor(1,i);
lcd.print((__FlashStringHelper *)MenuTable[index[i+menu]]);

}

//draw select
lcd.setCursor(0,menusel);
lcd.write(SELECT);
#ifdef DEBUG
lcd.print((char)(menu + '0'));
lcd.print(' ');
lcd.print((char)(menusel + '0'));
#endif
button = buttonWait(A0,NO,YES);

if(button == BTND)
{
if(index[menu] – currmenu + HMENU-1 < menulen) //space to potentially go down
menu++;//increment menu
else if(index[menusel + menu] – currmenu 70)
{
lcd.print((char)temp);
lcd.print((__FlashStringHelper *)Exit);
while(analogRead(analogpin) > 70){};
return 0;
}
}
}
#endif
#ifdef XBEE_ENABLED
#ifndef BT_ENABLED
if
#else
else //XBEE HW serial
#endif
#endif
#ifdef XBEE_ENABLED
{
if(init){
//delay(1000);
lcd.print((__FlashStringHelper *)ppplus);
Serial.print((__FlashStringHelper *)ppplus);
if(initcr)
Serial.write(0x0D);
delay(1000);
}
else
state = 3;
while(state != 6)
{
if(state == 3)
{
state++;
lcd.print((__FlashStringHelper *)string);
Serial.print((__FlashStringHelper *)string);
Serial.write(0x0D);
delay(100);
}
if(Serial.available())
{
temp = Serial.read();
if(temp == 0x0D)
{
state++;
continue;
}
//lcd.print((char)(state + ‘0’));
if(temp == ‘O’)
state++;
if(temp == ‘K’)
state++;
//if(temp == ‘E’ || temp == ‘R’)
// return 0; //error
lcd.print((char)temp);
}
if(analogRead(analogpin) > 70)
{
lcd.print((char)temp);
lcd.print((__FlashStringHelper *)Exit);
while(analogRead(analogpin) > 70){};
return 0;
}
}
}
#endif
return 1;
}

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 = 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 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 PRESSHOLD)
{
offset = 10;
}
if( value == 0)
{
return parse_buttons(offset+init_butt);
}
}
}
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s