Bouton poussoir -> Bouton On / Off à tout moment

Bonjour je n'ai pas de bouton On / Off alors j'aimerais en créer un à l'aide d'un bouton poussoir, j'aimerais que mon programme "pilotage_servo" s'active en appuyant un fois et qu'elle s'éteigne en r'appuyant dessus.
Comment je peux faire ?

voici mon code :

#include <Servo.h>
Servo servo;
unsigned long potvalue = 0 ;
unsigned long delaipot = 0 ;
const int trigPin = 5;// Pin Trig Ultrason
const int echoPin = 6;// Pin Echo Ultrason
const int boutonPin = 2;// Pin BP
const int PotPin = A0;// Pin Potentiomètre
const int LedRougePin = 13;// Pin Led Rouge
const int ServoPin = 8;// Pin Servomoteur
boolean pression_bouton = 0 ;
int Etat_bouton = 0;
int pos = 0;


void setup() {
  delaipot = 0;
  servo.attach(3);// Pin Servomoteur
  pinMode(PotPin, INPUT);// Pin pot
  pinMode(LedRougePin, OUTPUT);// Pin leds
  pinMode(boutonPin, OUTPUT);//Bouton
  Serial.begin(9600); // Initialisation moniteur série
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {

  pression_bouton = digitalRead(boutonPin);

  if (pression_bouton == 1) {
    Etat_bouton = 1;
    digitalWrite(LedRougePin, HIGH);
    pilotage_servo(pos); // Programme Servomoteur
    if (pression_bouton == 1) {
      Etat_bouton = 0;
    }

    if (Etat_bouton == 0) {
      digitalWrite(LedRougePin, LOW);
      servo.write(100);
    }
  }
}



void pilotage_servo(int angle) {
  for (pos = 0; pos <= 180; pos += 1) {
    potvalue = analogRead(A0);
    delaipot = ((200 - 50) * potvalue) / 1023 + 50; // Calcul delai pas avec pot
    servo.write(pos); // Position du moteur
    delay(delaipot);
    Serial.print("Angle"); //Moniteur série
    Serial.print(" = ");//Moniteur série
    Serial.print(pos);//Moniteur série
    Serial.println("°");//Moniteur série
    Serial.println(Etat_bouton);
  }
  for (pos = 180; pos >= 0; pos -= 1)
  {
    potvalue = analogRead(A0);
    delaipot = ((200 - 50) * potvalue) / 1023 + 50; // Calcul delai pas avec pot
    servo.write(pos);// Position du moteur
    delay(delaipot);
    Serial.print("Angle");//Moniteur série
    Serial.print(" = ");//Moniteur série
    Serial.print(pos);//Moniteur série
    Serial.println("°");//Moniteur série
    Serial.println(Etat_bouton);
  }
}

hello
testes ce code

#define boutonPin 2
byte etat=1;
byte compteur=0;
void setup() {
  Serial.begin(115200);
pinMode(boutonPin,INPUT_PULLUP);
}

void loop() {
   if (!digitalRead(boutonPin)){
    compteur++;
    if(compteur>2){compteur=0;}
   }
   etat=compteur%2;
   Serial.print(" etat ");Serial.println  (etat);
   delay(300);
}

Salut,

Et un truc du style dans ton loop:

If(bp==High){
   While( bp==High){}
   cdeServo != cdeServo //inverse l’etat D’un variable booléenne: par ex: « cdeServo »
}   // fin if

If(cdeServo ==true){

//tu envoie vers ta fonction de commande servo 

}

tu as declaré le BP en OUTPUT
j'ai rectifié en INPUT_PULLUP

ton BP doit être cablé entre GND et D2.
j'ai également passé la vitesse du moniteur à 115200. il faut modifier en bas à droite dans la fenetre du moniteur.

#include <Servo.h>
Servo servo;
unsigned long potvalue = 0 ;
unsigned long delaipot = 0 ;
const int trigPin = 5;// Pin Trig Ultrason
const int echoPin = 6;// Pin Echo Ultrason
const int boutonPin = 2;// Pin BP
const int PotPin = A0;// Pin Potentiomètre
const int LedRougePin = 13;// Pin Led Rouge
const int ServoPin = 8;// Pin Servomoteur
boolean pression_bouton = 0 ;
byte Etat_bouton = 1;
int pos = 0;
byte compteur = 0;
byte etat = 1;
void setup() {
  delaipot = 0;
  servo.attach(3);// Pin Servomoteur
  pinMode(PotPin, INPUT);// Pin pot
  pinMode(LedRougePin, OUTPUT);// Pin leds
  pinMode(boutonPin, INPUT_PULLUP);//Bouton
  Serial.begin(115200); // Initialisation moniteur série
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  etat=lecture_BP();
  if (etat == 1)
  { Serial.println(" ON ");
    digitalWrite(LedRougePin, HIGH);
    pilotage_servo(pos); // Programme Servomoteur
  }
  else
  {Serial.println(" OFF ");
    digitalWrite(LedRougePin, LOW);
    servo.write(100);
  }
}
//*****************************************lecture BP
int lecture_BP()
{
  if (!digitalRead(boutonPin))
  {
    compteur++;
    if (compteur > 2)
    {
      compteur = 1;
    }
  }
etat = compteur % 2;
while(!digitalRead(boutonPin)){};
return etat;
}
//********************************************pilotage servo  
void pilotage_servo(int angle) {
  for (pos = 0; pos <= 180; pos += 1) {
    potvalue = analogRead(A0);
    delaipot = ((200 - 50) * potvalue) / 1023 + 50; // Calcul delai pas avec pot
    servo.write(pos); // Position du moteur
    delay(delaipot);
    Serial.print("Angle"); //Moniteur série
    Serial.print(" = ");//Moniteur série
    Serial.print(pos);//Moniteur série
    Serial.println("°");//Moniteur série
    etat=lecture_BP();if (etat == 0){break;}
  }
  for (pos = 180; pos >= 0; pos -= 1)
  {
    potvalue = analogRead(A0);
    delaipot = ((200 - 50) * potvalue) / 1023 + 50; // Calcul delai pas avec pot
    servo.write(pos);// Position du moteur
    delay(delaipot);
    Serial.print("Angle");//Moniteur série
    Serial.print(" = ");//Moniteur série
    Serial.print(pos);//Moniteur série
    Serial.println("°");//Moniteur série
    etat=lecture_BP();if (etat == 0){break;}
  }
}