/*attiny servo cde aiguillage
07/08/2022
Commande de servo-moteur pour aiguillage avec ATtiny85
liens: https://forum.locoduino.org/index.php?topic=1437.0
https://wokwi.com/projects/424784448736080897
Brochage ATtiny85
=|1 U 8|= VCC
Led1 <--3 =|2 7|= 2--> PinServo
Led2 <--4 =|3 6|= 1<-- BP2
GND =|4 5|= 0<-- BP1
*/
const byte BP1 = 0 ; // cde le servo en position travail
const byte BP2 = 1 ; // cde le servo en position repos
const byte PinServo = 2; // signal de commande du servo
const byte Led1 = 3;// allumée si servo en position repos
const byte Led2 = 4;// allumée si servo en position travail
const byte PAS = 5; // agit sur la vitesse de rotation du servo
int periode = 20000;// période entre chaque début d'impulsion en microsecondes
int PO = 1550; // durée impulsion en microsecondes pour la position repos
int PF = 1250; // durée impulsion en microsecondes pour la position travail
int t1;
int t2;
byte a = 0;
void setup() {
pinMode(BP1, INPUT_PULLUP); // broche en entrée avec pullup
pinMode(BP2, INPUT_PULLUP); // broche en entrée avec pullup
pinMode(Led1, OUTPUT); // broche en sortie
pinMode(Led2, OUTPUT); // broche en sortie
digitalWrite(Led1, LOW); // Led1 éteinte
digitalWrite(Led2, LOW); // Led2 éteinte
pinMode(PinServo, OUTPUT); // signal en sortie
digitalWrite(PinServo, LOW); // on l'initialise à l'état bas
t1 = PO;
t2 = PF;
// initialise en aiguillage repos
digitalWrite(PinServo, HIGH); // on envoie l'impulsion
delayMicroseconds(t2); // pendant la bonne durée
digitalWrite(PinServo, LOW); // on stoppe l'impulsion
delayMicroseconds(periode - t2); // on attend le temps restant pour atteindre la période
digitalWrite(Led1, HIGH); // Led1 allumée
digitalWrite(Led2, LOW); // Led2 éteinte
}// fin setup
void loop() {
// Récupérer valeur des entrées de commande
bool etatBP1 = digitalRead(BP1);
bool etatBP2 = digitalRead(BP2);
// Cde aiguillage position travail par BP1
if (etatBP1 == LOW && t2 == PF) {
t1 = PF - PAS;
while (t1 != PO) {//
t1 = t1 + PAS ;// on augmente la valeur de l'impulsion
digitalWrite(PinServo, HIGH); // on envoie l'impulsion
delayMicroseconds(t1); // pendant la bonne durée
digitalWrite(PinServo, LOW); // on stoppe l'impulsion
delayMicroseconds(periode - t1); // on attend le temps restant pour atteindre la période
}// fin while
t2 = PO;
a = 1;
digitalWrite(Led1, LOW); // Led1 éteinte
digitalWrite(Led2, HIGH); // Led2 allumée
}// fin if
// Cde aiguillage position repos par BP2
if (etatBP2 == LOW && t1 == PO && a == 1) {
t2 = PO + PAS;
while (t2 != PF) {
t2 = t2 - PAS ;
digitalWrite(PinServo, HIGH); // on envoie l'impulsion
delayMicroseconds(t2); // pendant la bonne durée
digitalWrite(PinServo, LOW); // on stoppe l'impulsion
delayMicroseconds(periode - t2); // on attend le temps restant pour atteindre la période
}// fin while
t1 = PF;
digitalWrite(Led1, HIGH); // Led1 allumée
digitalWrite(Led2, LOW); // Led2 éteinte
}// fin if
}// fin loop
tiny:PB5
tiny:PB3
tiny:PB4
tiny:GND
tiny:PB0
tiny:PB1
tiny:PB2
tiny:VCC
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
led1:A
led1:C
led2:A
led2:C
servo1:GND
servo1:V+
servo1:PWM