Potentiomètre et neopixels

Bonjour,

j'ai un arduino nano avec 5 boutons poussoir (pin 2/3/4/5/6) qui changent la couleur de la led neopixel RGBW de 4w (pin 12)

// This program allows for several buttons to trigger specific colors.
// It also allows a button for rainbow and random effects.


#include <Adafruit_NeoPixel.h>

// Define the buttons and pin location
#define BUTTON_PIN_2   8  // Cycle/Random
#define BUTTON_PIN_3   2  // Red
#define BUTTON_PIN_4   3  // Green
#define BUTTON_PIN_5   4  // Blue
#define BUTTON_PIN_6   5  // White
#define BUTTON_PIN_7   6  // Rainbow



#define PIXEL_PIN    12    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 1    // Number of LEDs in the Neopixel

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// State variables and initial condition
bool oldState_2 = HIGH;
bool oldState_3 = HIGH;
bool oldState_4 = HIGH;
bool oldState_5 = HIGH;
bool oldState_6 = HIGH;
bool oldState_7 = HIGH;
int showType = 0;
int colorSpeed = 40; // Delay in pixel color wipe
int rainbowSpeed = .8; // Delay in rainbow wipe
int theaterSpeed = 100; // Delay in theater chase

void setup() {
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  pinMode(BUTTON_PIN_4, INPUT_PULLUP);
  pinMode(BUTTON_PIN_5, INPUT_PULLUP);
  pinMode(BUTTON_PIN_6, INPUT_PULLUP);
  pinMode(BUTTON_PIN_7, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState_2 = digitalRead(BUTTON_PIN_2);
  bool newState_3 = digitalRead(BUTTON_PIN_3);
  bool newState_4 = digitalRead(BUTTON_PIN_4);
  bool newState_5 = digitalRead(BUTTON_PIN_5);
  bool newState_6 = digitalRead(BUTTON_PIN_6);
  bool newState_7 = digitalRead(BUTTON_PIN_7);
 
  // Check if button 2 state changed from high to low (button press).
  if (newState_2 == LOW && oldState_2 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_2 = digitalRead(BUTTON_PIN_2);
    if (newState_2 == LOW) {
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
    }
    }
 
  // Check if button 3 state changed from high to low (button press).   
    else if (newState_3 == LOW && oldState_3 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_3 = digitalRead(BUTTON_PIN_3);
    if (newState_3 == LOW) {
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
    }
    }

   // Check if button 4 state changed from high to low (button press).   
    else if (newState_4 == LOW && oldState_4 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_4 = digitalRead(BUTTON_PIN_4);
    if (newState_4 == LOW) {
      colorWipe(strip.Color(0, 255, 0, 0), colorSpeed); // Green
    }
    }
   
    // Check if button 5 state changed from high to low (button press).
    else if (newState_5 == LOW && oldState_5 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_5 = digitalRead(BUTTON_PIN_5);
    if (newState_5 == LOW) {
      colorWipe(strip.Color(0, 0, 255, 0), colorSpeed);  // Blue
    }
    }
   
     // Check if button 6 state changed from high to low (button press).
    else if (newState_6 == LOW && oldState_6 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_6 = digitalRead(BUTTON_PIN_6);
    if (newState_6 == LOW) {
      colorWipe(strip.Color(0, 0, 0, 255), colorSpeed);  // White
    }
    }

     // Check if button 7 state changed from high to low (button press).
    else if (newState_7 == LOW && oldState_7 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_7 = digitalRead(BUTTON_PIN_7);
    if (newState_7 == LOW) {
      colorWipe(strip.Color(255, 255, 255, 255), colorSpeed);  // White+rgb
    }
    }
   
 
  // Set the last button state to the old state.
  oldState_2 = newState_2;
  oldState_3 = newState_3;
  oldState_3 = newState_4;
  oldState_3 = newState_5;
  oldState_3 = newState_6;
  oldState_3 = newState_7;
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}







// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

ce code fonctionne bien :slight_smile:

j'aimerais ajouter un potentiomètre (pin 0) pour pouvoir faire varier l'intensité lumineuse de la led
j'ai trouvé un autre code

//Modified by Bob Clagett for use with the project at http://www.iliketomakestuff.com/make-gopro-ring-light

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            12 

// How many NeoPixels are attached to the Arduino?
#define PIXEL_COUNT      1
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIN, NEO_GRBW + NEO_KHZ800);

