Please can anyone can help me, I would be eternally grateful. I’ve been trying to get this code to work all day… It’s giving me a headache and i’m supposed to have it finished to show my superiors tomorrow.
I think what i need this circuit to do is very simple, i’m using an Arduino Uno and WS2801 LED strip. and a button… all i need is for all the leds to be white, then when the button is pressed… a red chaser moves around the strip several times… before automatically going back to all being white again (and ready to be pushed again and again)
WHERE I’M AT:
I have a strip of 124 LEDS on pin 2 (this goes to the LED strip data wire)
I have a momentary switch on pin 4 which either reads HIGH or LOW.
(Pretty sure when it is held down it reads HIGH)
and I have two LED PATTERNS/states…
FIRST PATTERN: about 20 red leds in a row chasing along the strip indefinetly.
SECOND PATTERN: Single white LED chasing along the strip indefinetly.
when turned on, the led strip is in its first pattern (good), and when i HOLD down the button is stays in its second pattern. (good)
WHAT I NEED HELP WITH:
I need the first state to light all the leds up white by default (255,255,255) (This is the resting state) without having to manually write every single led. (Anyone know a nice way of setting all the leds to the same value in a short piece of code?)
then when the button is pushed i need the second pattern to run for a while before it goes back to its first state automatically. the button isn’t intended to be held down. its simply supposed to be hit… the leds chase for a while (lets say 10 seconds or so) then the leds go back to being white. (back to the rest state)
Much appreciate, KVR,
This is seriously troubling me and i cannot express how thankful i would be if somebody could guide me in through this before tomorrow.
code below.
[code]#include “FastLED.h”
// How many leds are in the strip?
#define NUM_LEDS 124
// Data pin that led data will be written out over
#define DATA_PIN 2
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define CLOCK_PIN 3
// This is an array of leds. One item for each led in your strip.
CRGB leds[NUM_LEDS];
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
// 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
// This function sets up the leds and tells the controller about them
void setup() {
// sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(2000);
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
//SETS STEP TIMING
#define STEPDURATION 10
unsigned long stepTime;
unsigned long lastLEDupdate;
//SETTING FIRST PATTERN
void setLEDcolors(unsigned long time)
{
memset(leds,0,sizeof(leds));
int led1=((time)%NUM_LEDS);
leds[led1]=CRGB(25,0,0);
int led2=((time+1)%NUM_LEDS);
leds[led2]=CRGB(50,0,0);
int led3=((time+2)%NUM_LEDS);
leds[led3]=CRGB(75,0,0);
int led4=((time+3)%NUM_LEDS);
leds[led4]=CRGB(100,0,0);
int led5=((time+4)%NUM_LEDS);
leds[led5]=CRGB(125,0,0);
int led6=((time+5)%NUM_LEDS);
leds[led6]=CRGB(150,0,0);
int led7=((time+6)%NUM_LEDS);
leds[led7]=CRGB(175,0,0);
int led8=((time+7)%NUM_LEDS);
leds[led8]=CRGB(200,0,0);
int led9=((time+8)%NUM_LEDS);
leds[led9]=CRGB(225,0,0);
int led10=((time+9)%NUM_LEDS);
leds[led10]=CRGB(255,0,0);
int led11=((time+10)%NUM_LEDS);
leds[led11]=CRGB(225,0,0);
int led12=((time+11)%NUM_LEDS);
leds[led12]=CRGB(200,0,0);
int led13=((time+12)%NUM_LEDS);
leds[led13]=CRGB(175,0,0);
int led14=((time+13)%NUM_LEDS);
leds[led14]=CRGB(150,0,0);
int led15=((time+14)%NUM_LEDS);
leds[led15]=CRGB(125,0,0);
int led16=((time+15)%NUM_LEDS);
leds[led16]=CRGB(100,0,0);
int led17=((time+16)%NUM_LEDS);
leds[led17]=CRGB(75,0,0);
int led18=((time+17)%NUM_LEDS);
leds[led18]=CRGB(50,0,0);
int led19=((time+18)%NUM_LEDS);
leds[led19]=CRGB(25,0,0);
int led20=((time+19)%NUM_LEDS);
FastLED.show();
}
//SETTING SECOND PATTERN
void setLEDcolors2(unsigned long time)
{
memset(leds,0,sizeof(leds));
int led1=(time%NUM_LEDS);
leds[led1]=CRGB(255,255,255);
FastLED.show();
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
{
if (millis()-lastLEDupdate>=10)
{
lastLEDupdate+=STEPDURATION;
stepTime++;
setLEDcolors(stepTime);
}
}
}
else {
if (millis()-lastLEDupdate>=10)
{
lastLEDupdate+=STEPDURATION;
stepTime++;
setLEDcolors2(stepTime);
}
}
}
[/code]
