/* Ce sketch reprend en partie les éléments de l'exemple Knob de Arduino Controlling a servo position using a potentiometer (variable resistor) by Michal Rinott modified on 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Knob */ #include Servo myservo; // create servo object to control a servo int attachPin = 9; // Le servo est attaché à la pin 9 char msgString[10]; // Tableau qui va recevoir les caractères envoyés int cur = 1500; // Position initiale du servo (µs) int val = 1500; // Valeur à atteindre pour le servo (µs) int del = 0; // Temporisation (ms) void receipt() { // Lecture du port série char c; while (Serial.available()) { c = Serial.read(); if (c == '<') { // Caractère délimiteur de début de message sprintf(msgString, ""); } else if (c == '>') { // Caractère délimiteur de fin de message parse(msgString); } else { // Caractères du message sprintf(msgString, "%s%c", msgString, c); } } } void parse(char *msgString) { sscanf(msgString, "%d%d", &val, &del); // On copie les valeurs reçues dans les varables val et del } void process() { if (val > cur) { for (cur; val >= cur; cur++) { myservo.writeMicroseconds(cur); // (µs) delay(del); // (ms) } } else if (val < cur) { for (cur; val <= cur; cur--) { myservo.writeMicroseconds(cur); // (µs) delay(del); // (ms) } } } void setup() { Serial.begin(115200); myservo.attach(attachPin); // attaches the servo on pin 9 to the servo object myservo.writeMicroseconds(cur); // On place le servo à sa position de départ delay(200); } void loop() { receipt(); process(); }