commande-serie.ino

Arduino Duemilanove — Board Fiche · File : commande-serie.ino · Language : Arduino · Use case : Allumer ou éteindre des LED par commandes tapées sur le port série

The serial monitor sends characters the board interprets: the first step toward control from a terminal, serial Bluetooth or a second microcontroller. The Duemilanove exposes the port through its FTDI bridge.

Hardware required

An Arduino Duemilanove (ATmega168 or 328P, 16 MHz, 5 V) with its mini-USB-B cable; the serial port appears once the FTDI driver is installed. 2 LEDs + 2×330 Ω.

Wiring

LED1 on 13, LED2 on 12 (anode via resistor), cathodes to GND.

How it works

Serial.available flags a pending byte; a simple switch on the value drives the pins. In the Arduino IDE, select « Arduino Duemilanove » (or « Diecimila » for very old boards) and the FTDI port, then Upload. The ATmega168 leaves only 14 KB for the sketch.

Going further

Extend to multi-character commands (« lum 200 ») by accumulating into a newline-terminated string.

commande-serie.ino

// Commande serie : '1'/'0' LED 13, '2'/'3' LED 12.
const int LED1 = 13, LED2 = 12;

void setup() {
  pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("Tapez 1/0 (LED13) ou 2/3 (LED12)"));
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c == '1') digitalWrite(LED1, HIGH);
    if (c == '0') digitalWrite(LED1, LOW);
    if (c == '2') digitalWrite(LED2, HIGH);
    if (c == '3') digitalWrite(LED2, LOW);
  }
}

← Back to the board