/* * This is the First 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~Toggle Led} Serial.print(F("{." // {. msg tells pfodApp to display a menu screen "Turn Led On or Off" // this is the title of the screen "|o~Toggle Led" // this is the menu, cmd is o text is Toggle Led // you can add more menu items here "}")); // this finishes the msg } 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 boolean ledState = digitalRead(led); if (ledState == LOW) { digitalWrite(led,HIGH); // was off turn on } else { digitalWrite(led,LOW); // was on turn off } Serial.print(F("{}")); // always return a response otherwise pfodApp times out and disconnects // 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 }