I am trying to write a code that lets you press a button to get an LED strip to light up certain LEDs. I'm trying the code below, and I can see the "store" integer increment on the serial monitor when I press the button. However, the LEDs don't do anything. I have checked the wiring and tried other sketches. The LEDs are working great. I'm very new at Arduino, so I wanted to see if I've got a major problem with the code. Anyone have any suggestions?
#include <FastLED.h>
#define DEBUG
#define NUM_LEDS 29
#define DATA_PIN 2
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 60
#define VOLTS 5
#define MAX_AMPS 500
//END DEFINES
CRGB leds[NUM_LEDS];
const int button=3;
int val;
int count=0;
int store;
void setup() {
#ifdef DEBUG
Serial.begin(9600);
Serial.println("Serial Communication Started");
#endif
FastLED.addLeds<CHIPSET,DATA_PIN,COLOR_ORDER>(leds,NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS,MAX_AMPS);
FastLED.setBrightness(BRIGHTNESS);
// put your setup code here, to run once:
pinMode(button, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
val=digitalRead(button);
if(val == HIGH){(store = count++);
Serial.print(store);
delay(250);}
if(store == 0){leds[0] = (255,0,0);
FastLED.show();}
}
The store variable is declared as an int and will have an initial value of zero. The button input is set to INPUT_PULLUP so if it is wired so that the pin is taken LOW when the button is pressed then it will be HIGH unless the button is pressed and store will be incremented every 250 milliseconds
Ah, I'm sorry, you are correct. So why did you call that symbol "MAX_AMPS" when you should have called it "MAX_MILLIAMPS"? Did you copy it without reading it, and not notice the error until I pointed it out?
Yep. That code is copied and I just rolled with it. Not good etiquette, I know. On you last comment, I do see the value go up. Is it not an integer or that shouldn't be printing anything to the serial monitor at all?
Yes, if you wire your button like that, the Arduino pin will read HIGH when you use INPUT_PULLUP. If you use INPUT, it might read HIGH or LOW at random, until the button is pressed, then it will definitely read HIGH. When the button is released it might read LOW but chances are it will continue to read HIGH. The question is, when will it definitely read LOW? When the button is not pressed? Sadly, no. The pin will only read LOW when connected to 0V.