I am a sculptor and relatively new to Arduino. I'm trying to make a piece with a series of buttons that all correspond to a colour so that when you press one button, the first section of the RGB LED strip turns a certain colour, you press another button and the next section turns a certain colour while the first section remains on and so forth.
I am comfortable with programming the colours and with wiring the buttons but I am unsure of how to code the strip so that the lights turn on sequentially without having to write "if" statements for every section of the led strip.
you have to write to every LED in the strip any time you have to change the color of one LED anywhere in the strip
Yes and no. Its true that the whole strip has to be updated, but the FastLED library does this for you, so you only have to update the leds you want to change.
We should not encourage cross-posting, it wastes people's time. On this subject, the OP will get advice and help that is at least as good as sites (and much better than some).
@Sabrina13, post the code you have so far, even if not complete, and we can advise how to proceed. Please read the forum guide in the sticky post to find out how to post your code, and other tips. Also post a schematic so we can check you are connecting the strip and using the components correctly, including details of the PSU.
Here is my code that I'm currently working on: UPDATED
#include <buttons.h>
#include <FastLED.h>
//#include "colorutils.h"
//#include "colorpalettes.h"
//#define LED_PIN 8
#define NUM_LEDS 50
#define LED_TYPE WS2812B
#define COLOR_ORDER RBG
#define DATA_PIN 5
//#define BUTTON_PIN 1 // Connect the button to GND and one of the pins.
#define BRIGHTNESS 32 // 255 is full brightness, 128 is half, 32 is an eighth.
const int buttonPin = 1; // the pin that the pushbutton is attached to
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 7;
const int buttonPin7 = 8;
const int ledPin = 9
; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
//int val = 0;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int bS1 = 0; //current state of the button
int lBS1 = 0; //last button state 1
int bS2 = 0;
int lBS2 = 0;
int bS3 = 0;
int lBS3 = 0;
int bS4 = 0;
int lBS4 = 0;
int bS5 = 0;
int lBS5 = 0;
int bS6 = 0;
int lBS6 = 0;
int bS7 = 0;
int lBS7 = 0;
CRGB leds[NUM_LEDS];
//CRGBPalette16 currentPalette;
//TBlendType currentBlending;
//byte prevKeyState = HIGH;
void setup() {
delay( 2000 ); // power-up safety delay
//FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
//currentBlending;
FastLED.addLeds<APA102>(leds, NUM_LEDS);
leds[
// pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(buttonPin7, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
// the main loop will constantly check to see if the button has been pressed
// if it has, a counter is incremented, and then some action can be taken
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
bS1 = digitalRead(buttonPin1);
bS2 = digitalRead(buttonPin2);
bS3 = digitalRead(buttonPin3);
bS4 = digitalRead(buttonPin4);
bS5 = digitalRead(buttonPin5);
bS6 = digitalRead(buttonPin6);
bS7 = digitalRead(buttonPin7);
// 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 went 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 went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// 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:
if (buttonPushCounter % 4 == 0) {
leds[0] = CRGB::Red;
FastLED.show();
delay(30); ;
} else {
leds[0] = CRGB::Blue;
FastLED.show();
delay(30); ;;
}
}
The 2nd time I read your "desire" in the 1st post, I got a different image in my head - most likely from reading it quickly the 1st time.
Its a great idea to incorporate multiple lighting schemes to a piece of art and allowing a user to progress through the schemes - I'd love to see a video of the final result.
Its unclear to me how many sections you visualize in your string of 50 LEDs.
You talked about 2 clicks in the initial post - just how many different sections are you planning?
If its only a few, then whats wrong with doing it with IF stmts.?
... so it must be more complicated than that or you'd be further along.
Example:
If you had 6 buttons (meaning 6 sections), each button can be on or off, in any combination, so you'd have 2^6 combinations or 64 possible outcomes - meaning 64 IF statements? Is that where you saw this heading?