Hello, New to this forum, although I have learned much from reading the posts here. I am completely new to low level programming. I only know some basics that I have taught myself in the last few weeks.
Here is my intent:
Trinket runs a loop with 2 modes. one mode(SCANNER) runs all the time except when BUTTON pressed(5v+ on pin 4 , in this case). When BUTTON pressed, mode switches to FLASHY. I am sure you have all seen these posted on line, but I made a few edits to both of these modes to work in my project.
So far, everything works perfectly. My only issue is that I would like to run the if (buttonState == HIGH) { for an adjustable amount of milliseconds(~200), which then reverts back to 'else'. This ~200ms is longer than the button 'press' time. I tried basic delays, but I think I need a way to repeat the FLASHY loop for (x)ms. Trying to do this without a timer library/code, if possible.
I know this is NOT the way to insert code....but could not find another way except .ino upload... THANKS for your time!
// this is working attempt at combining two programs FLASHY and SCANNER
// with SCANNER running in background and when momentary
// button pressed, FLASHY program runs .
#include <Adafruit_NeoPixel.h>
#ifdef AVR_ATtiny85 // Trinket, Gemma, etc.
#include <avr/power.h>
#endif
const int N_LEDS = 8; // number pixels(leds)in strip
const int PIN = 1; //neopixel strip
const int BUTTON = 4; // momentary input 5v+ to pin 4
int buttonState = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
uint8_t mode = 0, // Current animation effect
offset = 0; // Position of spinny eyes
uint32_t color = 0xff0000; // Start RED
uint32_t prevTime;
void setup() {
// initialize the LED pin as an output:
pinMode(PIN, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(BUTTON, INPUT);
#ifdef AVR_ATtiny85 // Trinket, Gemma, etc.
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
strip.begin();
strip.setBrightness(60); // brightness
prevTime = millis();
}
int pos = 0, dir = 1; // Position, direction of LEDS
void loop() {
uint8_t i;
uint32_t t;
int j;
// read the state of the pushbutton value:
buttonState = digitalRead(BUTTON);
if (buttonState == HIGH) {
//run the FLASHY loop
digitalWrite(PIN, mode);
switch (mode) {
case 0:
i = random(32);
strip.setPixelColor(i, random(color));
strip.show();
delay(5);
strip.setPixelColor(i, 0);
delay(5);
break;
}
} else {
// return FLASHY strip to SCANNER strip
digitalWrite(PIN, mode);
switch (mode) {
case 0:
// Draw 5 pixels centered on pos. setPixelColor() will clip any
// pixels off the ends of the strip, we don't need to watch for that.
strip.setPixelColor(pos - 2, 0x021401); // Dark green
strip.setPixelColor(pos - 1, 0x0c6b0a); // Medium green
strip.setPixelColor(pos , 0x19f215); // Center pixel is brightest
strip.setPixelColor(pos + 1, 0x0c6b0a); // Medium green
strip.setPixelColor(pos + 2, 0x021401); // Dark green
strip.show();
delay(150);
// erase and redraws pixel sequence.
for (j = -2; j <= 2; j++) strip.setPixelColor(pos + j, 0);
// 'center' pixel bounces off ends of strip
pos += dir;
if (pos < 0) {
pos = 1;
dir = -dir;
} else if (pos >= strip.numPixels()) {
pos = strip.numPixels() - 2;
dir = -dir;
}
break;
}
}
}