Coding on tiny85, havent had much issues but this one is getting me.
Led turns on when power applied, then counter gets to 20, led back off and sleep happens.
Led turns on when button pressed, then counts to 20, led off and sleep.
When i press the button while the led is already on, it does not turn off and sleep like i want it to.
What am i missing here? Help appreciated!
#include <avr/sleep.h>
#include <avr/interrupt.h>
const int switchPin = 1;
const int statusLED = 4;
int running = 0;
int loopcounter=0;
void setup() {
pinMode(statusLED, OUTPUT);
}
void sleep()
{
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT2); // Use PB2 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
MCUCR |= _BV(SM1);
MCUCR &= ~_BV(SM0); // Select "Power-down" sleep mode
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // SLEEP
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT2); // Turn off PB2 as interrupt pin
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
}
ISR(PCINT0_vect) {
running++;
}
void loop() {
loopcounter++;
digitalWrite(statusLED, running);
delay(100);
if (!running){digitalWrite(statusLED, LOW);delay(100);sleep();}
if (loopcounter > 20 || running > 1){loopcounter = 0;running = 0;}
}