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
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
// 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