Ardurrosage Arrosage géré par Arduino

Bonjour

Je m'appelle Marc et je début dans l'Arduino

Le but de ce projet est de faire un petit boitier qui va commander deux électrovannes qui vont alimenter l'arrosage de la serre et du jardin. 2x2 boutons pour gérer la quantité d'eau un bouton Start, et un écran LCD pour voir les instructions.

j'ai commencé le code. Et j'ai un bug, quand j’incrémente ou désincrément la partie serre pas de problème. par contre du coté jardin quand j'incrémente OK mais quand je désincrémente la valeur jardin diminue de 2

Vidéo (pas très bonne) Ardurrosage Bug - YouTube

Pouvez vous m'aider?

#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
// this constant won't change:
const int SerreP = 2; // the pin that the pushbutton is attached to
const int SerreM = 3; // the pin that the LED is attached to
const int JardinP = 4; // the pin that the pushbutton is attached to
const int JardinM = 5; // the pin that the LED is attached to
const int Start = 6;
const int Reset = 7;
// Variables will change:
int SerreCounter = 0; // counter for the number of button presses
int JardinCounter = 0;
int buttonStateP = 0; // current state of the button
int buttonStateM = 0; // current state of the button
int buttonJStateP = 0;
int buttonJStateM = 0;
int lastButtonStateP = 0; // previous state of the button
int lastButtonStateM = 0; // previous state of the button
int lastButtonJStateP = 0;
int lastButtonJStateM = 0;
int colorR = 200;
int colorG = 200;
int colorB = 200;
void setup() {
 // initialize the button pin as a input:
 pinMode(SerreP, INPUT);
 pinMode(SerreM, INPUT);
 pinMode(JardinP, INPUT);
 pinMode(JardinM, INPUT);
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 // Print a message to the LCD.
 lcd.setRGB(colorR, colorG, colorB);
 lcd.print("Ardurrosage");
}
void loop() {
 // read the pushbutton input pin:
 buttonStateP = digitalRead(SerreP);
 buttonStateM = digitalRead(SerreM);
 buttonJStateP = digitalRead(JardinP);
 buttonJStateM = digitalRead(JardinM);
 colorR = 20 * SerreCounter;
 colorG = 20 * JardinCounter ;
 colorB = 20 * SerreCounter;
 lcd.setRGB(colorR, colorG, colorB);
 lcd.setCursor(0, 1);
 lcd.print("Serre");
 lcd.setCursor(6, 1);
 lcd.print(SerreCounter);
 lcd.setCursor(8, 1);
 lcd.print("Jardin");
 lcd.setCursor(15, 1);
 lcd.print(JardinCounter);
 // compare the buttonState to its previous state
 if (buttonStateP != lastButtonStateP) {
 // if the state has changed, increment the counter
 if (buttonStateP == HIGH) {
 // if the current state is HIGH then the button went from off to on:
 SerreCounter++;
 if (SerreCounter <= 9) {
 SerreCounter = SerreCounter;
 } else {
 SerreCounter = 9;
 }
 }
 }
 if (buttonJStateP != lastButtonJStateP) {
 // if the state has changed, increment the counter
 if (buttonJStateP == HIGH) {
 // if the current state is HIGH then the button went from off to on:
 JardinCounter++;
 if (JardinCounter <= 9) {
 JardinCounter = JardinCounter;
 } else {
 JardinCounter = 9;
 }
 }
 }
 if (buttonStateM != lastButtonStateM) {
 // if the state has changed, increment the counter
 if (buttonStateM == HIGH) {
 // if the current state is HIGH then the button went from off to on:
 SerreCounter--;
 if (SerreCounter >= 0) {
 SerreCounter = SerreCounter;
 } else {
 SerreCounter = 0;
}
 }
}
 if (buttonJStateM != lastButtonJStateM) {
 // if the state has changed, increment the counter
 if (buttonJStateP == HIGH) {
 // if the current state is HIGH then the button went from off to on:
 JardinCounter--;
 if (JardinCounter >= 0) {
 JardinCounter = JardinCounter;
 } else {
 JardinCounter = 0;
 }
 }
 }
 // Delay a little bit to avoid bouncing
 delay(200);
 // save the current state as the last state, for next time through the loop
 lastButtonStateP = buttonStateP;
 lastButtonStateM = buttonStateM;
 lastButtonJStateP = buttonJStateP;
 lastButtonJStateM = buttonJStateM;

Salut

Il semblerait que tu ne gères pas les rebonds sur les boutons.

Jette un œil ICI (solution software).
Ou ICI (solution hardware).

@+

Merci pour la réponse, je vais tester la solution hardware.

j'avais changer de bouton et pin sur arduino mais toujours le même phénomène sur la même partie du code :o

Bonjour,

Ce n'est pas un problème de rebond car tu as un delay(200) ce qui est plus que suffisant pour le debouncing (tu peux le réduire à 20 ms).
C'est un problème de copier coller.
Dans le dernier if tu as
if (buttonJStateP == HIGH) {
Alors que ça devrait être
if (buttonJStateM== HIGH) {

Merci, j'étais persuadé qu'il avait une coquille dans le code, mais je l'avais relu 10* :smiling_imp:

Zarbi:
Merci, j'étais persuadé qu'il avait une coquille dans le code, mais je l'avais relu 10* :smiling_imp:

d'où l'importance d'avoir des noms de variables super clairs... là y'avait un effort mais pas suffisant

J-M-L:
d'où l'importance d'avoir des noms de variables super clairs... là y'avait un effort mais pas suffisant

Oui m'sieu

Zarbi:
Oui m'sieu

:smiley: :smiley: