So I am brand new to coding in Arduino. I want to turn on my neopixel ring with a button push, and turn it off with another button push. I am getting an error message, 'button press' was not declared in this scope. I am not sure how to solve this or if my code will even do what I want. I also took the format for most of the code off reddit. Any help is appreciated. Thanks.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN 6
#define NUMPIXELS 12
#define ONSWITCH 4
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
bool switched_on = false;
void setup() {
pinMode(ONSWITCH, INPUT_PULLUP);
pixels.begin();
button_press = millis();
}
void loop() {
//read button "on" and flip the switched_on variable
if (digitalRead(ONSWITCH) == LOW) {
unsigned long buttonpress_start = millis();
while (digitalRead(ONSWITCH) == LOW); //wait how long the button is pressed
if (millis() - buttonpress_start > 250) { //Button pressed for longer than 250ms
switched_on = !switched_on; //Invert switched_on
}
}
if (switched_on == true) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(75, 0, 130));
pixels.show();
} else {
pixels.clear();
}
}
I am getting an error message, 'button press' was not declared in this scope
The compiler does not lie
button_press is not declared anywhere in the program let alone in the scope in which it is used, Mind you, even if it were declared, it is not used anywhere else in the program so what is it doing there ?
button_press is not declared anywhere in the program let alone in the scope in which it is used, Mind you, even if it were declared, it is not used anywhere else in the program so what is it doing there ?
This was part of the code that is from the source I got on reddit. Like I said, I am brand new so I wasn't sure if it was needed. After deleting that command I am able to turn on my leds, but pushing the button will not turn them off. Do you know of a command to fix this issue?
“ not declared” means you have not identified the variable to the compiler.
He, the compiler, needs to be told everything.
button_press = millis(); in setup() should be buttonpress_start = millis();
You have this line in setup() function button_press = millis(); however buttonpress_start is only defined in the loop() function.
To make buttonpress_start accessible inside setup() ‘and in’ loop() you need to make the variable a ‘global variable’.
Remove ‘unsigned long’ from this line in loop() unsigned long buttonpress_start = millis();
Next add unsigned long buttonpress_start; ahead of void setup() {
And yes, always tell us what your code is supposed to by adding comments to your lines of code.
Add:
pixels.show();
After
pixels.clear();
Homework, what does this example do ?
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define SWITCH 4
#define PIN 6
#define NUMPIXELS 12
byte counter = 1;
byte lastSWITCH;
unsigned long switchMillis;
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//*****************************************************************
void setup()
{
pixels.begin();
pixels.clear();
pixels.show();
pinMode(SWITCH, INPUT_PULLUP);
}
//*****************************************************************
void loop()
{
checkSwitches();
} //END of loop()
//*****************************************************************
void checkSwitches()
{
//check for a switch change every 250ms (1/4 second)
if (millis() - switchMillis < 250)
{
//it is not time to check our switches yet
return;
}
//it is time to check our switches
//restart the timer
switchMillis = millis();
byte currentState = digitalRead(SWITCH);
//has this switch changed state ?
if (lastSWITCH != currentState)
{
//the switch has change state, save this new state
lastSWITCH = currentState;
//has the switch been pushed (i.e. closed)
if (currentState == LOW)
{
//push ON, push OFF switch action
counter = 1 - counter;
//do we turn on the pixels ?
if (counter == 0)
{
for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(75, 0, 130));
}
pixels.show();
}
//turn off the pixels
else
{
pixels.clear();
pixels.show();
}
} //END of if (currentState == HIGH)
} //END of if (lastOnSwitch != currentState)
} //END of checkSwiches()