alberthrocks

Arduino Test Code

Jul 21st, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.47 KB | None | 0 0
  1. /*
  2.  * This code communicates with the computer to get a date, then starts
  3.  * counting down from 3 when a button is pressed.
  4.  */
  5.  
  6. // include the library code:
  7. #include <Wire.h>
  8. #include <Adafruit_MCP23017.h>
  9. #include <Adafruit_RGBLCDShield.h>
  10. #include <stdlib.h>
  11.  
  12. // The shield uses the I2C SCL and SDA pins. On classic Arduinos
  13. // this is Analog 4 and 5 so you can't use those for analogRead() anymore
  14. // However, you can connect other I2C sensors to the I2C bus and share
  15. // the I2C bus.
  16. Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
  17.  
  18. // These #defines make it easy to set the backlight color
  19. #define RED 0x1
  20. #define YELLOW 0x3
  21. #define GREEN 0x2
  22. #define BLUE 0x4
  23. #define WHITE 0x7
  24.  
  25. uint8_t i=0;
  26. bool serial_init = false;
  27. char msg[300];
  28. char tmpchar;
  29.  
  30. void setup() {
  31.   // Debugging output
  32.   Serial.begin(9600);
  33.   // set up the LCD's number of rows and columns:
  34.   lcd.begin(16, 2);
  35.  
  36.   // Print a message to the LCD. We track how long it takes since
  37.   // this library has been optimized a bit and we're proud of it :)
  38.   int time = millis();
  39.   lcd.print("Hello, world!");
  40.   time = millis() - time;
  41.   Serial.print("Took "); Serial.print(time); Serial.println(" ms");
  42.   Serial.println("Ready for input.");
  43.   delay(2000);
  44.   //Serial.print("> ");
  45.   lcd.setBacklight(BLUE);
  46.   lcd.clear();
  47.  
  48.   // Allocate some memory
  49.   //msg = (char*) malloc(1);
  50.   //msg[0] = '\0';
  51. }
  52.  
  53. void loop() {
  54.   // set the cursor to column 0, line 1
  55.   // (note: line 1 is the second row, since counting begins with 0):
  56.   lcd.setCursor(0, 0);
  57.   // print the number of seconds since reset:
  58.   lcd.print(millis());
  59.   //Serial.print("Time tick: "); Serial.print(millis()/1000); Serial.println(" sec");
  60.  
  61.   strcpy(msg, "");
  62.   /* Check for packet start character */
  63.   //lcd.print("Reading serial.");
  64.   char initial_char = Serial.read();
  65.   if (initial_char == '!') {
  66.     lcd.setCursor(0, 1);
  67.     //lcd.print("Got input.");
  68.     bool end_of_command = false;
  69.     while (!end_of_command) {
  70.       while (Serial.available()>0){  
  71.             tmpchar=Serial.read();
  72.             if (tmpchar != '\n') {
  73.               //msg = (char*) malloc(strlen(p) + 1);
  74.               strcat(msg, (const char*)tmpchar);
  75.               Serial.print(tmpchar);
  76.             } else {
  77.               Serial.print('\n');
  78.               end_of_command = true;
  79.               break;
  80.             }
  81.       }
  82.     }
  83.     lcd.setCursor(0, 1);
  84.     lcd.print("            ");
  85.   }
  86.   Serial.println("Checkpoint 1");
  87.   //Serial.print("final msg is ");
  88.   //Serial.println(msg);
  89.   //if (msg != "")
  90.   //  delay(3000);
  91.  
  92.   /* Init an array for 15 arguments max! */
  93.   char command_array[15][20] = {NULL};
  94.   char* p;
  95.   char command[20];
  96.   int i = 0;
  97.  
  98.   p = strtok(msg, " ");
  99.   Serial.println("Checkpoint 2");
  100.   while (p)
  101.   {
  102.     //command_array[i] = (char*)malloc(strlen(p) + 1);
  103.     strcpy(command_array[i++], p);
  104.     p = strtok(NULL, " ");
  105.   }
  106.   Serial.println("Checkpoint 3");
  107.   if (command_array[0]) {
  108.     Serial.println(" - Command found!");
  109.     Serial.print(" - Command is: ");
  110.     Serial.println(command_array[0]);
  111.     strcpy(command, command_array[0]);
  112.   } else {
  113.     Serial.println(" - No command found!");
  114.     strcpy(command, "NO_COMMAND");
  115.   }
  116.   Serial.println("Checkpoint 4");
  117.   //Serial.print("command is ");
  118.   //Serial.println(command);
  119.   /* Did we get a signal from the PC to start serial comm? */
  120.   if (strcmp(command, "SERIAL_INIT")) {
  121.     lcd.setCursor(0, 0);
  122.     lcd.print("Received serial");
  123.     lcd.setCursor(0, 1);
  124.     lcd.print("init signal!");
  125.     Serial.println("SERIAL_INIT_ACK");
  126.   /* Did we get a PING command from the PC for the handshake? */
  127.   } else if (strcmp(command, "PINGC")) {
  128.     lcd.clear();
  129.     lcd.setCursor(0, 0);
  130.     if (sizeof(command_array) == 2) {
  131.       lcd.print("Doing handshake!");
  132.       Serial.print("PONGC ");
  133.       Serial.println(command_array[1]);
  134.     } else {
  135.       lcd.print("Got invalid");
  136.       lcd.setCursor(0, 1);
  137.       lcd.print("clnt handshake!");
  138.       Serial.println("ERRPONGC");
  139.     }
  140.   }
  141.   Serial.println("Checkpoint 5");
  142.  
  143.   uint8_t buttons = lcd.readButtons();
  144.  
  145.   if (buttons) {
  146.     lcd.clear();
  147.     lcd.setCursor(0,0);
  148.     if (buttons & BUTTON_UP) {
  149.       lcd.print("UP ");
  150.       lcd.setBacklight(RED);
  151.     }
  152.     if (buttons & BUTTON_DOWN) {
  153.       lcd.print("DOWN ");
  154.       lcd.setBacklight(YELLOW);
  155.     }
  156.     if (buttons & BUTTON_LEFT) {
  157.       lcd.print("LEFT ");
  158.       lcd.setBacklight(GREEN);
  159.     }
  160.   }
  161.   Serial.println("Checkpoint 6");
  162. }
Advertisement
Add Comment
Please, Sign In to add comment