Initiation à Arduino

ARDUINO est un microcontroleur Open source qui peut servir à piloter un robot, une station météo ou à enregistrer des données dans une fusée;

Il comprend des entrées et des sorties analogiques et numériques, une mémoire et un microprocesseur. C'est un véritable ordinateur.

Il garde en mémoire le programme qu'on lui a associé, même sans jus. Dès qu'on va le brancher, il va ressortir son programme.


I- Exemple du clignotement d'une diode

int led = 13;

void setup() {

// initialize the digital pin as an output.

pinMode(led, OUTPUT); → la sortie led, donc la sortie 13 sera une sortie, pas une entrée

}

// the loop routine runs over and over again forever:

void loop() { équivalent du void draw sur Processing

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) → lumiere

delay(1000); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW → pas lumière

delay(1000); // wait for a second

}

Un robot fonctionne avec une boucle infinie, qui va lui permettre de réagir en fonction des données reçues par le port série ou par les entrées. Pour que l'action soit visible, il faut des actionneurs, reliés aux sorties des Arduino.


Amélioration du programme :

int led = 9; // the pin that the LED is attached to

int brightness = 0; // how bright the LED is

int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:

void setup() {

// declare pin 9 to be an output:

pinMode(led, OUTPUT);

}

void loop() {

// set the brightness of pin 9:

analogWrite(led, brightness);

// change the brightness for next time through the loop:

brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:

if (brightness == 0 || brightness == 255) {

fadeAmount = -fadeAmount ;

}

// wait for 30 milliseconds to see the dimming effect

delay(30);

}

Dans cet exemple plus complet, l'intensité lumineuse de la diode dépend de la variable brightness. Cette variabale change toutes les 30millisecondes à chaque tour de la fonction loop.


II- Communication série 

void setup() {

// Open serial communications and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for Leonardo only

}

// send an intro:

Serial.println("send any byte and I'll tell you everything I can about it");

Serial.println();

}

void loop() {

// get any incoming bytes:

if (Serial.available() > 0) {

int thisChar = Serial.read();

// say what was sent:

Serial.print("You sent me: \'");

Serial.write(thisChar); l'ordi va ecrire ce qui a été entré en lettre

Serial.print("\' ASCII Value: ");

Serial.println(thisChar); l'ordi va ecrire ce qui a été entré en code ASCII en chiffre

// analyze what was sent:

if(isAlphaNumeric(thisChar)) {

Serial.println("it's alphanumeric");

}

if(isAlpha(thisChar)) {

Serial.println("it's alphabetic");

}

if(isAscii(thisChar)) {

Serial.println("it's ASCII");

}

if(isWhitespace(thisChar)) {

Serial.println("it's whitespace");

}

if(isControl(thisChar)) {

Serial.println("it's a control character");

}

if(isDigit(thisChar)) {

Serial.println("it's a numeric digit");

}

if(isGraph(thisChar)) {

Serial.println("it's a printable character that's not whitespace");

}

if(isLowerCase(thisChar)) {

Serial.println("it's lower case");

}

if(isPrintable(thisChar)) {

Serial.println("it's printable");

}

if(isPunct(thisChar)) {

Serial.println("it's punctuation");

}

if(isSpace(thisChar)) { si c'est un espace qui est écrit, alors il va se passer :

Serial.println("it's a space character");

}

if(isUpperCase(thisChar)) {

Serial.println("it's upper case");

}

if (isHexadecimalDigit(thisChar)) {

Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)");

}

// add some space and ask for another byte:

Serial.println();

Serial.println("Give me another byte:");

Serial.println();

}

}

Créez votre site web gratuitement ! Ce site internet a été réalisé avec Webnode. Créez le votre gratuitement aujourd'hui ! Commencer