ArduCitrouille
De Nybi.cc
Révision de 2 novembre 2013 à 19:48 par Loic.fejoz (discussion | contributions)
Exemple
Voir la vidéo : ardu-citrouillev0
Programme
// Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> #include <Servo.h> #include <stdarg.h> void debug(char *fmt, ... ){ char tmp[128]; // resulting string limited to 128 chars va_list args; va_start (args, fmt ); vsnprintf(tmp, 128, fmt, args); va_end (args); Serial.print(tmp); } Servo myservo; // create servo object to control a servo int debattement = 35; int target_pos = 90; int current_pos = target_pos - debattement; int delta_pos = 5; void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(10, OUTPUT); pinMode(11, OUTPUT); Serial.begin(9600); Serial.println("Hello world"); delay(2000);// Give reader a chance to see the output. } void loop() { current_pos = current_pos + delta_pos; if (current_pos >= target_pos) { target_pos = 90 - debattement; } if (current_pos < target_pos) { target_pos = 90 + debattement; } delta_pos = (target_pos > current_pos) ? 1 : -1; if (delta_pos > 0) { digitalWrite(10, LOW); digitalWrite(11, HIGH); } else { digitalWrite(10, HIGH); digitalWrite(11, LOW); } debug("%d looking at %d by step %d\n", current_pos, target_pos, delta_pos); myservo.write(current_pos); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }