ATtiny13a Pumpkin
Pumpkins.mp4
This project is a work in progress, I've built 2 of them, one with ATtiny85 and now condensed down for ATtiny13. There is a random flickering candle with a combination of Orange, yellow and white lights. There there are two eyes that fade back and forth.. Also still working on the code to overhaul it. Just through the above code together for this Halloween.
The Random section is thanks to :
Arduino code:
/* Pumkin Lighting aka Pumkin Guts
Sherman Stebbins 20210929 generic ATtiny13a with 1mhz
Data Type Length (bits/bytes) Valuesuint8_t 8 / 1 0 to 255int8_t 8 / 1 -128 to 127uint16_t 16 / 2 0 to 65535int16_t 16 / 2 -32768 to 32767uint32_t 32 / 4 0 to 4294967295int32_t 32 / 4 -2147483648 to 2147483647uint64_t 64 / 8 0 to 1.8*1019int64_t 64 / 8 -9.2*1018 to 9.2*1018 *///later stuff to add.//#include <avr/sleep.h> // Sleep Modes//#include <avr/power.h> // Power management//#include <avr/wdt.h> // Watchdog timer//#include <util/delay.h>//#include <avr/interrupt.h>
#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
//must be > 2char fadeAmount = 10; // how many points to fade the LED bychar fadeAmount2 = -10; // how many points to fade the LED by unsigned long previousMillis = 0;
#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 = LOW;
#define LedOrange PB2 //orange led//unsigned long countOrange=0;int countOrange=0;int ranNumOrange=3;int ledStateOrange = LOW;
int countSleep=0;int sleepAfter=5;
// the setup routine runs once when you press reset:void setup() { //DDRB = 0b00011111; // set LED pins as OUTPUT PB0-PB4 //PORTB = (1<<PB4)|(1<<PB1)|(1<<PB0); DDRB = (1<<DDB4)|(1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0);}
//ISR(WDT_vect) {//}
// the loop routine runs over and over again forever:void loop() {
unsigned long currentMillis = millis(); /////////////////////EYES////////////////////////////// if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis;
// set the brightness of pin 9: analogWrite(LedEye1, brightness); analogWrite(LedEye2, brightness2);
// change the brightness for next time through the loop: brightness = brightness + fadeAmount; brightness2 = brightness2 + fadeAmount2; if(brightness < 20){ brightness-(fadeAmount/2); } if(brightness2 < 20){ brightness2-(fadeAmount2/2); }
// reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 100) { fadeAmount = -1*fadeAmount; //change from positive to neg or vise versa } if (brightness2 <= 0 || brightness2 >= 120) { fadeAmount2 = -fadeAmount2; //change from positive to neg or vise versa } /*if(countSleep>sleepAfter){ countSleep=0; //goToSleep(9); //sleepNow(); sleep_mode(); // go to sleep and wait for interrupt... } countSleep++;*/ } // wait for 30 milliseconds to see the dimming effect
//yellow 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 }}
static uint16_t prng_lfsr16(void){ static uint16_t cnt16 = LFSR_SEED; return (cnt16 = (cnt16 >> 1) ^ (-(cnt16 & 1) & 0xB400));}
Sherman Stebbins 20210929 generic ATtiny13a with 1mhz
Data Type Length (bits/bytes) Valuesuint8_t 8 / 1 0 to 255int8_t 8 / 1 -128 to 127uint16_t 16 / 2 0 to 65535int16_t 16 / 2 -32768 to 32767uint32_t 32 / 4 0 to 4294967295int32_t 32 / 4 -2147483648 to 2147483647uint64_t 64 / 8 0 to 1.8*1019int64_t 64 / 8 -9.2*1018 to 9.2*1018 *///later stuff to add.//#include <avr/sleep.h> // Sleep Modes//#include <avr/power.h> // Power management//#include <avr/wdt.h> // Watchdog timer//#include <util/delay.h>//#include <avr/interrupt.h>
#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
//must be > 2char fadeAmount = 10; // how many points to fade the LED bychar fadeAmount2 = -10; // how many points to fade the LED by unsigned long previousMillis = 0;
#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 = LOW;
#define LedOrange PB2 //orange led//unsigned long countOrange=0;int countOrange=0;int ranNumOrange=3;int ledStateOrange = LOW;
int countSleep=0;int sleepAfter=5;
// the setup routine runs once when you press reset:void setup() { //DDRB = 0b00011111; // set LED pins as OUTPUT PB0-PB4 //PORTB = (1<<PB4)|(1<<PB1)|(1<<PB0); DDRB = (1<<DDB4)|(1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0);}
//ISR(WDT_vect) {//}
// the loop routine runs over and over again forever:void loop() {
unsigned long currentMillis = millis(); /////////////////////EYES////////////////////////////// if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis;
// set the brightness of pin 9: analogWrite(LedEye1, brightness); analogWrite(LedEye2, brightness2);
// change the brightness for next time through the loop: brightness = brightness + fadeAmount; brightness2 = brightness2 + fadeAmount2; if(brightness < 20){ brightness-(fadeAmount/2); } if(brightness2 < 20){ brightness2-(fadeAmount2/2); }
// reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 100) { fadeAmount = -1*fadeAmount; //change from positive to neg or vise versa } if (brightness2 <= 0 || brightness2 >= 120) { fadeAmount2 = -fadeAmount2; //change from positive to neg or vise versa } /*if(countSleep>sleepAfter){ countSleep=0; //goToSleep(9); //sleepNow(); sleep_mode(); // go to sleep and wait for interrupt... } countSleep++;*/ } // wait for 30 milliseconds to see the dimming effect
//yellow 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 }}
static uint16_t prng_lfsr16(void){ static uint16_t cnt16 = LFSR_SEED; return (cnt16 = (cnt16 >> 1) ^ (-(cnt16 & 1) & 0xB400));}
Microchip Studio c code:
Microchip Studio c code:
/* * ATtiny13-pumpin-guts.c 1.2mhz * * Created: 10/22/2022 13:14:29 * * Author : Sherman Stebbins */ #define F_CPU 1200000#include <avr/io.h>#include <avr/sleep.h> // Sleep Modes//#include <avr/power.h> // Power management//#include <avr/wdt.h> // Watchdog timer#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); //redundant? 5/11/23 /* 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<<WDTIE); 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(); //After wake up: 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 } }
}
#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); //redundant? 5/11/23 /* 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<<WDTIE); 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(); //After wake up: 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 } }
}