diff --git a/arduino.pde b/arduino.pde index 51cb859..7d10134 100644 --- a/arduino.pde +++ b/arduino.pde @@ -6,146 +6,167 @@ int cmd_arg[2]; int serialStatus = 0; -void setup() { - // connect to the serial port - Serial.begin(115200); - setupPins(); - serialStatus = 1; +void setup() +{ + // connect to the serial port + Serial.begin(115200); + // setup the output and input pins + setupPins(); + serialStatus = 1; } void loop() { - if(serialStatus==0) - { - Serial.flush(); - setupPins(); - } - askCmd(); - - { - if(Serial.available()>0) - { - cmd = int(Serial.read()) - 48; - - if(cmd==0) //set digital low - { - cmd_arg[0] = int(readData()) - 48; - digitalWrite(cmd_arg[0],LOW); - } - - if(cmd==1) //set digital high - { - cmd_arg[0] = int(readData()) - 48; - digitalWrite(cmd_arg[0],HIGH); - } - - if(cmd==2) //get digital value - { - cmd_arg[0] = int(readData()) - 48; - cmd_arg[0] = digitalRead(cmd_arg[0]); - Serial.println(cmd_arg[0]); - } - - if(cmd==3) // set analog value - { - Serial.println("I'm in the right place"); - cmd_arg[0] = int(readData()) - 48; - cmd_arg[1] = readHexValue(); - analogWrite(cmd_arg[0],cmd_arg[1]); - } - - if(cmd==4) //read analog value - { - cmd_arg[0] = int(readData()) - 48; - cmd_arg[0] = analogRead(cmd_arg[0]); - Serial.println(cmd_arg[0]); - } - - if(cmd==5) + if(serialStatus == 0) + { + Serial.flush(); + // setup the output and input pins + setupPins(); + serialStatus = 1; + } + askCmd(); + + + + { + if(Serial.available()>0) { - serialStatus = 0; + cmd = Serial.read(); + + if(cmd == 0) //set digital low + { + cmd_arg[0] = readData(); + digitalWrite(cmd_arg[0],LOW); + } + else if(cmd == 1) //set digital high + { + cmd_arg[0] = readData(); + digitalWrite(cmd_arg[0],HIGH); + } + else if(cmd == 2) //get digital value + { + cmd_arg[0] = readData(); + cmd_arg[0] = digitalRead(cmd_arg[0]); + Serial.write(cmd_arg[0]); + } + else if(cmd == 3) // set analog value + { + Serial.println("I'm in the right place"); + cmd_arg[0] = readData(); + cmd_arg[1] = readHexValue(); + analogWrite(cmd_arg[0],cmd_arg[1]); + } + else if(cmd == 4) //read analog value + { + cmd_arg[0] = readData(); + cmd_arg[0] = analogRead(cmd_arg[0]); + Serial.println(cmd_arg[0]); + } + else if(cmd == 5) + { + serialStatus = 0; + } + else + { + // invalid command + } } - } - } + } } char readData() { - askData(); - - while(1) - { - if(Serial.available()>0) - { - return Serial.read(); - } - } + askData(); + + while(1) + { + if(Serial.available() > 0) + { + return Serial.read(); + } + } } //read hex value from serial and convert to integer int readHexValue() { - int strval[2]; - int converted_str; - - while(1) - { - if(Serial.available()>0) - { - strval[0] = convert_hex_to_int(Serial.read()); - break; - } - } + int strval[2]; + int converted_str; - askData(); + while(1) + { + if(Serial.available() > 0) + { + strval[0] = convert_hex_to_int(Serial.read()); + break; + } + } - while(1) - { - if(Serial.available()>0) - { - strval[1] = convert_hex_to_int(Serial.read()); - break; - } - } + askData(); + + while(1) + { + if(Serial.available() > 0) + { + strval[1] = convert_hex_to_int(Serial.read()); + break; + } + } - converted_str = (strval[0]*16) + strval[1]; - return converted_str; + converted_str = (strval[0]*16) + strval[1]; + return converted_str; } int convert_hex_to_int(char c) { - return (c <= '9') ? c-'0' : c-'a'+10; + return (c <= '9') ? c-'0' : c-'a'+10; } void askData() { - Serial.println("?"); + Serial.println("?"); } void askCmd() { - askData(); - while(Serial.available()<=0) - {} + askData(); + while(Serial.available() <= 0) + {} } void setupPins() { - while(Serial.available()<1) - { - // get number of output pins and convert to int - cmd = int(readData()) - 48; - for(int i=0; i