int delayval = 10; // delay for half a second
int potPin  =   0;  //analog pin used for potentiometer

void setup() {


  pixels.begin(); // This initializes the NeoPixel library.
}


void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
int sensorValue = analogRead(potPin);
int brightnessVal = map(sensorValue, 0, 1023, 0, 255); 
  for(int i=0;i<PIXEL_COUNT;i++){
     // pixels.setBrightness sets the  brightness of the strip.
  
    pixels.setBrightness(brightnessVal);

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
   
    pixels.setPixelColor(i, pixels.Color(255,255,255)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}

Lui aussi fonctionne, sauf au tout début de la course du potentiomètre ou la led flash (peut être que le potentiomètre B50K n'est pas adapté ou trop fatigué ?)

J'ai donc essayé d'ajouter le second code au premier mais je n'y arrive pas :confused:

// This program allows for several buttons to trigger specific colors.
// It also allows a button for rainbow and random effects.


#include <Adafruit_NeoPixel.h>

// Define the buttons and pin location
#define BUTTON_PIN_2   8  // Cycle/Random
#define BUTTON_PIN_3   2  // Red
#define BUTTON_PIN_4   3  // Green
#define BUTTON_PIN_5   4  // Blue
#define BUTTON_PIN_6   5  // White
#define BUTTON_PIN_7   6  // Rainbow



#define PIXEL_PIN    12    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 1    // Number of LEDs in the Neopixel

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// State variables and initial condition
bool oldState_2 = HIGH;
bool oldState_3 = HIGH;
bool oldState_4 = HIGH;
bool oldState_5 = HIGH;
bool oldState_6 = HIGH;
bool oldState_7 = HIGH;
int showType = 0;
int colorSpeed = 40; // Delay in pixel color wipe
int rainbowSpeed = .8; // Delay in rainbow wipe
int theaterSpeed = 100; // Delay in theater chase


int delayval = 10; // delay for half a second
int potPin  =   0;  //analog pin used for potentiometer

void setup() {
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  pinMode(BUTTON_PIN_4, INPUT_PULLUP);
  pinMode(BUTTON_PIN_5, INPUT_PULLUP);
  pinMode(BUTTON_PIN_6, INPUT_PULLUP);
  pinMode(BUTTON_PIN_7, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
int sensorValue = analogRead(potPin);
int brightnessVal = map(sensorValue, 0, 1023, 0, 255); 
  for(int i=0;i<PIXEL_COUNT;i++){
     // pixels.setBrightness sets the  brightness of the strip.
  
    pixels.setBrightness(brightnessVal);

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
   
    pixels.setPixelColor(i, pixels.Color(255,255,255)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }

  
  // Get current button state.
  bool newState_2 = digitalRead(BUTTON_PIN_2);
  bool newState_3 = digitalRead(BUTTON_PIN_3);
  bool newState_4 = digitalRead(BUTTON_PIN_4);
  bool newState_5 = digitalRead(BUTTON_PIN_5);
  bool newState_6 = digitalRead(BUTTON_PIN_6);
  bool newState_7 = digitalRead(BUTTON_PIN_7);
 
  // Check if button 2 state changed from high to low (button press).
  if (newState_2 == LOW && oldState_2 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_2 = digitalRead(BUTTON_PIN_2);
    if (newState_2 == LOW) {
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
    }
    }
 
  // Check if button 3 state changed from high to low (button press).   
    else if (newState_3 == LOW && oldState_3 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_3 = digitalRead(BUTTON_PIN_3);
    if (newState_3 == LOW) {
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
    }
    }

   // Check if button 4 state changed from high to low (button press).   
    else if (newState_4 == LOW && oldState_4 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_4 = digitalRead(BUTTON_PIN_4);
    if (newState_4 == LOW) {
      colorWipe(strip.Color(0, 255, 0, 0), colorSpeed); // Green
    }
    }
   
    // Check if button 5 state changed from high to low (button press).
    else if (newState_5 == LOW && oldState_5 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_5 = digitalRead(BUTTON_PIN_5);
    if (newState_5 == LOW) {
      colorWipe(strip.Color(0, 0, 255, 0), colorSpeed);  // Blue
    }
    }
   
     // Check if button 6 state changed from high to low (button press).
    else if (newState_6 == LOW && oldState_6 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_6 = digitalRead(BUTTON_PIN_6);
    if (newState_6 == LOW) {
      colorWipe(strip.Color(0, 0, 0, 255), colorSpeed);  // White
    }
    }

     // Check if button 7 state changed from high to low (button press).
    else if (newState_7 == LOW && oldState_7 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_7 = digitalRead(BUTTON_PIN_7);
    if (newState_7 == LOW) {
      colorWipe(strip.Color(255, 255, 255, 255), colorSpeed);  // White+rgb
    }
    }
   
 
  // Set the last button state to the old state.
  oldState_2 = newState_2;
  oldState_3 = newState_3;
  oldState_3 = newState_4;
  oldState_3 = newState_5;
  oldState_3 = newState_6;
  oldState_3 = newState_7;
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}







// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

J'ai comme erreur

'pixels' was not declared in this scope

Normal l'erreur lors de la fusion des programmes n'a pas unifié le nom de la bande.
dans ce dernier code vous avez la ligne :

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

Vous nommez l'objet "strip" puis à certains endroits dans le code vous l'appelez comme par exemple :

pixels.setBrightness(brightnessVal);

j'aurais du mettre :

strip.setBrightness(brightnessVal);

Changez l'un ou l'autre, mais unifiez tout.

Oh merci :smiley:

Le potentiomètre fonctionne mais si j’éteins la led complètement je ne peut pas la rallumer !
Comment faire ?

Bien qu'avec un clin d'œil légèrement gênant, la fonction potentiomètre fonctionne correctement pour moi. Comment l'avez-vous connecté ?
J'ai mis le code que je pense être correct, je n'ai rien supprimé, je viens de commenter ce que je pense que vous devriez supprimer.

// This program allows for several buttons to trigger specific colors.
// It also allows a button for rainbow and random effects.


#include <Adafruit_NeoPixel.h>

// Define the buttons and pin location
#define BUTTON_PIN_2   8  // Cycle/Random
#define BUTTON_PIN_3   2  // Red
#define BUTTON_PIN_4   3  // Green
#define BUTTON_PIN_5   4  // Blue
#define BUTTON_PIN_6   5  // White
#define BUTTON_PIN_7   6  // Rainbow



#define PIXEL_PIN    12    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 1    // Number of LEDs in the Neopixel

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// State variables and initial condition
bool oldState_2 = HIGH;
bool oldState_3 = HIGH;
bool oldState_4 = HIGH;
bool oldState_5 = HIGH;
bool oldState_6 = HIGH;
bool oldState_7 = HIGH;
//int showType = 0;
int colorSpeed = 40; // Delay in pixel color wipe
//int rainbowSpeed = .8; // Delay in rainbow wipe
//int theaterSpeed = 100; // Delay in theater chase


//int delayval = 10; // delay for half a second
int potPin  =   0;  //analog pin used for potentiometer

void setup() {
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  pinMode(BUTTON_PIN_4, INPUT_PULLUP);
  pinMode(BUTTON_PIN_5, INPUT_PULLUP);
  pinMode(BUTTON_PIN_6, INPUT_PULLUP);
  pinMode(BUTTON_PIN_7, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  int sensorValue = analogRead(potPin);
  int brightnessVal = map(sensorValue, 0, 1023, 0, 255);
  // for(int i=0;i<PIXEL_COUNT;i++){
  // pixels.setBrightness sets the  brightness of the strip.

  strip.setBrightness(brightnessVal);

  // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255

  //    strip.setPixelColor(i, strip.Color(255,255,255)); // Moderately bright green color.

  //    strip.show(); // This sends the updated pixel color to the hardware.

  //    delay(delayval); // Delay for a period of time (in milliseconds).

  //  }


  // Get current button state.
  bool newState_2 = digitalRead(BUTTON_PIN_2);
  bool newState_3 = digitalRead(BUTTON_PIN_3);
  bool newState_4 = digitalRead(BUTTON_PIN_4);
  bool newState_5 = digitalRead(BUTTON_PIN_5);
  bool newState_6 = digitalRead(BUTTON_PIN_6);
  bool newState_7 = digitalRead(BUTTON_PIN_7);

  // Check if button 2 state changed from high to low (button press).
  if (newState_2 == LOW && oldState_2 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_2 = digitalRead(BUTTON_PIN_2);
    if (newState_2 == LOW) {
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
    }
  }

  // Check if button 3 state changed from high to low (button press).
  else if (newState_3 == LOW && oldState_3 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_3 = digitalRead(BUTTON_PIN_3);
    if (newState_3 == LOW) {
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
    }
  }

  // Check if button 4 state changed from high to low (button press).
  else if (newState_4 == LOW && oldState_4 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_4 = digitalRead(BUTTON_PIN_4);
    if (newState_4 == LOW) {
      colorWipe(strip.Color(0, 255, 0, 0), colorSpeed); // Green
    }
  }

  // Check if button 5 state changed from high to low (button press).
  else if (newState_5 == LOW && oldState_5 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_5 = digitalRead(BUTTON_PIN_5);
    if (newState_5 == LOW) {
      colorWipe(strip.Color(0, 0, 255, 0), colorSpeed);  // Blue
    }
  }

  // Check if button 6 state changed from high to low (button press).
  else if (newState_6 == LOW && oldState_6 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_6 = digitalRead(BUTTON_PIN_6);
    if (newState_6 == LOW) {
      colorWipe(strip.Color(0, 0, 0, 255), colorSpeed);  // White
    }
  }

  // Check if button 7 state changed from high to low (button press).
  else if (newState_7 == LOW && oldState_7 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_7 = digitalRead(BUTTON_PIN_7);
    if (newState_7 == LOW) {
      colorWipe(strip.Color(255, 255, 255, 255), colorSpeed);  // White+rgb
    }
  }
  
  // Set the last button state to the old state.
  oldState_2 = newState_2;
  oldState_3 = newState_3;
  oldState_3 = newState_4;
  oldState_3 = newState_5;
  oldState_3 = newState_6;
  oldState_3 = newState_7;
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

Salutations

Bonjour

On peut supprimer ce clignotement en supprimant cette ligne:
strip.setPixelColor(i, strip.Color(255,255,255)); // Moderately bright green color.
ce qui amène le problème exposé par @moielias

Il faut restructurer ton programme, le problème avec setBrightness(0), c'est "destructif", c'est à dire que avec setBrightness(0), Neopixel efface les valeurs RGB précédentes. Comme ton programme ne paramètre la couleur qu'au changement de bouton si, entre deux tu est passé par 0 pour la luminosité les valeurs RGB sont perdues.
Tu peux tester ça en pesant un coup sur un bouton, la LED s'allume, tu descend la luminosité à 0 la LED s'éteint et si tu remontes il ne se passe plus rien.
Fais la même chose en gardant le bouton pressé, donc rafraichissement permanent, tu peux passer à 0 et remonter, ça fonctionne.
Comme tu n'as jamais qu'un bouton "évalué" donc une couleur sélectionnée, il faudrait rafraîchir, dans loop(), les LED en fonction de ce bouton mémorisé.
Une petite remarque dans ton programme:

int potPin  =   A0;  //analog pin used for potentiometer

// est plus claire que

int potPin  =   0;  //analog pin used for potentiometer

Une autre remarque sur le fond, quand il y a beaucoup de boutons à traiter leur mise en tableau facilite leur gestion.
Si tu le désires je peux te faire un exemple.

Cordialement
jpbricole

Bonjour moielias

Je t'ai fait un exemple avec mémorisation du dernier bouton pressé, int dernierBouton = 0;

// This program allows for several buttons to trigger specific colors.
// It also allows a button for rainbow and random effects.


#include <Adafruit_NeoPixel.h>

 //Define the buttons and pin location
#define BUTTON_PIN_2   8  // Cycle/Random
#define BUTTON_PIN_3   2  // Red
#define BUTTON_PIN_4   3  // Green
#define BUTTON_PIN_5   4  // Blue
#define BUTTON_PIN_6   5  // White
#define BUTTON_PIN_7   6  // Rainbow

// Config jpbbricole
//#define BUTTON_PIN_2   A0  // Cycle/Random
//#define BUTTON_PIN_3   A1  // Red
//#define BUTTON_PIN_4   A2  // Green
//#define BUTTON_PIN_5   9  // Blue
//#define BUTTON_PIN_6   10  // White
//#define BUTTON_PIN_7   10  // Rainbow



#define PIXEL_PIN    12    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 1    // Number of LEDs in the Neopixel

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// State variables and initial condition
bool oldState_2 = HIGH;
bool oldState_3 = HIGH;
bool oldState_4 = HIGH;
bool oldState_5 = HIGH;
bool oldState_6 = HIGH;
bool oldState_7 = HIGH;
int showType = 0;
int colorSpeed = 400; // Delay in pixel color wipe
int rainbowSpeed = .8; // Delay in rainbow wipe
int theaterSpeed = 100; // Delay in theater chase


int delayval = 10; // delay for half a second
int potPin  =   A4;  //analog pin used for potentiometer

int dernierBouton = 0;     // Memorise le dernier bouton pressé

void setup() {
	Serial.begin(115200);
	
	pinMode(BUTTON_PIN_2, INPUT_PULLUP);
	pinMode(BUTTON_PIN_3, INPUT_PULLUP);
	pinMode(BUTTON_PIN_4, INPUT_PULLUP);
	pinMode(BUTTON_PIN_5, INPUT_PULLUP);
	pinMode(BUTTON_PIN_6, INPUT_PULLUP);
	pinMode(BUTTON_PIN_7, INPUT_PULLUP);
	strip.begin();
	strip.show(); // Initialize all pixels to 'off'
}

void loop() {

	// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
	int sensorValue = analogRead(potPin);
	int brightnessVal = map(sensorValue, 0, 1023, 0, 255);
	for(int i=0;i<PIXEL_COUNT;i++){
		// pixels.setBrightness sets the  brightness of the strip.
		
		strip.setBrightness(brightnessVal);

		// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
		
		//strip.setPixelColor(i, strip.Color(255,255,255)); // Moderately bright green color.

		strip.show(); // This sends the updated pixel color to the hardware.

		delay(delayval); // Delay for a period of time (in milliseconds).

	}

	
	// Get current button state.
	bool newState_2 = digitalRead(BUTTON_PIN_2);
	bool newState_3 = digitalRead(BUTTON_PIN_3);
	bool newState_4 = digitalRead(BUTTON_PIN_4);
	bool newState_5 = digitalRead(BUTTON_PIN_5);
	bool newState_6 = digitalRead(BUTTON_PIN_6);
	bool newState_7 = digitalRead(BUTTON_PIN_7);
	
	// Check if button 2 state changed from high to low (button press).
	if (newState_2 == LOW && oldState_2 == HIGH) {
		// Short delay to debounce button.
		delay(20);
		// Check if button is still low after debounce.
		newState_2 = digitalRead(BUTTON_PIN_2);
		if (newState_2 == LOW) {
			dernierBouton = 2;
		}
	}
	
	// Check if button 3 state changed from high to low (button press).
	else if (newState_3 == LOW && oldState_3 == HIGH) {
		// Short delay to debounce button.
		delay(20);
		// Check if button is still low after debounce.
		newState_3 = digitalRead(BUTTON_PIN_3);
		if (newState_3 == LOW) {
			dernierBouton = 3;
		}
	}

	// Check if button 4 state changed from high to low (button press).
	else if (newState_4 == LOW && oldState_4 == HIGH) {
		// Short delay to debounce button.
		delay(20);
		// Check if button is still low after debounce.
		newState_4 = digitalRead(BUTTON_PIN_4);
		if (newState_4 == LOW) {
			dernierBouton = 4;
		}
	}
	
	// Check if button 5 state changed from high to low (button press).
	else if (newState_5 == LOW && oldState_5 == HIGH) {
		// Short delay to debounce button.
		delay(20);
		// Check if button is still low after debounce.
		newState_5 = digitalRead(BUTTON_PIN_5);
		if (newState_5 == LOW) {
			dernierBouton = 5;
		}
	}
	
	// Check if button 6 state changed from high to low (button press).
	else if (newState_6 == LOW && oldState_6 == HIGH) {
		// Short delay to debounce button.
		delay(20);
		// Check if button is still low after debounce.
		newState_6 = digitalRead(BUTTON_PIN_6);
		if (newState_6 == LOW) {
			dernierBouton = 6;
		}
	}

	// Check if button 7 state changed from high to low (button press).
	else if (newState_7 == LOW && oldState_7 == HIGH) {
		// Short delay to debounce button.
		delay(20);
		// Check if button is still low after debounce.
		newState_7 = digitalRead(BUTTON_PIN_7);
		if (newState_7 == LOW) {
			dernierBouton = 7;
		}
	}
	
	
	// Set the last button state to the old state.
	oldState_2 = newState_2;
	oldState_3 = newState_3;
	oldState_3 = newState_4;
	oldState_3 = newState_5;
	oldState_3 = newState_6;
	oldState_3 = newState_7;
	
	switch (dernierBouton)
	{
		case 2:     // Bouton 1
			colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
			break;
		case 3:     // ...
			colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // Red
			break;
		case 4:
			colorWipe(strip.Color(0, 255, 0, 0), colorSpeed); // Green
			break;
		case 5:
			colorWipe(strip.Color(0, 0, 255, 0), colorSpeed);  // Blue
			break;
		case 6:
			colorWipe(strip.Color(0, 0, 0, 255), colorSpeed);  // White
			break;
		case 7:
			colorWipe(strip.Color(255, 255, 255, 255), colorSpeed);  // White+rgb
			break;
	}
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
	for(uint16_t i=0; i<strip.numPixels(); i++) {
		strip.setPixelColor(i, c);
		strip.show();
		delay(wait);
	}
}







// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
	WheelPos = 255 - WheelPos;
	if(WheelPos < 85) {
		return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
		} else if(WheelPos < 170) {
		WheelPos -= 85;
		return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
		} else {
		WheelPos -= 170;
		return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
	}
}

A+
Cordialement
jpbbricole

1 Like

Salut, merci pour le code je l'ai un peut modifié (potar qui était pas assez fluide)

// This program allows for several buttons to trigger specific colors.
// It also allows a button for rainbow and random effects.


#include <Adafruit_NeoPixel.h>

 //Define the buttons and pin location
#define BUTTON_PIN_2   12  // rien
#define BUTTON_PIN_3   6   //   1 blanc chaud
#define BUTTON_PIN_4   5  //    2 ambre
#define BUTTON_PIN_5   4  //    3 Bleu
#define BUTTON_PIN_6   3  //    4 Rouge
#define BUTTON_PIN_7   2  //    5 Cyan

// Config jpbbricole
//#define BUTTON_PIN_2   A0  // Cycle/Random
//#define BUTTON_PIN_3   A1  // Red
//#define BUTTON_PIN_4   A2  // Green
//#define BUTTON_PIN_5   9  // Blue
//#define BUTTON_PIN_6   10  // White
//#define BUTTON_PIN_7   10  // Rainbow



#define PIXEL_PIN    12    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 1    // Number of LEDs in the Neopixel

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// State variables and initial condition
bool oldState_2 = HIGH;
bool oldState_3 = HIGH;
bool oldState_4 = HIGH;
bool oldState_5 = HIGH;
bool oldState_6 = HIGH;
bool oldState_7 = HIGH;
int showType = 0;
int colorSpeed = 40; // Delay in pixel color wipe
int rainbowSpeed = .8; // Delay in rainbow wipe
int theaterSpeed = 100; // Delay in theater chase


int delayval = 10; // delay for half a second
int potPin  =   0;  //analog pin used for potentiometer

int dernierBouton = 0;     // Memorise le dernier bouton pressé

void setup() {
  Serial.begin(115200);
  
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  pinMode(BUTTON_PIN_4, INPUT_PULLUP);
  pinMode(BUTTON_PIN_5, INPUT_PULLUP);
  pinMode(BUTTON_PIN_6, INPUT_PULLUP);
  pinMode(BUTTON_PIN_7, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  int sensorValue = analogRead(potPin);
  int brightnessVal = map(sensorValue, 0, 1023, 0, 255);
  for(int i=0;i<PIXEL_COUNT;i++){
    // pixels.setBrightness sets the  brightness of the strip.
    
    strip.setBrightness(brightnessVal);

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    
    //strip.setPixelColor(i, strip.Color(255,255,255)); // Moderately bright green color.

    strip.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }

  
  // Get current button state.
  bool newState_2 = digitalRead(BUTTON_PIN_2);
  bool newState_3 = digitalRead(BUTTON_PIN_3);
  bool newState_4 = digitalRead(BUTTON_PIN_4);
  bool newState_5 = digitalRead(BUTTON_PIN_5);
  bool newState_6 = digitalRead(BUTTON_PIN_6);
  bool newState_7 = digitalRead(BUTTON_PIN_7);
  
  // Check if button 2 state changed from high to low (button press).
  if (newState_2 == LOW && oldState_2 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_2 = digitalRead(BUTTON_PIN_2);
    if (newState_2 == LOW) {
      dernierBouton = 2;
    }
  }
  
  // Check if button 3 state changed from high to low (button press).
  else if (newState_3 == LOW && oldState_3 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_3 = digitalRead(BUTTON_PIN_3);
    if (newState_3 == LOW) {
      dernierBouton = 3;
    }
  }

  // Check if button 4 state changed from high to low (button press).
  else if (newState_4 == LOW && oldState_4 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_4 = digitalRead(BUTTON_PIN_4);
    if (newState_4 == LOW) {
      dernierBouton = 4;
    }
  }
  
  // Check if button 5 state changed from high to low (button press).
  else if (newState_5 == LOW && oldState_5 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_5 = digitalRead(BUTTON_PIN_5);
    if (newState_5 == LOW) {
      dernierBouton = 5;
    }
  }
  
  // Check if button 6 state changed from high to low (button press).
  else if (newState_6 == LOW && oldState_6 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_6 = digitalRead(BUTTON_PIN_6);
    if (newState_6 == LOW) {
      dernierBouton = 6;
    }
  }

  // Check if button 7 state changed from high to low (button press).
  else if (newState_7 == LOW && oldState_7 == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState_7 = digitalRead(BUTTON_PIN_7);
    if (newState_7 == LOW) {
      dernierBouton = 7;
    }
  }
  
  
  // Set the last button state to the old state.
  oldState_2 = newState_2;
  oldState_3 = newState_3;
  oldState_3 = newState_4;
  oldState_3 = newState_5;
  oldState_3 = newState_6;
  oldState_3 = newState_7;
  
  switch (dernierBouton)
  {
    case 2:     // Bouton 1
      colorWipe(strip.Color(0, 0, 0, 0), colorSpeed);  // RIEN 
      break;
    case 3:     // ...
      colorWipe(strip.Color(0, 0, 0, 255), colorSpeed);  // 1 Blanc chaud
      break;
    case 4:
      colorWipe(strip.Color(255, 90, 0, 235), colorSpeed); // 2 Ambre
      break;
    case 5:
      colorWipe(strip.Color(0, 0, 255, 0), colorSpeed);  // 3 Bleu
      break;
    case 6:
      colorWipe(strip.Color(255, 0, 0, 0), colorSpeed);  // 4 Rouge
      break;
    case 7:
      colorWipe(strip.Color(0, 255, 255, 0), colorSpeed);  // 5 cyan
      break;
  }
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}







// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    } else if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
    } else {
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

Mais j'ai toujours ce problème de potentiomètre qui flash dans les bas niveau :confused: es que rajouter un condensateur (de quel valeur ?) pourrait m'aider ?
Et est il possible de faire clignoter une led a une vitesse définie via l'appui d'un bouton, je crois que j'ai laissé cette partie de code dans le code que tu a modifié ?

Dans tout les cas merci a @gonpezzi & @jpbbricole pour votre aide !

Avec un condensateur ce n'est pas fixe, je viens de l'essayer avec plusieurs valeurs. Et s'il le répare suffisamment, (il échoue encore un peu et parfois il reste clignotant), c'est de faire une moyenne de plusieurs lectures avant de faire la "map".
Où avez-vous:

int sensorValue = analogRead(potPin);

Changez la ligne en tera pour tout ça :

 int sensorValue = 0;
  for (int i = 0; i <= 9; i++) {
    sensorValue = analogRead(0) + sensorValue; 
    delay (10); 
  }
  sensorValue = sensorValue / 10;

Salutations.

Merci ça fonctionne! mais du coup la courbe de graduation est en dents de scie, mais c'est un moindre mal comparé la lecture direct du potar.

Bonjour, après quelques mois d'utilisation, j’aimerais ajouter un temps de monté entre chaque appui sur un bouton, pour ne pas avoir un changement de couleur trop brusque, es possible ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.