/* * This is the Second Digital Output sketch for Uno * It lets you control the turn a digial output on and off from your Android mobile using pfodApp * It is modification of the standard BLINK example * *(c)2013 Forward Computing and Control Pty. Ltd. * This code may be freely used for both private and commerical use. * Provide this copyright is maintained. */ #include #include // include the library pfodParser parser; // create the cmd parser // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; // the setup routine runs once when you press reset: void setup() { Serial.begin(9600); for (int i=3; i>0; i--) { // wait a few secs to see if we are being programmed delay(1000); } // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { byte in = 0; byte cmd = 0; if (Serial.available()) { in = Serial.read(); // read the next char cmd = parser.parse(in); // pass it to the parser returns non-zero when a command is fully parsed if (cmd != 0) { // have parsed a complete msg { to } if ('.' == cmd) { // pfodApp sent {.} it is asking for the main menu // the complete messages is {.Turn Led On or Off||o~Turn Led ... } Serial.print(F("{.Turn Led On or Off|o~Turn Led ")); // here insert the appropiate word ON or OFF if (digitalRead(led) == LOW) { // next click will turn it on Serial.print(F("ON")); } else { // next click will turn it off Serial.print(F("OFF")); } // finally close the message with } Serial.print(F("}")); } else if ('o' == cmd) { // pfodApp sent {o} i.e. user clicked on On/Off menu item // note o was the cmd associated with the Toggle Led menu item above if (digitalRead(led) == LOW) { digitalWrite(led,HIGH); // was off turn on // now update the menu with the menu text Serial.print(F("{:|o~Turn Led OFF}")); } else { digitalWrite(led,LOW); // was on turn off // now update the menu with the menu text Serial.print(F("{:|o~Turn Led ON}")); } // you can add more command handlers here match the cmd to the menu cmd above } else { // don't recongnize this command just ignore and return empty response; Serial.print(F("{}")); // otherwise pfodApp times out and disconnects } } cmd = 0; // have processed this cmd now // so clear it and wait for next one } // else no serial chars just loop }