/* * ATtiny85-Pumkin-guts.c * Works Great!!! * Created: 10/23/2022 12:47:25 * Author : Sherman Stebbins */ 
#define F_CPU 1000000#include <avr/io.h>#include <avr/sleep.h>    // Sleep Modes#include <util/delay.h>#include <avr/interrupt.h>
#define down 0#define up 1#define sleepMaxLoops  10#define maxCandleLoops 3
#define LedEye1 PB0           // the PWM pin the LED is attached tochar brightness = 0;    // how bright the LED is //same as uint8_t
#define LedEye2 PB1 //PB4           // the PWM pin the LED is attached touint8_t brightness2 = 100;    // how bright the LED is
//candle:///////////////////////////////////////////////////////////#define LFSR_SEED       (91)const long interval = 80; //no diff//#define interval 80
#define LedYellow PB4 //PB1 //yellow ledconst char ranVar[3]={1,4,5};//,5,90,4,70,50,5,300,5};char ranSelect=0;const uint8_t ranMax=10;int countYellow = 0;int ranNumYellow=3;
#define LedWhite PB3 //white ledint countWhite=0;int ranNumWhite=3;int ledStateWhite = 0;
#define LedOrange PB2 //orange led//unsigned long countOrange=0;int countOrange=0;int ranNumOrange=3;int ledStateOrange = 0;
uint8_t candleLoop = 0;
//end of candle////////////////////////////////////////////
void SleepNow(void);static uint16_t prng_lfsr16(void);void Candle(void);
int main(void){	CLKPR=0b10000000;//change clock	CLKPR=0b00000011;//set within 4 cycles and clear bit		/* Clear WDRF in MCUSR */	MCUSR &= ~(1<<WDRF);	/* Write logical one to WDCE and WDE */	/* Keep old prescaler setting to prevent unintentional time-out */	WDTCR |= (1<<WDCE) | (1<<WDE);	/* Turn off WDT */	WDTCR = 0x00;			TCCR0A = 0b11100011; //with A=non-invert and B=invert then last 3 bits set to fast PWM	TCCR0B = 0b00000011; //prescaler to 64	OCR0A = 150;	OCR0B = 150;		uint8_t count = 0;	uint8_t direction = 0;	uint8_t sleepCount = 0;
	int loops = 0;	DDRB = 0b00001100;	for(uint8_t i=0;i<100;i++){		Candle();		_delay_ms(50);	}	DDRB = 0b00001111;	while (1)	{		if(loops>200){			if(direction){				if(++count > 254){					direction = down;				}				}else{				if(--count < 1){					direction = up;					sleepCount++;					if(sleepCount>sleepMaxLoops){						sleepCount=0;						SleepNow();					}				}			}			OCR0A = count;			OCR0B = count;			loops=0;					}		//	_delay_ms(100000);		loops++;						////////////Candle///////////////////////////////////////		Candle();		//////end of candle///////////////////////	}}
static uint16_t prng_lfsr16(void){	static uint16_t cnt16 = LFSR_SEED;	return (cnt16 = (cnt16 >> 1) ^ (-(cnt16 & 1) & 0xB400));}
void SleepNow(void){	PORTB = 0b00000000;	DDRB = 0b00000000;	// pre scale timer to 8s so we can measure current	WDTCR |= (1<<WDP3 )|(0<<WDP2 )|(0<<WDP1)|(0<<WDP0); // WDP3, WDP0 = 8s // WDP3 = 4s	//	WDTCR = 0b00101001;		// Enable watchdog timer interrupts	WDTCR |= (1<<WDE);	sei(); // Enable global interrupts	// Use the Power Down sleep mode	//ADCSRA &= ~(1<<ADEN);	//ACSR = (1<<ACD); //Disable the analog comparator	PRR = 0b00000001;	set_sleep_mode(SLEEP_MODE_PWR_DOWN);	sleep_mode();	cli();
	DDRB = 0b00001111;}
void Candle(){	//yellow	if(++candleLoop > maxCandleLoops){		candleLoop=0;		countYellow++;		if(countYellow>=ranNumYellow){			PORTB ^= _BV(LedYellow); //Toggle LED			countYellow=0;			ranNumYellow=((prng_lfsr16()/15));//Random number		}
		//white		countWhite++;		if(countWhite>ranNumWhite){			//digitalWrite(LedWhite, ledStateWhite = ledStateWhite ? LOW : HIGH);			PORTB ^= _BV(LedWhite);			countWhite=0;			//ranNumWhite= 750;//random(1000);			ranNumWhite=(prng_lfsr16()/30);//random number			//delay(100);		}
		//orange		countOrange++;		if(countOrange>ranNumOrange){			//analogWrite(LedOrange, ledStateOrange = ledStateOrange ? 0 : 255);			//PORTB ^= _BV(LedOrange);			PORTB ^= (1<<LedOrange);			countOrange=0;			//ranNumOrange=350;//random(1800);			ranNumOrange=(prng_lfsr16()/50);//random number		}	}
}