Arduino…Arduino…

Arduino Duemilanove

Hors donc, un collègue a organisé un cours pour apprendre à programmer les cartes à micro-controleur de type Arduino. Une commande de groupe de kits de départ comprenant un Arduino Duemilanove avait été organisée. Ce kits comprennent une carte dotée d’une micro-processeur ATMEGA328, d’une Platine Labdec (Breadboard en Anglais) d’une collection de fils de connections et de LEDs. Avec ce nouveau jouet, j’ai passé quelques heures à bricoler. Ma dernière expérience de programmation de microcontrôleur datait de l’université, et je dois dire que les kits d’aujourd’hui sont beaucoup plus pratiques à utiliser. À l’époque il fallait programmer les EEPROM en assembleur, puis charger le programme après les avoir effacées aux ultra-violets. La carte Arduino dispose d’une port USB avec une puce qui émule un port série. Cela signifie que l’ont peut charger directement le programme depuis l’IDE sur la mémoire flash du processeur, et communiquer avec celui-ci via un port série. Alors qu’il fallait facilement un quart d’heure pour tester un programme, il faut à présent quelques secondes.

La carte comporte de nombreuses entrées sorties, mais aussi une LED intégrée qui peut être contrôlée, on peut donc parfaitement travailler avec un laptop, un câble USB et la carte. J’ai donc pu continuer à coder dans le train entre Zürich et Genève. J’ai profité du trajet pour écrire un petit programme qui traduit le texte entré sur le port sériel en morse. Voici le résultat:

So a colleague of mine gave a course on how to program an Arduino boards. For this course, a bunch of starter kits with an Arduino Duemilanove were ordered. The kit included a board equipped with a ATMEGA328 processor, a breadboard and set of cables and LEDs. With this new toy, I spent a few hours building stuff. My last experience with micro-controller programming was at the university, and I must say that today’s kit a way easier to use. Back-then we had to program the EEPROM in assembly, load them with the code after having erased them with ultra-violet light. The Arduino board has a USB port and a chip that emulates a serial port, this means the program can be loaded directly from the IDE into the flash memory and the one can communicate with the program by the way of the serial port. It took a quarter of an hour to test some code, now only a few seconds are needed.

The board includes various input/ouputs, but also an onboard LED that can be controlled, so it is perfectly possible to work just with a laptop, a USB cable and the board. So I was able to do keep coding while riding the train between Zürich and Geneva. I took the opportunity to write a small program that translates the text send to the serial port into morse code. He is the result:

const int ledPin = 13;
const int shortDelay = 200;
const int longDelay = shortDelay * 3;
const int spaceDely = longDelay * 2;

const char* charTable[] = {
  ".-", "-...", "-.-.", "-..",  ".", "..-.",  "--.", "....",  "..", ".---",  "-.-", // K
  ".-..", "--",  "-.", "---", ".--.", "--.-", "-.-", "...", "-", "..-", "...-", ".--",
  "-..-", "-.--",  "--..",
};
const char* numberTable[] = {
  "-----", ".----", "..---", "...--", "....-", ".....",
  "-....",  "--...", "---..", "----.",  "-----",
};

void ditChar(char c) {
  if (c == '.') {
    digitalWrite(ledPin, HIGH);
    delay(shortDelay);
  }
  if (c == '-') {
    digitalWrite(ledPin, HIGH); 
    delay(longDelay);
  }
  if (c == ' ') {
    delay(longDelay);
  }
  digitalWrite(ledPin, LOW);
  delay(shortDelay);
}

void ditString(const char* string) {
  while(*string) {
    ditChar(*string++);
  }
  delay(longDelay);
}

void morse(char c) {
  if (c >= 'A' && c< = 'Z') {
    ditString(charTable[c - 'A']);
  } else if (c >= 'a' && c < = 'z') {
    ditString(charTable[c - 'a']);
  } else if (c >= '0' && c < = '9') {
    ditString(numberTable[c - '0']);
  } else {
    ditChar(c); 
  }
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  char c = Serial.read();
  if (c >= 0) {
    morse(c);
    Serial.write(c);
  }
}

4 thoughts on “Arduino…Arduino…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.