Codes
Retrouvez ici mes différents scripts et morceaux de code.
#include <Servo.h> // librairie de controle des servos
Servo servo; // creation d'un objet servo
int byte_read = 0;
int coords;
int last = 0;
int angle[] = {60, 92}; // angle min et max
int button = 0;
long time = 0;
long debounce = 2000;
void setup() {
pinMode(7, INPUT); // bouton poussoir
Serial.begin(9600);
}
boolean is_a_number(int n)
{
return n >= 48 && n <= 57;
}
int ascii2int(int byte_read)
{
return byte_read - 48;
}
void loop() {
while(Serial.available() > 0)
{
byte_read = Serial.read();
if (is_a_number(byte_read))
{
coords = ascii2int(byte_read);
}
}
button = digitalRead(7); // lecture de l'etat du bouton poussoir
Serial.println(coords); // affichage de l'etat de l'interrupteur
if(coords != last || button == 1 && millis() - time > debounce)
{
button = 0;
time = millis();
if(last == 0)
{
servo.attach(9); // on connecte le servo quand on en a besoin
servo.write(angle[0]);
last = 1;
coords = 1;
delay(500);
servo.detach(); // on deconnecte le servo pour ne pas qu'il force
}
else if(last == 1)
{
servo.attach(9);
servo.write(angle[1]);
last = 0;
coords = 0;
delay(500);
servo.detach();
}
}
delay(1000);
}