//real candle simulation 
//based on this post http://petemills.blogspot.com/2012/02/attiny-candle.html

#include <FastLED.h>

#define DATA_PIN    0                //set your leds datapin   change to 32 for m5 atom lite
#define LED_TYPE    WS2812B          //leds type
#define COLOR_ORDER GRB              //color order of leds

#define MAX_POWER_MILLIAMPS 700      //write here your power in milliamps. default i set 800 mA for safet

#define NUM_LEDS 1                   
byte brigtness = 255;


const byte candleRaw [] PROGMEM =  {

0x10, 0x00, 0x00, 0x00, 0x77, 0x7B, 0x7D, 0x7B, 0x78, 0xBA, 0x95, 0x53, 0x5E, 0x3E, 0x3E, 0x43, 0x52, 0x35, 0x3E, 0x75, 0x95, 
0x10, 0x00, 0x10, 0x00, 0x4B, 0x65, 0x7B, 0x52, 0x63, 0x58, 0x6B, 0x55, 0x62, 0x77, 0x72, 0x87, 0x85, 0x8C, 0x5D, 0x7A, 0x2E, 
0x10, 0x00, 0x20, 0x00, 0x53, 0x5D, 0x50, 0x62, 0x55, 0x6F, 0x67, 0x58, 0x78, 0x4E, 0x55, 0x95, 0x6B, 0x6D, 0x7D, 0x73, 0x73, 
0x10, 0x00, 0x30, 0x00, 0x7D, 0x5B, 0x6B, 0x68, 0x6A, 0x6A, 0x60, 0x6B, 0x77, 0x77, 0x98, 0x7A, 0x87, 0x60, 0x5B, 0x6B, 0xC9, 
0x10, 0x00, 0x40, 0x00, 0x53, 0x4A, 0x53, 0x68, 0x45, 0x3B, 0x65, 0x67, 0x9C, 0x60, 0x67, 0x53, 0x73, 0x75, 0x63, 0x8A, 0x81, 
0x10, 0x00, 0x50, 0x00, 0x7F, 0x83, 0x88, 0x80, 0x63, 0x58, 0x58, 0x6B, 0x7A, 0x78, 0x7B, 0x83, 0x8A, 0x87, 0x8A, 0x85, 0x08, 
0x10, 0x00, 0x60, 0x00, 0x83, 0x88, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8C, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x88, 0x83, 0x7F, 0x0B, 
0x10, 0x00, 0x70, 0x00, 0x7D, 0x7B, 0x7A, 0x78, 0x77, 0x75, 0x70, 0x70, 0x72, 0x70, 0x70, 0x4D, 0x41, 0x6D, 0x68, 0x60, 0xB5, 
0x10, 0x00, 0x80, 0x00, 0x35, 0x35, 0x3D, 0x3B, 0x41, 0x45, 0x52, 0x5E, 0x41, 0x53, 0x5D, 0x60, 0x65, 0x6A, 0x50, 0x48, 0xA0, 
0x10, 0x00, 0x90, 0x00, 0x4B, 0x4E, 0x35, 0x35, 0x31, 0x33, 0x33, 0x36, 0x3B, 0x40, 0x50, 0x4E, 0x52, 0x5D, 0x60, 0x53, 0x15, 
0x10, 0x00, 0xA0, 0x00, 0x56, 0x4B, 0x35, 0x2D, 0x2E, 0x2E, 0x35, 0x38, 0x38, 0x39, 0x3B, 0x38, 0x31, 0x58, 0x40, 0x60, 0x77, 
0x10, 0x00, 0xB0, 0x00, 0x4D, 0x50, 0x5A, 0x5D, 0x43, 0x40, 0x53, 0x58, 0x5A, 0x55, 0x4E, 0x31, 0x31, 0x2B, 0x2E, 0x33, 0xD3, 
0x10, 0x00, 0xC0, 0x00, 0x31, 0x36, 0x35, 0x36, 0x38, 0x39, 0x39, 0x38, 0x40, 0x4A, 0x41, 0x3B, 0x50, 0x62, 0x40, 0x36, 0x4E, 
0x10, 0x00, 0xD0, 0x00, 0x29, 0x2D, 0x45, 0x5E, 0x5D, 0x52, 0x3E, 0x33, 0x3B, 0x43, 0x35, 0x45, 0x38, 0x35, 0x31, 0x33, 0x3E, 
0x10, 0x00, 0xE0, 0x00, 0x36, 0x36, 0x39, 0x36, 0x38, 0x3B, 0x41, 0x36, 0x36, 0x30, 0x39, 0x33, 0x2B, 0x29, 0x33, 0x5A, 0x98, 
0x10, 0x00, 0xF0, 0x00, 0x63, 0x56, 0x41, 0x3D, 0x50, 0x52, 0x55, 0x60, 0x65, 0x55, 0x3B, 0x30, 0x2E, 0x30, 0x3B, 0x4E, 0x66, 
0x10, 0x01, 0x00, 0x00, 0x36, 0x2E, 0x2B, 0x3B, 0x39, 0x3D, 0x4A, 0x50, 0x3D, 0x45, 0x58, 0x4E, 0x4B, 0x4E, 0x4A, 0x45, 0xC5, 
0x10, 0x01, 0x10, 0x00, 0x58, 0x4B, 0x55, 0x5D, 0x5B, 0x56, 0x58, 0x5E, 0x60, 0x77, 0x5E, 0x38, 0x5A, 0x52, 0x46, 0x4B, 0x79, 
0x10, 0x01, 0x20, 0x00, 0x50, 0x4A, 0x4A, 0x35, 0x4E, 0x41, 0x2E, 0x36, 0x36, 0x38, 0x52, 0x4B, 0x46, 0x3B, 0x33, 0x40, 0xC4, 
0x10, 0x01, 0x30, 0x00, 0x4E, 0x60, 0x5A, 0x50, 0x4D, 0x43, 0x4A, 0x50, 0x4B, 0x48, 0x40, 0x3D, 0x40, 0x46, 0x52, 0x5B, 0xFA, 
0x10, 0x01, 0x40, 0x00, 0x62, 0x63, 0x63, 0x5B, 0x52, 0x46, 0x5B, 0x43, 0x55, 0x45, 0x26, 0x35, 0x3B, 0x5B, 0x43, 0x4D, 0xDB, 
0x10, 0x01, 0x50, 0x00, 0x40, 0x56, 0x58, 0x5A, 0x5D, 0x50, 0x46, 0x45, 0x45, 0x41, 0x3B, 0x43, 0x72, 0x87, 0x90, 0x8A, 0x08, 
0x10, 0x01, 0x60, 0x00, 0x8F, 0x97, 0x9D, 0x95, 0x73, 0x65, 0x6B, 0x4D, 0x46, 0x45, 0x55, 0x55, 0x41, 0x56, 0x55, 0x55, 0x31, 
0x10, 0x01, 0x70, 0x00, 0x56, 0x5A, 0x5A, 0x5B, 0x5E, 0x56, 0x62, 0x55, 0x65, 0x58, 0x5A, 0x62, 0x68, 0x6D, 0x6D, 0x6B, 0x89, 
0x10, 0x01, 0x80, 0x00, 0x68, 0x6A, 0x6F, 0x65, 0x6D, 0x31, 0x6F, 0x55, 0x48, 0x50, 0x55, 0x67, 0x5A, 0x41, 0x55, 0x5E, 0xC5, 
0x10, 0x01, 0x90, 0x00, 0x60, 0x65, 0x68, 0x68, 0x63, 0x60, 0x6A, 0x60, 0x67, 0x6A, 0x7F, 0x83, 0x8C, 0x87, 0x88, 0x92, 0x3D, 
0x10, 0x01, 0xA0, 0x00, 0x8D, 0x8F, 0x88, 0x8C, 0x8C, 0x85, 0x82, 0x6A, 0x4E, 0x35, 0x23, 0x11, 0x19, 0x43, 0x3B, 0x41, 0x93, 
0x10, 0x01, 0xB0, 0x00, 0x67, 0x4A, 0x4A, 0x3B, 0x2E, 0x30, 0x45, 0x41, 0x4A, 0x58, 0x48, 0x70, 0x5B, 0x6D, 0x72, 0x62, 0x2F, 
0x10, 0x01, 0xC0, 0x00, 0x75, 0x67, 0x56, 0x5A, 0x5E, 0x55, 0x4D, 0x77, 0x53, 0x2D, 0x36, 0x41, 0x5D, 0x55, 0x40, 0x40, 0x03, 
0x10, 0x01, 0xD0, 0x00, 0x40, 0x40, 0x40, 0x3E, 0x41, 0x5E, 0x82, 0x92, 0x88, 0x88, 0x90, 0x94, 0x88, 0x85, 0x7B, 0x63, 0x4F, 
0x10, 0x01, 0xE0, 0x00, 0x55, 0x53, 0x56, 0x55, 0x50, 0x53, 0x55, 0x03, 0x34, 0x01, 0x3A, 0x7E, 0xFF, 0x01, 0x60, 0x3E, 0x36, 
0x10, 0x01, 0xF0, 0x00, 0x3E, 0x28, 0x01, 0x8E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x16, 
0x00, 0x00, 0x00, 0x01, 0xFF

uint16_t masslen = sizeof(candleRaw);
uint16_t index = 0;

int main() {
  while (true) {
    // byte randomVal = pgm_read_byte (candleRaw+(index++)%masslen);
    // byte randomVal = getRandom();
    byte randomVal = lcgRandom();  
    // byte randomVal = random_two_shifts();

    leds [0] = CHSV (0, 255, randomVal);
    // if (random8(100)>10) leds [0].setHue(random8(15)); else leds [0] = 0;
    // delay(random8(30,100));

uint8_t getRandom(void) {
 const uint8_t randomvals[] PROGMEM = {

  static uint8_t random1, random2;
  if (random1 == 0) {
    random2 += 0x41;
  return pgm_read_byte(randomvals + random1) ^ random2;

uint8_t lcgRandom (){   //linear congruential generator
  static byte randomNum;
  randomNum = (randomNum*5) + 129;
  return (randomNum); 

uint32_t random_two_shifts() {  //found this in https://cdn.hackaday.io/files/10721458687264/bh-badge-animate.c 
  static uint32_t r_z = 362436069, r_w = 521288629;

	r_z = 36969 * (r_z & 65535) + (r_z >> 16);
  r_w = 18000 * (r_w & 65535) + (r_w >> 16);

  return (r_z << 16) + r_w;