I have a very rudimentary setup with an arduino nano, an LED strip, a pushbutton and a resistor.
I was trying to use the pushbutton as a toggle, using this code
#include <stdio.h>
#include <stdlib.h>
#include<FastLED.h> // header file
#define NUM_LEDS 14 // number of led present in your strip
#define DATA_PIN 12 // digital pin of your arduino
CRGB leds[NUM_LEDS];
const int button = 2; // GPIO 8 for the button
int status = false;
void setup() {
//Serial.begin(9600);
pinMode(button, INPUT); // define button as an input
pinMode(DATA_PIN, OUTPUT); // define LED as an output
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
if (digitalRead(button)==true) { // if button is pressed
// flip status
//Serial.println("Changing status");
status = !status;
//Serial.println(status);
toggle(status);
}
while (digitalRead(button) == true) {
//Serial.println("while loop");
delay(50);
//Serial.println(status);
}//
delay(50);
} // begin again
void toggle(bool status) {
if (status == true) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White; //set the all leds to white
}
FastLED.show();
} else {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black; //set the all leds off
}
FastLED.show();
}
}
I am powering this from a battery (PNY model 12200, 2200 mAh, Output 5 V=1A) via USB into the arduino board. I am having some issues. Mainly, the lights would go on by themselvs after a while, maybe not full power but still would be on. Not sure where the problem is, any hints appreciated.
I also sometimes get the lights turned off, maybe I'm feeding current into the wrong place and a safety mechanism either on the arduino or the battery stick turns everything off to protect itself?
while(digitalRead(button) == true);{
I doubt the semicolon there is intentional. It may help to format your code.Tools/Auto Format
will do that for you. If the commented out code isn't relevant to the question you might remove it.