Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This code communicates with the computer to get a date, then starts
- * counting down from 3 when a button is pressed.
- */
- // include the library code:
- #include <Wire.h>
- #include <Adafruit_MCP23017.h>
- #include <Adafruit_RGBLCDShield.h>
- #include <stdlib.h>
- // The shield uses the I2C SCL and SDA pins. On classic Arduinos
- // this is Analog 4 and 5 so you can't use those for analogRead() anymore
- // However, you can connect other I2C sensors to the I2C bus and share
- // the I2C bus.
- Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
- // These #defines make it easy to set the backlight color
- #define RED 0x1
- #define YELLOW 0x3
- #define GREEN 0x2
- #define BLUE 0x4
- #define WHITE 0x7
- uint8_t i=0;
- bool serial_init = false;
- char msg[300];
- char tmpchar;
- void setup() {
- // Debugging output
- Serial.begin(9600);
- // set up the LCD's number of rows and columns:
- lcd.begin(16, 2);
- // Print a message to the LCD. We track how long it takes since
- // this library has been optimized a bit and we're proud of it :)
- int time = millis();
- lcd.print("Hello, world!");
- time = millis() - time;
- Serial.print("Took "); Serial.print(time); Serial.println(" ms");
- Serial.println("Ready for input.");
- delay(2000);
- //Serial.print("> ");
- lcd.setBacklight(BLUE);
- lcd.clear();
- // Allocate some memory
- //msg = (char*) malloc(1);
- //msg[0] = '\0';
- }
- void loop() {
- // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- lcd.setCursor(0, 0);
- // print the number of seconds since reset:
- lcd.print(millis());
- //Serial.print("Time tick: "); Serial.print(millis()/1000); Serial.println(" sec");
- strcpy(msg, "");
- /* Check for packet start character */
- //lcd.print("Reading serial.");
- char initial_char = Serial.read();
- if (initial_char == '!') {
- lcd.setCursor(0, 1);
- //lcd.print("Got input.");
- bool end_of_command = false;
- while (!end_of_command) {
- while (Serial.available()>0){
- tmpchar=Serial.read();
- if (tmpchar != '\n') {
- //msg = (char*) malloc(strlen(p) + 1);
- strcat(msg, (const char*)tmpchar);
- Serial.print(tmpchar);
- } else {
- Serial.print('\n');
- end_of_command = true;
- break;
- }
- }
- }
- lcd.setCursor(0, 1);
- lcd.print(" ");
- }
- Serial.println("Checkpoint 1");
- //Serial.print("final msg is ");
- //Serial.println(msg);
- //if (msg != "")
- // delay(3000);
- /* Init an array for 15 arguments max! */
- char command_array[15][20] = {NULL};
- char* p;
- char command[20];
- int i = 0;
- p = strtok(msg, " ");
- Serial.println("Checkpoint 2");
- while (p)
- {
- //command_array[i] = (char*)malloc(strlen(p) + 1);
- strcpy(command_array[i++], p);
- p = strtok(NULL, " ");
- }
- Serial.println("Checkpoint 3");
- if (command_array[0]) {
- Serial.println(" - Command found!");
- Serial.print(" - Command is: ");
- Serial.println(command_array[0]);
- strcpy(command, command_array[0]);
- } else {
- Serial.println(" - No command found!");
- strcpy(command, "NO_COMMAND");
- }
- Serial.println("Checkpoint 4");
- //Serial.print("command is ");
- //Serial.println(command);
- /* Did we get a signal from the PC to start serial comm? */
- if (strcmp(command, "SERIAL_INIT")) {
- lcd.setCursor(0, 0);
- lcd.print("Received serial");
- lcd.setCursor(0, 1);
- lcd.print("init signal!");
- Serial.println("SERIAL_INIT_ACK");
- /* Did we get a PING command from the PC for the handshake? */
- } else if (strcmp(command, "PINGC")) {
- lcd.clear();
- lcd.setCursor(0, 0);
- if (sizeof(command_array) == 2) {
- lcd.print("Doing handshake!");
- Serial.print("PONGC ");
- Serial.println(command_array[1]);
- } else {
- lcd.print("Got invalid");
- lcd.setCursor(0, 1);
- lcd.print("clnt handshake!");
- Serial.println("ERRPONGC");
- }
- }
- Serial.println("Checkpoint 5");
- uint8_t buttons = lcd.readButtons();
- if (buttons) {
- lcd.clear();
- lcd.setCursor(0,0);
- if (buttons & BUTTON_UP) {
- lcd.print("UP ");
- lcd.setBacklight(RED);
- }
- if (buttons & BUTTON_DOWN) {
- lcd.print("DOWN ");
- lcd.setBacklight(YELLOW);
- }
- if (buttons & BUTTON_LEFT) {
- lcd.print("LEFT ");
- lcd.setBacklight(GREEN);
- }
- }
- Serial.println("Checkpoint 6");
- }
Advertisement
Add Comment
Please, Sign In to add comment