// 2009 mlab.uiah.fi/paja // Connecting to PC // PC <-> Arduino with Messenger library /* *** Protocal using Messenger Arduino library. Send following string from PC as a command. Each string needs to end with carrige return (13 in ASCII) - Read analog values: r a [a/d (pin)] [pin] (e.g. "r a d 7") - Read digital pins: r d [pin] (e.g. "r d 6") - Write digital pin: w d [pin] [value] (e.g. "w a 9") - Write analog pins: w a [pin] [value] (e.g. "w d 12") */ // download the library from following URL // arduino.cc/playground/Code/Messenger // // Copy the library folder following location // Arduino.app > (Show package contents) > // Contents > Resources > Java > hardware > libraries // Inport Messenger library #include // Instantiate Messenger object with the default separator // (the space character) Messenger message = Messenger(); ///*** Import additional library if needed ***/// // #include <***.h> ///*** Main functions ***/// void setup(){ // Start serial communication Serial.begin(9600); // Attach the callback function to the Messenger message.attach(messageReady); // Blink a Led three times when Arduino is ready to connect blinkLed(); blinkLed(); blinkLed(); } void loop(){ // The following line is the most effective way of using // Serial and Messenger's callback while (Serial.available()) message.process(Serial.read () ); } ///*** Write functions to read sensors and write actuators ***/// int readPing(int pin){ int input; pinMode(pin, OUTPUT); digitalWrite(pin, LOW); delayMicroseconds(2); digitalWrite(pin, HIGH); delayMicroseconds(5); digitalWrite(pin, LOW); pinMode(pin, INPUT); input = pulseIn(pin, HIGH); return input; } void writeAnalogLed(int pin, int value){ pinMode(pin, OUTPUT); value = map(value, 0, 400, 0, 255); analogWrite(pin, value); } ///*** Blinking a LED when you need to monitor ***/// void blinkLed(){ int pin = 13; pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); delay(100); digitalWrite(pin, LOW); delay(100); } ///*** Reading incoming values ***/// void messageReady(){ // Checks to see if the message is complete if (message.available()){ // read first char switch (message.readChar()){ case 'r': readValue(); break; case 'w': writeValue(); break; } } } void readValue(){ int value; int pin; switch (message.readChar()){ case 'd': // read boolean from digital pins pin = message.readInt(); pinMode(pin, INPUT); value = digitalRead(pin); break; case 'a': switch (message.readChar()){ // read integer from analog-in pins case 'a': pin = message.readInt(); value = analogRead(pin); break; // read integer from digital pins case 'd': pin = message.readInt(); switch (pin){ case 0: // Write code here for the device connected to pin 0 break; case 1: // Write code here for the device connected to pin 1 break; case 2: // Write code here for the device connected to pin 2 break; case 3: // Write code here for the device connected to pin 3 break; case 4: // Write code here for the device connected to pin 4 break; case 5: // Write code here for the device connected to pin 5 break; case 6: // Write code here for the device connected to pin 6 break; case 7: // Write code here for the device connected to pin 7 value = readPing(pin); break; case 8: // Write code here for the device connected to pin 8 break; case 9: // Write code here for the device connected to pin 9 break; case 10: // Write code here for the device connected to pin 10 break; case 11: // Write code here for the device connected to pin 11 break; case 12: // Write code here for the device connected to pin 12 break; case 13: // Write code here for the device connected to pin 13 break; } } } // send the value to PC followed by linefeed. Serial.println(value); } ///*** Writing outgoing values ***/// void writeValue(){ int pin; int value; switch (message.readChar()){ // write boolean to digital pins case 'd': pin = message.readInt(); pinMode(pin, OUTPUT); value = message.readInt(); digitalWrite(pin, value); break; // write integer to digital pins case 'a': pin = message.readInt(); value = message.readInt(); // Arduino NG (ATMEGA8 supports PWM only to pin 9, 10 and 11. switch(pin){ case 0: // Write code here for the device connected to pin 0 break; case 1: // Write code here for the device connected to pin 1 break; case 2: // Write code here for the device connected to pin 2 break; case 3: // Write code here for the device connected to pin 3 break; case 4: // Write code here for the device connected to pin 4 break; case 5: // Write code here for the device connected to pin 5 break; case 6: // Write code here for the device connected to pin 6 break; case 7: // Write code here for the device connected to pin 7 break; case 8: // Write code here for the device connected to pin 8 break; case 9: // Write code here for the device connected to pin 9 // PWM pin writeAnalogLed(pin, value); break; case 10: // Write code here for the device connected to pin 10 // PWM pin break; case 11: // Write code here for the device connected to pin 11 // PWM pin break; case 12: // Write code here for the device connected to pin 12 break; case 13: // Write code here for the device connected to pin 13 break; } } }