Bonjour voici le code qui permet de gérer 3 boutons sur 3 sortie digitale et chaque bouton poussoir contrôle une sortie.
Les appuis sur chaque bouton sont retranscrit vers le serial monitor.
le problème ici c'est qu'il faut appuyer sur les 3 boutons pour allumer les 3 leds. Le code pose donc problème pour un appuie de bouton pour activer 1 circuit.
Quelqu'un a t'il une idée pour réctifier le code.
// this constant won't change:
const int buttonPinvert = 2; // the pin that the pushbutton is attached to
const int ledPinvert = 13; // the pin that the LED is attached to
const int buttonPinorange = 3; // the pin that the pushbutton is attached to
const int ledPinorange = 12; // the pin that the LED is attached to
const int buttonPinrouge = 4; // the pin that the pushbutton is attached to
const int ledPinrouge = 11; // the pin that the LED is attached to
// Variables will change:
int buttonPushCountervert = 0; // counter for the number of button presses
int buttonStatevert = 0; // current state of the button
int lastButtonStatevert = 0; // previous state of the button
int buttonPushCounterorange = 0; // counter for the number of button presses
int buttonStateorange = 0; // current state of the button
int lastButtonStateorange = 0; // previous state of the button
int buttonPushCounterrouge = 0; // counter for the number of button presses
int buttonStaterouge = 0; // current state of the button
int lastButtonStaterouge = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPinvert, INPUT);
// initialize the LED as an output:
pinMode(ledPinvert, OUTPUT);
// initialize serial communication:
// initialize the button pin as a input:
pinMode(buttonPinorange, INPUT);
// initialize the LED as an output:
pinMode(ledPinorange, OUTPUT);
// initialize serial communication:
// initialize the button pin as a input:
pinMode(buttonPinrouge, INPUT);
// initialize the LED as an output:
pinMode(ledPinrouge, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonStatevert = digitalRead(buttonPinvert);
// read the pushbutton input pin:
buttonStateorange = digitalRead(buttonPinorange);
// read the pushbutton input pin:
buttonStaterouge = digitalRead(buttonPinrouge);
// compare the buttonState to its previous state
if (buttonStatevert != lastButtonStatevert) {
// if the state has changed, increment the counter
if (buttonStatevert == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCountervert++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCountervert);
}
if (buttonStateorange != lastButtonStateorange) {
// if the state has changed, increment the counter
if (buttonStateorange == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounterorange++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounterorange);
}
if (buttonStaterouge != lastButtonStaterouge) {
// if the state has changed, increment the counter
if (buttonStaterouge == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounterrouge++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounterrouge);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonStatevert = buttonStatevert;
// save the current state as the last state,
//for next time through the loop
lastButtonStateorange = buttonStateorange;
// save the current state as the last state,
//for next time through the loop
lastButtonStaterouge = buttonStaterouge;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCountervert % 2 == 0) {
digitalWrite(ledPinvert, HIGH);
} else {
digitalWrite(ledPinvert, LOW);
}
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounterorange % 2 == 0) {
digitalWrite(ledPinorange, HIGH);
} else {
digitalWrite(ledPinorange, LOW);
}
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounterrouge % 2 == 0) {
digitalWrite(ledPinrouge, HIGH);
} else {
digitalWrite(ledPinrouge, LOW);
}
}
}
}
Je ne suis pas sur de ce que tu veux faire donc je ne comprend pas ton problème.
Déjà commence par indenter ton code correctement car là il est illisible on ne voit pas clairement où son les début et les fins des ifs.
peut être d'ailleurs quand toi-même y verra plus clair tu trouveras la solution.
J'ai l'impression que tu traite le bouton orange que si le bouton vert est appuyé.
Tes accolades de fermeture ne doivent pas être bien placées si tu souhaites que chaque bouton soit traité indépendamment
aïe l'indentation ^^' je suis d'accord avec barbudor, fait nous un beau code et à mon avis la solution tu la trouvera sans nous ^^ sinon exprime mieux ton problème la c'est pas claire :s
Ton probléme vient de ta cascade if imbriqué qui ne devrait pas l'être !
Essaye ceci (je n'ai fait que déplacer deux { ) :
// this constant won't change:
const int buttonPinvert = 2; // the pin that the pushbutton is attached to
const int ledPinvert = 13; // the pin that the LED is attached to
const int buttonPinorange = 3; // the pin that the pushbutton is attached to
const int ledPinorange = 12; // the pin that the LED is attached to
const int buttonPinrouge = 4; // the pin that the pushbutton is attached to
const int ledPinrouge = 11; // the pin that the LED is attached to
// Variables will change:
unsigned int buttonPushCountervert = 0; // counter for the number of button presses
unsigned int buttonStatevert = 0; // current state of the button
unsigned int lastButtonStatevert = 0; // previous state of the button
unsigned int buttonPushCounterorange = 0; // counter for the number of button presses
unsigned int buttonStateorange = 0; // current state of the button
unsigned int lastButtonStateorange = 0; // previous state of the button
unsigned int buttonPushCounterrouge = 0; // counter for the number of button presses
unsigned int buttonStaterouge = 0; // current state of the button
unsigned int lastButtonStaterouge = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPinvert, INPUT);
// initialize the LED as an output:
pinMode(ledPinvert, OUTPUT);
// initialize serial communication:
// initialize the button pin as a input:
pinMode(buttonPinorange, INPUT);
// initialize the LED as an output:
pinMode(ledPinorange, OUTPUT);
// initialize serial communication:
// initialize the button pin as a input:
pinMode(buttonPinrouge, INPUT);
// initialize the LED as an output:
pinMode(ledPinrouge, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonStatevert = digitalRead(buttonPinvert);
// read the pushbutton input pin:
buttonStateorange = digitalRead(buttonPinorange);
// read the pushbutton input pin:
buttonStaterouge = digitalRead(buttonPinrouge);
// compare the buttonState to its previous state
if (buttonStatevert != lastButtonStatevert) {
// if the state has changed, increment the counter
if (buttonStatevert == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCountervert++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCountervert);
}
}
if (buttonStateorange != lastButtonStateorange) {
// if the state has changed, increment the counter
if (buttonStateorange == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounterorange++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounterorange);
}
}
if (buttonStaterouge != lastButtonStaterouge) {
// if the state has changed, increment the counter
if (buttonStaterouge == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounterrouge++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounterrouge);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonStatevert = buttonStatevert;
// save the current state as the last state,
//for next time through the loop
lastButtonStateorange = buttonStateorange;
// save the current state as the last state,
//for next time through the loop
lastButtonStaterouge = buttonStaterouge;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCountervert % 2 == 0)
digitalWrite(ledPinvert, HIGH);
else
digitalWrite(ledPinvert, LOW);
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounterorange % 2 == 0)
digitalWrite(ledPinorange, HIGH);
else
digitalWrite(ledPinorange, LOW);
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounterrouge % 2 == 0)
digitalWrite(ledPinrouge, HIGH);
else
digitalWrite(ledPinrouge, LOW);
}
Ps: tu fait une gestion des led par modulo 2, ce n'est pas une méthode bien propre mais bon ...
Étant donnée que tu ne fait qu'utiliser des valeurs positive utilise des "unsigned int" au lieu de simple "int".
Un "unsigned int" peut stocker deux fois plus de nombres qu'un "int" classique (signed) en supprimant la possibilité d'avoir des nombres négatif.