Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




LED Effects - AVR s...
 
Share:
Notifications
Clear all

[Solved] LED Effects - AVR state machine for buttons

2 Posts
1 Users
0 Likes
1,926 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Excellent set of effects here. Bravo! I am wanting to use a few in my ATMEGA328 Pro Trinket 5V. Im aware it is essentially an Arduino Uno.

Your code works perfect, but I am wanting to splice in a single, double, and long press state into this. Ideally 2 separate buttons to have 3 states like this. I am only going to be using 5 animations and an off LED state, but I can not for the life of me, get a button library working with this.

I sat and tried to figure out state machines, and I came up with this simple code, hoping to drop it into yours here, but I can’t get the state machine to work. Any suggestions on implementing these button states? Thanks for any advice at all, because I just keep hitting brick walls and nobody is giving me the time of day.  

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define LED_PIN    8

#define LED_COUNT 10

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);


const int buttonPin = 3; 
bool secondPress = false;
unsigned long timerOne;
unsigned long timerTwo;
unsigned long pressTime;

void setup(){
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif

    pinMode(buttonPin, INPUT_PULLUP);
      strip.begin();           // INITIALIZE NeoPixel strip+++++++ object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)


}

void loop(){

if(buttonPin == LOW && secondPress == false) //first button press!
   timerOne = millis();
   secondPress = true;
   

if(buttonPin == LOW && secondPress == true) { //second press!
    timerTwo = millis();
    pressTime = timerTwo - timerOne;    //presstime now holds the total time between presses in milliseconds.   

    if(pressTime <= 100);{
functionGo;  // single press
    }
  if (pressTime > 300);{
    functionGo2;  // double press

        //don't forget to reset!
    timerOne = 0;
    timerTwo = 0;
    secondPress = false;
  }
  }
}



void functionGo() {
  colorWipe(strip.Color(255,   0,   0), 50); // Red
  colorWipe(strip.Color(  0, 255,   0), 50); // Green
  colorWipe(strip.Color(  0,   0, 255), 50); // Blue
}

void functionGo2(){
  colorWipe(strip.Color(  0,   0, 255), 50); // Blue
  colorWipe(strip.Color(  0, 255,   0), 50); // Green
  colorWipe(strip.Color(255,   0,   0), 50); // Red 
}

void colorWipe(uint32_t color, int wait) {
      for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
        strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
        strip.show();                          //  Update strip to match
      }
    }

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Looking at the initial code, my first concern would be if the AVR specifics could have a negative impact on the LED strip library (NeoPixel) since these use specific timing as well.

Looking closer, I do see that the loop() goes a little wrong as well.
For example there are a few things going wrong with your if-statements, for example this one:

if(buttonPin == LOW && secondPress == false)

 

First thing is to use more brackets, so that you get blocks which have a TRUE or FALSE state, like so:

if(  (buttonPin == LOW) && (secondPress == false)  )

 

Next you assigned the PIN used for the button to buttonPin, but that value will remain a const number 3, so it will never change between TRUE and FALSE.

Also I see just one button ...?

So it may be a good idea to start from scratch, ignoring state machine and led effects for a second and just get the buttons to work.

Note: another challenge will be reading the buttons when the effects are running (since they will not be detected when the effect is running).

I'll have to think a lot more about this one, as it won't be all that easy ... 😱 


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: