Advertisement
dustinrobotics

Control

Mar 26th, 2012
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.45 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Nunchuck.h>
  3.  
  4. /* Controller 2.0
  5.   Includes the following:
  6.   20x4 LCD
  7.   XBee Connected to pins 0 and 1 of ATMEGA328
  8.   BTM-182 connected to pins 7 and 7
  9.   PS2 connector
  10.   Nunchuck connector
  11.   microSD adapter (using old microSD to SD converter)
  12.  
  13.   Dustin Reynolds
  14.   2012-03-24
  15. */
  16. #include <SoftwareSerial.h>
  17. #include <ShiftRegLCD.h>
  18. #include <Buttons.h>
  19.  
  20.  
  21. #define HMENU 4
  22. #define WMENU 20
  23.  
  24. //#define DEBUG 1
  25.  
  26. ShiftRegLCD lcd(4, 3, 5, 4);  //we have a 20x4 LCD data clock enable #lines
  27.  
  28.  
  29. SoftwareSerial btser(7, 8);
  30.  
  31. //want simple XBEE config
  32. #define XBEESLEEP 6  //Pin 12 for XBee sleep control, goes to pin 9 on xbee
  33.  
  34. #define UPARROW                 ((uint8_t)3)
  35. #define DOWNARROW               ((uint8_t)4)
  36. #define SELECT                  ((uint8_t)5)
  37. #define HEART                   ((uint8_t)0)
  38. #define SMILE                   ((uint8_t)2)
  39. #define KEY                     ((uint8_t)1)
  40.  
  41. #define XBEECONF 0
  42. #define BTCONF   1
  43. #define CONNCPU  2
  44. #define CONNGAR  3
  45. #define CONNSAB  4
  46.  
  47. #define BACKMAIN 0
  48. #define XBEEPPP   1
  49. #define XBEESM1  2
  50. #define XBEECLOSE  3
  51.  
  52. #define BTPPP   1
  53. #define BTATR0  2
  54. #define BTATH   3
  55. #define BTATF   4
  56. #define BTATD   5
  57. #define BTATA   6
  58. #define BTCLOSE 7
  59. #define MAINSUB 0
  60. #define MAINLEN 5 //actual - 1
  61. const prog_char PROGMEM MenuStr1[] = "XBee Configure";
  62. const prog_char PROGMEM MenuStr2[] = "BT Configure";
  63. const prog_char PROGMEM MenuStr3[] = "NunChuck Configure";
  64. const prog_char PROGMEM MenuStr4[] = "Connect to CPU";
  65. const prog_char PROGMEM MenuStr5[] = "Connect to Garden";
  66. const prog_char PROGMEM MenuStr6[] = "Connect to Sable";
  67. #define XBEESUB 6 //starts at 2
  68.  
  69.  
  70. #define XBEELEN 3  //actual - 1
  71. const prog_char PROGMEM Xbee1[] = "Back to Main";
  72. const prog_char PROGMEM Xbee2[] = "+++";
  73. const prog_char PROGMEM Xbee3[] = "SM1,ST3e8,SO1";
  74. const prog_char PROGMEM Xbee4[] = "WN,CN";
  75. #define BTSUB 10
  76.  
  77. #define BTLEN 7 //actual - 1
  78. const prog_char PROGMEM BTser1[] = "Back to Main";
  79. const prog_char PROGMEM BTser2[] = "+++ <-not alneeded";
  80. const prog_char PROGMEM BTser3[] = "ATR0";
  81. const prog_char PROGMEM BTser4[] = "ATH (drops conn)";
  82. const prog_char PROGMEM BTser5[] = "ATF?";
  83. const prog_char PROGMEM BTser6[] = "ATD";
  84. const prog_char PROGMEM BTser7[] = "ATA";
  85. const prog_char PROGMEM BTser8[] = "ATO closes conn";
  86. //currentmenu
  87. #define MAIN   MAINSUB
  88. #define XBEE   XBEESUB
  89. #define BT     BTSUB
  90.  
  91. PGM_P PROGMEM MenuTable[] = {
  92.   MenuStr1,
  93.   MenuStr2,
  94.   MenuStr3,
  95.   MenuStr4,
  96.   MenuStr5,
  97.   MenuStr6,
  98.   Xbee1,
  99.   Xbee2,
  100.   Xbee3,
  101.   Xbee4,
  102.   BTser1,
  103.   BTser2,
  104.   BTser3,
  105.   BTser4,
  106.   BTser5,
  107.   BTser6,
  108.   BTser7,
  109.   BTser8,
  110. };
  111.  
  112. const prog_char PROGMEM ppplus[] = "+++";
  113. const prog_char PROGMEM sm1[] = "ATSM1,ST3E8,SO1";
  114. const prog_char PROGMEM wncn[] = "ATWN,CN";
  115. const prog_char PROGMEM atr0[] = "ATR0";
  116. const prog_char PROGMEM ath[] = "ATH";
  117. const prog_char PROGMEM atf[] = "ATF?";
  118. const prog_char PROGMEM atd[] = "ATD";
  119. const prog_char PROGMEM ata[] = "ATA";
  120. const prog_char PROGMEM ato[] = "ATO";
  121. const prog_char PROGMEM Timeout[] = "TimeOut";
  122. const prog_char PROGMEM Exit[] = "Exit";
  123.  
  124. byte Select[8]={  B01000,  B01100,  B00110,  B11111,  B11111,  B00110,  B01100,  B01000};
  125.  
  126. char menu = 0; //0 = main
  127. char menulen = MAINLEN;
  128. char menusel = 0;  //cursor
  129. char index[20];
  130. char currmenu = MAIN;  //current menu
  131.  
  132. int setup_ser(char ser, char * string, char init, char initcr,char justppp, unsigned long timeout, uint16_t analogpin);
  133.  
  134. void setup()
  135. {
  136.   //xbee
  137.   Serial.begin(19200);
  138.   pinMode(XBEESLEEP,OUTPUT); //6
  139.   digitalWrite(XBEESLEEP, HIGH);
  140.   //bt
  141.   pinMode (7,INPUT);
  142.   pinMode (8,OUTPUT);
  143.  
  144.   btser.begin(9600);
  145.  
  146.   lcd.createChar(5, Select);
  147.  
  148.   for(int i=0; i<20; i++)
  149.     index[i] = i;
  150.    
  151.   //Nunchuck
  152.     lcd.print("init starting");
  153.   nunchuk_init();
  154.   lcd.clear();
  155.   lcd.print("init finished");
  156.   delay(1000);
  157.   while(1)
  158.   {
  159.     //lcd.clear();
  160.     nunchuk_send_request();
  161.     nunchuk_get_data();
  162.     Serial.print("Z:");
  163.     Serial.print((unsigned int)nunchuk_zbutton());
  164.     Serial.print("C:");
  165.     Serial.print((unsigned int)nunchuk_cbutton());
  166.     //rest of data
  167.     Serial.print("Jx:");
  168.     Serial.print((unsigned int)nunchuk_joy_x());
  169.     //Serial.setCursor(12,0);
  170.     Serial.print("Jy:");
  171.     Serial.print((unsigned int)nunchuk_joy_y());
  172.     //Serial.setCursor(0,1);
  173.     //accel
  174.     Serial.print("AccX:");
  175.     Serial.print((unsigned int)nunchuk_accelx());
  176.     //Serial.setCursor(0,2);
  177.     Serial.print("AccY:");
  178.     Serial.print((unsigned int)nunchuk_accely());
  179.     //Serial.setCursor(0,2);
  180.     Serial.print("AccZ:");
  181.     Serial.println((unsigned int)nunchuk_accelz());
  182.     //Serial.setCursor(0,2);
  183.     delay(500);
  184.   }
  185.    
  186. }
  187.  
  188. //handle things here, not in loop
  189. void select(uint8_t cmenu, uint8_t menus)
  190. {
  191.   switch(cmenu)
  192.   {
  193.     case MAIN:
  194.       if(menus == XBEECONF)
  195.       {
  196.         menu=XBEESUB;
  197.         menulen=XBEELEN;
  198.         currmenu = XBEE;
  199.         digitalWrite(XBEESLEEP, LOW);
  200.       }
  201.       else if(menus == BTCONF)
  202.       {
  203.         menu=BTSUB;
  204.         menulen=BTLEN;
  205.         currmenu = BT;
  206.       }
  207.       else
  208.         menu = MAINSUB;
  209.       break;
  210.     case XBEE:
  211.       if(menus == BACKMAIN)
  212.       {
  213.         menu=MAINSUB; ;
  214.         menulen=MAINLEN;
  215.         currmenu = MAIN;
  216.         digitalWrite(XBEESLEEP, HIGH);
  217.       }
  218.       else if (menus == XBEEPPP)
  219.       {
  220.         //do code here, or move to other function?
  221.         lcd.clear();
  222.         setup_ser(XBEE,wncn,YES,NO,NO,10000,A0);
  223.         //delay(2000);
  224.       }
  225.       else if (menus == XBEECLOSE)
  226.       {
  227.         //do code here, or move to other function?
  228.         lcd.clear();
  229.         setup_ser(XBEE,wncn,YES,NO,NO,10000,A0);
  230.         //delay(2000);
  231.       }
  232.       else
  233.         menu = XBEESUB;
  234.       break;
  235.     case BT:
  236.       if(menus == BACKMAIN)
  237.       {
  238.         menu=MAINSUB;
  239.         menulen=MAINLEN;
  240.         currmenu = MAIN;
  241.       }
  242.       else if (menus == BTPPP)
  243.       {
  244.         //do code here, or move to other function?
  245.         lcd.clear();
  246.         setup_ser(BT,ppplus,YES,YES,YES,10000,A0);
  247.         //delay(2000);
  248.       }
  249.       else if (menus == BTATR0)
  250.       {
  251.         //do code here, or move to other function?
  252.         lcd.clear();
  253.         setup_ser(BT,atr0,NO,NO,NO,10000,A0);
  254.         //delay(2000);
  255.       }
  256.       else if (menus == BTATH)
  257.       {
  258.         //do code here, or move to other function?
  259.         lcd.clear();
  260.         setup_ser(BT,ath,NO,NO,NO,10000,A0);
  261.         //delay(2000);
  262.       }
  263.       else if (menus == BTATF)
  264.       {
  265.         //do code here, or move to other function?
  266.         lcd.clear();
  267.         setup_ser(BT,atf,NO,NO,NO,10000,A0);
  268.         //delay(2000);
  269.       }
  270.       else if (menus == BTATD)
  271.       {
  272.         //do code here, or move to other function?
  273.         lcd.clear();
  274.         setup_ser(BT,atd,NO,NO,NO,10000,A0);
  275.         //delay(2000);
  276.       }
  277.       else if (menus == BTATA)
  278.       {
  279.         //do code here, or move to other function?
  280.         lcd.clear();
  281.         setup_ser(BT,ata,NO,NO,NO,10000,A0);
  282.         //delay(2000);
  283.       }
  284.       else if (menus == BTCLOSE)
  285.       {
  286.         //do code here, or move to other function?
  287.         lcd.clear();
  288.         setup_ser(BT,ato,NO,NO,NO,10000,A0);
  289.         //delay(2000);
  290.       }
  291.       else
  292.         menu = BTSUB;
  293.       break;
  294.     default:
  295.       break;
  296.   }
  297.   menusel = 0;
  298. }
  299.  
  300. void loop()
  301. {
  302.   uint8_t button;
  303.  
  304.   lcd.clear();
  305.   //simple menu here
  306.   for(int i=0; i<HMENU && index[i+menu]-currmenu <= menulen ; i++)
  307.   {
  308.     lcd.setCursor(1,i);
  309.     lcd.print((__FlashStringHelper *)MenuTable[index[i+menu]]);
  310.    
  311.   }
  312.  
  313.   //draw select
  314.   lcd.setCursor(0,menusel);
  315.   lcd.write(SELECT);
  316. #ifdef DEBUG
  317.   lcd.print((char)(menu + '0'));
  318.   lcd.print(' ');
  319.   lcd.print((char)(menusel + '0'));
  320. #endif
  321.   button = buttonWait(A0,NO,YES);
  322.  
  323.   if(button == BTND)
  324.   {
  325.     if(index[menu] - currmenu + HMENU-1  < menulen)  //space to potentially go down
  326.       menu++;//increment menu
  327.     else if(index[menusel + menu] - currmenu < menulen)
  328.       menusel++;
  329.     else
  330.     {
  331.       //at bottom of menu, go back to top
  332.      menu = currmenu;
  333.      menusel = 0;
  334.     }
  335.   }
  336.   if(button == BTNB)
  337.     select(currmenu, index[menusel+menu]-currmenu);
  338. }
  339. int setup_ser(char ser, PGM_P PROGMEM string, char init, char initcr, char justppp, unsigned long timeout,uint16_t analogpin)
  340. {
  341.   char state=0, temp;
  342.   unsigned long time1 = millis();
  343.   if(ser == BT)
  344.   {
  345.   if(init){
  346.     //delay(1000);
  347.     lcd.print((__FlashStringHelper *)ppplus);
  348.     btser.print((__FlashStringHelper *)ppplus);
  349.     if(initcr)
  350.       btser.write(0x0D);
  351.     delay(1000);
  352.   }
  353.   else
  354.     state = 3;
  355.   while(1)
  356.   {
  357.     if(state == 3)
  358.     {
  359.       state++;
  360.       if(justppp)
  361.         continue;
  362.       lcd.print((__FlashStringHelper *)string);
  363.       btser.print((__FlashStringHelper *)string);
  364.       btser.write(0x0D);
  365.       delay(100);
  366.     }
  367.     if(btser.available())
  368.     {
  369.       temp = btser.read();
  370.       if(temp == 0x0D)
  371.       {
  372.         state++;
  373.         continue;
  374.       }
  375.       lcd.print((char)temp);
  376.       //lcd.print((char)(state + '0'));
  377.       if(temp == 'O')
  378.         state++;
  379.       if(temp == 'K')
  380.         state++;
  381.       //if(temp == 'E' || temp == 'R')
  382.       //  return 0; //error
  383.     }
  384.     if(analogRead(analogpin) > 70)
  385.     {
  386.       lcd.print((char)temp);
  387.       lcd.print((__FlashStringHelper *)Exit);
  388.       while(analogRead(analogpin) > 70){};
  389.       return 0;
  390.     }
  391.   }
  392.   }
  393.   else  //XBEE HW serial
  394.   {
  395.     if(init){
  396.     //delay(1000);
  397.     lcd.print((__FlashStringHelper *)ppplus);
  398.     Serial.print((__FlashStringHelper *)ppplus);
  399.     if(initcr)
  400.       Serial.write(0x0D);
  401.     delay(1000);
  402.   }
  403.   else
  404.     state = 3;
  405.   while(state != 6)
  406.   {
  407.     if(state == 3)
  408.     {
  409.       state++;
  410.       lcd.print((__FlashStringHelper *)string);
  411.       Serial.print((__FlashStringHelper *)string);
  412.       Serial.write(0x0D);
  413.       delay(100);
  414.     }
  415.     if(Serial.available())
  416.     {
  417.       temp = Serial.read();
  418.       if(temp == 0x0D)
  419.       {
  420.         state++;
  421.         continue;
  422.       }
  423.       //lcd.print((char)(state + '0'));
  424.       if(temp == 'O')
  425.         state++;
  426.       if(temp == 'K')
  427.         state++;
  428.       //if(temp == 'E' || temp == 'R')
  429.       //  return 0; //error  
  430.       lcd.print((char)temp);
  431.     }
  432.     if(analogRead(analogpin) > 70)
  433.     {
  434.       lcd.print((char)temp);
  435.       lcd.print((__FlashStringHelper *)Exit);
  436.       while(analogRead(analogpin) > 70){};
  437.       return 0;
  438.     }
  439.   }
  440.   }
  441.  return 1;
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement