boutton up et down

Bonjour,

pour commencer je suis débutant, maintenant voici mon problème; j'ai des neopixels que je veux faire passer de éteinte a r/v/b et pouvoir revenir en arrière, mais voila que tout mes tentative sont des échecs. je vais vous donner mon code et dite moi comment faire svp pour ajouter un bouton down

merci BCp de m'aider

const int  buttonUpPin = 2;    
const int  buttonDownPin = 7;    

// Variables will change:
int buttonPushCounter = 0;  
int buttonState = 0;         
int lastButtonState = 0;     
int ColorMode = 0; 

#include <Adafruit_NeoPixel.h>

#define PIN 1//This is the data pin connected to arrow on first NeoPixel

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
 
 pinMode(buttonUpPin, INPUT);
 // initialize serial communication:
//  Serial.begin(9600);
 strip.begin(); //intialize neopixels
}


void loop() {

 buttonState = digitalRead(buttonUpPin);

 
 if (buttonState != lastButtonState) {
 
   if (buttonState == HIGH) {
    
     buttonPushCounter++;
    
   } 
   else {
    
   }
 }
 
 
 
 
 
 lastButtonState = buttonState;

 
 
 ColorMode = buttonPushCounter % 5; //This is performing integer divison and calculating the remainder
 
 if (ColorMode == 0) { // ColorMode 0 is all off
   strip.setPixelColor (0,0,0,0); //first neopixel in strip
     strip.setPixelColor (1,0,0,0); //second neopixel in strip
     strip.setPixelColor (2,0,0,0); //third neopixel in strip
     strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
 } else if (ColorMode == 1){
  strip.setPixelColor (0,155,0,0); //first neopixel in strip
     strip.setPixelColor (1,155,0,0); //second neopixel in strip
     strip.setPixelColor (2,155,0,0); //third neopixel in strip
     strip.setPixelColor (3,155,0,0); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
  
 } else if (ColorMode == 2){
  strip.setPixelColor (0,0,155,0); //first neopixel in strip
     strip.setPixelColor (1,0,155,0); //second neopixel in strip
     strip.setPixelColor (2,0,155,0); //third neopixel in strip
     strip.setPixelColor (3,0,155,0); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
  
 }
 else if (ColorMode == 3){
  strip.setPixelColor (0,0,0,155); //first neopixel in strip
     strip.setPixelColor (1,0,0,155); //second neopixel in strip
     strip.setPixelColor (2,0,0,155); //third neopixel in strip
     strip.setPixelColor (3,0,0,155); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
  
 } else if (ColorMode == 4){
  strip.setPixelColor (0,0,0,155); //first neopixel in strip
     strip.setPixelColor (1,0,0,155); //second neopixel in strip
     strip.setPixelColor (2,0,0,155); //third neopixel in strip
     strip.setPixelColor (3,0,0,155); //fourth neopixel in strip
strip.setPixelColor (4,0,0,155); //second neopixel in strip
     strip.setPixelColor (5,0,0,155); //third neopixel in strip
     strip.setPixelColor (6,0,0,155); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
  
 }else
 delay(1);
 
}

Boujour,

Utiliser 'INPUT_PULLUP' à la place de 'INPUT' dans 'pinMode(buttonUpPin, INPUT);' si vous utilisez un bouton poussoir relié à GND.

Merci de modifier votre post en utilisant les balises code.

Tu devrais t'intéresser à l'écriture des fonctions : lorsque plusieurs blocs d'instructions similaires se retrouvent à plusieurs endroits, il est plus intéressant de les remplacer par des appels de fonctions. Les lignes suivantes que l'on retrouve dans tes blocs if

     strip.setPixelColor (0,0,155,0); //first neopixel in strip
      strip.setPixelColor (1,0,155,0); //second neopixel in strip
      strip.setPixelColor (2,0,155,0); //third neopixel in strip
      strip.setPixelColor (3,0,155,0); //fourth neopixel in strip
      strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do

pourraient être remplacées par l'appel d'une fonction qui allumerait les pixels de 0 à N, avec les couleurs que tu lui indiquerais en argument (R, V, B):

allumePixels(N,R,V,B);

Pense aussi à mettre une boucle for dans cette fonction

Pour ta question concernant l'ajout d'un bouton, tu as déjà tout dans ton code actuel, il suffit de dupliquer certaines lignes:
Déclaration

  pinMode(buttonDownPin, INPUT);

Comme suggère Zlika, regarde le INPUT_PULLUP
Lecture du bouton, il te faut une autre variable d'état (une pour chaque bouton)

  buttonDownState = digitalRead(buttonDownPin);

Ensuite, les tests sur l'état du bouton, similaires à ce que tu as déjà...

Sloko:
Bonjour,

pour commencer je suis débutant, maintenant voici mon problème; j'ai des neopixels que je veux faire passer de éteinte a r/v/b et pouvoir revenir en arrière, mais voila que tout mes tentative sont des échecs. je vais vous donner mon code et dite moi comment faire svp pour ajouter un bouton down

merci BCp de m'aider

// this constant won't change:

const int  buttonUpPin = 2;    
const int  buttonDownPin = 7;

// Variables will change:
int buttonPushCounter = 0;  
int buttonState = 0;        
int lastButtonState = 0;    
int ColorMode = 0;

#include <Adafruit_NeoPixel.h>

#define PIN 1//This is the data pin connected to arrow on first NeoPixel

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
 // initialize the button pin as a input:
 pinMode(buttonUpPin, INPUT_PULLUP);
 // initialize serial communication:
//  Serial.begin(9600);
 strip.begin(); //intialize neopixels
}

void loop() {
 // read the pushbutton input pin:
 buttonState = digitalRead(buttonUpPin);

// compare the buttonState to its previous state
 if (buttonState != lastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     buttonPushCounter++;
     //Serial.println("on");
     //Serial.print("number of button pushes:  ");
     //Serial.println(buttonPushCounter);
   }
   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
 lastButtonState = buttonState;

// 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:
 ColorMode = buttonPushCounter % 4; //This is performing integer divison and calculating the remainder
 
 if (ColorMode == 0) { // ColorMode 0 is all off
   strip.setPixelColor (0,150); //first neopixel in strip
     strip.setPixelColor (1,0,0,0); //second neopixel in strip
     strip.setPixelColor (2,0,0,0); //third neopixel in strip
     strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
 } else if (ColorMode == 1){
  strip.setPixelColor (0,0,0,0); //first neopixel in strip
     strip.setPixelColor (1,150); //second neopixel in strip
     strip.setPixelColor (2,0,0,0); //third neopixel in strip
     strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
 
 } else if (ColorMode == 2){
  strip.setPixelColor (0,0,0,0); //first neopixel in strip
     strip.setPixelColor (1,0,0,0); //second neopixel in strip
     strip.setPixelColor (2,150); //third neopixel in strip
     strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
 
 }
 else if (ColorMode == 3){
  strip.setPixelColor (0,0,0,0); //first neopixel in strip
    strip.setPixelColor (1,0,0,0); //second neopixel in strip
     strip.setPixelColor (2,0,0,0); //third neopixel in strip
     strip.setPixelColor (3,150); //fourth neopixel in strip
     strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
 
 
 }else
 delay(1);
 
}

Rien de neuf ?
Au passage, je te déconseille de brancher la bande de leds sur la pin 1, qui est aussi utilisée pour la liaison série et le téléversement des sketchs dans l'Arduino.

mais voila que tout mes tentative sont des échecs
merci BCp de m'aider

Bien, mais nous voilà bien avancés.
Quel est le problème ?
Sans explication comment aider ?

C'est comme dire : j'ai utilisé un logiciel que j'ai téléchargé ICI et ça bug.
Oui mais ça bug comment ?

Des observations ? des résultats ?

PS : le code ne se met pas entre quotes mais entre balises code : </>

Bon je ne suis pas devant mon ordi mais au travail, mais pour faire une histoire courte, le bouton UP aucun problème à le faire fonctionner. Quand je rajoute le DOWN. Se que ca fait c'est soit, toute les lumieres clignotent à répétition, rien ne fonction ou le bouton UP fonction bien, mais le down est comme inactif. Jai pas les exemple de mes echecs car je recommence a chaque fois.

Merci pour l'info je vais déplacer ma strip sur le 5

Tes codes ne sont toujours pas entre balises code. >:(
Regarde ici

Donc voila mon code, que je crois bon mais malheureusement rien ne foctionne. Une fois de plus quand j'ajoute des fonction au DwButtonpin, plus rien ne va.

merci bcp de tenter de m'aider

// this constant won't change:
const int  UpbuttonPin = 2;    // the pin that the pushbutton is attached to
const int  DwbuttonPin = 7;    // the pin that the pushbutton is attached to


// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int ColorMode = 0;         //holds the color value in this case we have 4 states off, red, green, and blue

#include <Adafruit_NeoPixel.h>

#define PIN 5//This is the data pin connected to arrow on first NeoPixel

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);
 
 
void setup() {
  // initialize the button pin as a input:
  pinMode(UpbuttonPin, INPUT_PULLUP);
  pinMode(DwbuttonPin, INPUT_PULLUP);
  // initialize serial communication:
//  Serial.begin(9600);
  strip.begin(); //intialize neopixels
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(UpbuttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      //Serial.println("on");
      //Serial.print("number of button pushes:  ");
      //Serial.println(buttonPushCounter);
    } 
   
  // read the pushbutton input pin:
  buttonState = digitalRead(DwbuttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter--;
      //Serial.println("on");
      //Serial.print("number of button pushes:  ");
      //Serial.println(buttonPushCounter);
    } 
    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
  lastButtonState = buttonState;

  
  // 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:
  ColorMode = buttonPushCounter % 4; //This is performing integer divison and calculating the remainder
  
  if (ColorMode == 0) { // ColorMode 0 is all off
    strip.setPixelColor (0,0,0,0); //first neopixel in strip
      strip.setPixelColor (1,0,0,0); //second neopixel in strip
      strip.setPixelColor (2,0,0,0); //third neopixel in strip
      strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
      strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
  } else if (ColorMode == 1){
     strip.setPixelColor (0,155,0,0); //first neopixel in strip
      strip.setPixelColor (1,0,0,0); //second neopixel in strip
      strip.setPixelColor (2,0,0,0); //third neopixel in strip
      strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
      strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
   
  } else if (ColorMode == 2){
     strip.setPixelColor (0,0,155,0); //first neopixel in strip
      strip.setPixelColor (1,0,155,0); //second neopixel in strip
      strip.setPixelColor (2,0,0,0); //third neopixel in strip
      strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
      strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
   
  }
  else if (ColorMode == 3){
     strip.setPixelColor (0,0,0,155); //first neopixel in strip
      strip.setPixelColor (1,0,0,155); //second neopixel in strip
      strip.setPixelColor (2,0,0,155); //third neopixel in strip
      strip.setPixelColor (3,0,0,0); //fourth neopixel in strip
      strip.show (); //this is like hitting send on a text to neopixels but the text tells the lights what to do
   
  }else
  delay(1);
  
}

La variable 'lastButtonState' doit être différente pour les deux boutons, tu ne peux pas stocker les états de deux boutons différents dans une même variable.
Tu peux utiliser une seule variable 'buttonState' , mais tu dois placer la ligne de code 'lastButtonState = buttonState;' (avec les variables corrigées) aux bons endroits

Modifié aussi les codes de tes posts précédents stp.

Je te propose ça :

// this constant won't change:
const int  UpbuttonPin = 2;    // the pin that the pushbutton is attached to
const int  DwbuttonPin = 7;    // the pin that the pushbutton is attached to


// Variables will change:
int ButtonPushCounter = 0;   // counter for the number of button presses
int upButtonState = 0;         // current state of the button
int upLastButtonState = 0;     // previous state of the button
int dwButtonState = 0;         // current state of the button
int dwLastButtonState = 0;     // previous state of the button
int ColorMode = 0;         //holds the color value in this case we have 4 states off, red, green, and blue

#include <Adafruit_NeoPixel.h>
#define PIN 5//This is the data pin connected to arrow on first NeoPixel

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);

void colorLeds (int N1, int N, int Rouge, int Vert, int Bleu) {
  for (int i = 0; i < N1; i++) strip.setPixelColor (i, Rouge, Vert, Bleu);
  for (int i = N1; i < N; i++) strip.setPixelColor (i, 0, 0, 0);
  strip.show ();
}

void setup() {
  // initialize the button pin as a input:
  pinMode(UpbuttonPin, INPUT_PULLUP);
  pinMode(DwbuttonPin, INPUT_PULLUP);
  // initialize serial communication:
  //  Serial.begin(9600);
  strip.begin(); //intialize neopixels
}


void loop() {
  // read the pushbutton input pin:
  upButtonState = digitalRead(UpbuttonPin);
  dwButtonState = digitalRead(DwbuttonPin);
  delay(30);

  // compare the buttonState to its previous state
  if (upButtonState != upLastButtonState) {
    // if the state has changed, increment the counter
    if (upButtonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      ButtonPushCounter++;
      //Serial.println("on");
      //Serial.print("number of button pushes:  ");
      //Serial.println(ButtonPushCounter);
    }
  }
  // compare the buttonState to its previous state
  if (dwButtonState != dwLastButtonState) {
    // if the state has changed, increment the counter
    if (dwButtonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      ButtonPushCounter--;
      if (ButtonPushCounter < 0) ButtonPushCounter = 0;
      //Serial.println("on");
      //Serial.print("number of button pushes:  ");
      //Serial.println(ButtonPushCounter);
    }
  }

  // save the current state as the last state,
  //for next time through the loop
  upLastButtonState = upButtonState;
  dwLastButtonState = dwButtonState;


  // 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:
  ColorMode = ButtonPushCounter % 4; //This is performing integer divison and calculating the remainder
  if (ColorMode == 0) colorLeds (4, 4, 0, 0, 0); // ColorMode 0 is all off
  else if (ColorMode == 1) colorLeds (1, 4, 155, 0, 0);
  else if (ColorMode == 2)colorLeds (2, 4, 0, 155, 0);
  else if (ColorMode == 3) colorLeds (3, 4, 0, 0, 155);
  else delay(1);
}

Peu de différence avec ce que tu as commencé à faire sauf que j'ai mis une fonction pour allumer les leds.
ça compile, mais je ne sais pas si ça fonctionne comme tu veux...

Merci!!!! a tous pour votre aide. Lesept mais bouton fonction numero 1 ( up and down ) en plus je crois comprendre la logique. Merci encore pour votre aide