evert-arias / EasyButton

Arduino library for debouncing momentary contact switches, detect press, release, long press and sequences with event definitions and callbacks.
https://easybtn.earias.me
MIT License
452 stars 63 forks source link

Running EasyButton on an Adafruit Trinket (16MHz) #94

Open NohWayJose opened 3 months ago

NohWayJose commented 3 months ago

As soon as I add the code to attach the callbacks. it breaks and the LED doesn't light up. The only conceptual difference between the SinglePress example and my code (I think) is that the Adafruit Trinket doesn't have a Serial port, so Serial,begin() isn't used (and fails to compile if it is included). Is serial a required dependency (as per the example)?


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

#define COLOR_BUTTON_PIN   0 // button to cycle through OFF/WHITE/RED/GREEN/BLUE
#define BRIGHTNESS_BUTTON_PIN   1 // button to increment brightness factor from 0.1 to 1 in increments of 0.1 
#define PIXEL_PIN    2  // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 29  // Number of NeoPixels

// Buttons.
EasyButton COLOR_BUTTON(COLOR_BUTTON_PIN);
EasyButton BRIGHTNESS_BUTTON(BRIGHTNESS_BUTTON_PIN);

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

// button variables
float   bright        = 0.1;  // brightness factor
int     col           = 0;    // Currently-active colour index on colour array (of lightCol objects)

//colour array (OFF/WHITE/RED/GREEN/BLUE)
uint16_t colour[5][3]{ {0,0,0}, {255,255,255}, {255,0,0}, {0,255,0}, {0,0,255}};

// Callback for the brightness button.
void onBrightnessPressed() {
  bright += 0.1;
    if(bright > 1) {bright = 0;} // Advance to next brightness level, wrap around after #1
    //Serial.println(bright);
    renewStrip(bright, col);
}

// Callback for the colour change button.
void onColourPressed() {
  if(++col > 4) {col = 0;} // Advance to next mode, wrap around after #4
  //Serial.println(col);
  renewStrip(bright, col);
}

void renewStrip(float b, int c){ //b=brightness factor from 0.1 to 1 & c = index to colour array, containing colour objects (RGB values)
  uint8_t red   = static_cast<int>(colour[c][0]*b);
  uint8_t green = static_cast<int>(colour[c][1]*b);
  uint8_t blue  = static_cast<int>(colour[c][2]*b);

  for (int pixel = 0; pixel < strip.numPixels(); pixel++) {
// strip.Colour(R,G,B) converts the three uint8_t RGB values in the indexed colour array into a combined colour value for strip.Color
    strip.setPixelColor(pixel, strip.Color(red,green,blue));
  }
  strip.show();
}

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
    clock_prescale_set(clock_div_1);
  #endif
   // END of Trinket-specific code.

  // Initialize the buttons.
  COLOR_BUTTON.begin();
  BRIGHTNESS_BUTTON.begin();

  // Attach callbacks.
  BRIGHTNESS_BUTTON.onPressed(onBrightnessPressed);
  COLOR_BUTTON.onPressed(onColourPressed);

  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)
  renewStrip(0.1, 1);
}

void loop(){
  delay(500);
  COLOR_BUTTON.read();
  BRIGHTNESS_BUTTON.read();
}
NohWayJose commented 3 months ago

Just discovered that if I choose ONE button, it works! Is it meant to work with > 1 button?