I have a toy I am modifying for my son after he accidentally broke it. the new light in the toy has 3 color cycle functions (using a NewPixle 12 ring), that part works fine. I am trying to use the buttons on the toy (testing with my own buttons on a breadboard).
I have the "Void loop" setup to step through a bunch of "if" statements and act or not act accordingly. the issue is that at the step where the code checks the state of a "timer" variable I have written it appears to set the timer back to zero.
I have some serial monitoring going on so I can see where it happens, but I cant sort out WHY, probably something simple I am missing, that or I am taking the total wrong approach, I am fairly new to Arduino and I am not a programmer by trade, so I am learning a lot as I go.
Code:
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, 3, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
int buttonApin = 8; //Setup buttonA on pin 8
int buttonBpin = 9; //Setup buttonB on pin 9
int buttonCpin = 10; //Setup buttonC on pin 10
int buttonONpin = 11; //Setup buttonON on pin 11
int pwrPin = 12; //This is the pin tht will give power to the neopixle ring
int buttonOnState = 0;
int timer = 0; //Setup timer variable and set it to 0
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
pinMode(buttonCpin, INPUT_PULLUP);
pinMode(buttonONpin, INPUT_PULLUP);
pinMode(pwrPin, OUTPUT);
Serial.begin(9600); //enable serial access
}
void loop()
{
Serial.print("Starting");
Serial.println(" ");
delay(1000);
//buttonOnState = digitalRead(buttonONpin);
//check the state of the power button
//if (buttonOnState == LOW)
// {
// timer = (timer + 1);
// Serial.print("test");
// }
if (digitalRead(buttonONpin) == LOW)
{
timer = (timer + 1);
Serial.print("Timer button pressed, timer = ");
Serial.print(timer);
Serial.println(" ");
delay(1000);
}
//Check if the timer is running - turn off power to the neopixle if timer is at zero
Serial.print("Checking if the timer is at zero, timer = ");
Serial.print(timer);
Serial.println(" ");
delay(1000);
if (timer = 0)
{
digitalWrite(pwrPin, LOW);
}
Serial.print("Checking if the timer is above zero, timer = ");
Serial.print(timer);
Serial.println(" ");
delay(1000);
//Check if the timer is running, if running turn ON power to the neopixle
if (timer > 0)
{
digitalWrite(pwrPin, HIGH);
timer = timer + 1;
Serial.print("Timer is counting up, timer = ");
Serial.print(timer);
Serial.println(" ");
delay(1000);
}
Serial.print("Checking if the timer is above 32000, timer = ");
Serial.print(timer);
Serial.println(" ");
delay(1000);
//Check the timer, if above 32000 set back to zero
if (timer > 32000)
{
timer = 0;
}
Serial.print("Checking if the timer is less than zero, timer = ");
Serial.print(timer);
Serial.println(" ");
delay(1000);
//Check the timer, if below zero (a negative number) set back to zero
if (timer < 0)
{
timer = 0;
}
if (digitalRead(buttonApin) == LOW)
{
SeaCycle(); //Rotate through 3 undersea looking colors
Serial.print("Running SeaCycle");
Serial.print(timer);
Serial.println(" ");
delay(1000);
}
if (digitalRead(buttonBpin) == LOW)
{
rainbow(20); //rotate through colors of the reinbow
Serial.print("Running rainbow");
Serial.print(timer);
Serial.println(" ");
delay(1000);
}
if (digitalRead(buttonCpin) == LOW)
{
rainbowCycle(20); //rotate a rainbow color wheel
Serial.print("Running rainbowCycle");
Serial.print(timer);
Serial.println(" ");
delay(1000);
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void SeaCycle(){
colorWipe(strip.Color(0, 200, 50), 500); // Green
colorWipe(strip.Color(0, 0, 255), 500); // Blue
colorWipe(strip.Color(70, 0, 255), 500); // Purple
}
Serial output:
Starting
Checking if the timer is at zero, timer = 0
Checking if the timer is above zero, timer = 0
Checking if the timer is above 32000, timer = 0
Checking if the timer is less than zero, timer = 0
Running SeaCycle0
Starting
Timer button pressed, timer = 1
Checking if the timer is at zero, timer = 1
Checking if the timer is above zero, timer = 0
Checking if the timer is above 32000, timer = 0
Checking if the timer is less than zero, timer = 0
Running SeaCycle0
Starting
Checking if the timer is at zero, timer = 0
Checking if the timer is above zero, timer = 0
Checking if the timer is above 32000, timer = 0
Checking if the timer is less than zero, timer = 0
Running SeaCycle0
Starting
Timer button pressed, timer = 1
Checking if the timer is at zero, timer = 1
Checking if the timer is above zero, timer = 0
Checking if the timer is above 32000, timer = 0
Checking if the timer is less than zero, timer = 0