// This example demonstrates two-way serial communication. When a character // '2'...'7' is received, a led on the corresponding digital i/o pin (2-7) // lights up. Meanwhile, values read from a potentiometer connected to // analog input pin 0 are sent out over the serial line. // NOTE: we cannot use leds on pins 0 and 1 while using the serial line, // since these pins double as the serial receive (Rx) and transmit (Tx) pins. #define pinPot 0 // analog input pin with potentiometer int pinLed; // remembers which led is "on" void setup() { for (pinLed=2; pinLed<8; pinLed++) { // set digital pins 2-7 as outputs pinMode(pinLed, OUTPUT); digitalWrite(pinLed, LOW); // and set them "off" } Serial.begin(9600); // start serial comm at 9600 bits/sec } void loop() { // handle incoming serial data if (Serial.available() > 0) { // if new incoming serial data available digitalWrite(pinLed, LOW); // switch "off" the pin that was "on" pinLed = map(Serial.read(),'2','7',2,7); // read char from serial line and map it to pin number digitalWrite(pinLed, HIGH); // switch pin h "on" } // handle outgoing serial data int v = analogRead(pinPot); // read value v [0-1023] from analog input pin v = map(v,0,1023,0,255); // convert v from [0-1023] to [0-255] (could also do "v/=4;") Serial.write(v); // send v [0-255] to serial port delay(100); }