Hey all,
Now that I've got my ATtiny85 programmable, I wanted to run a little more complex code. First I'll describe what it is I'm intending to do.
I would like a 1 sec hold button press to both wake and put the ATtiny85 to sleep.
I would also like that same button to be able to run some toggle functions, after turning the ATtiny85 on.
The button will press through a few different functions like a bike light. Bright/dim/flash, that sort of thing.
This is as far as I've gotten with my code. The light remains on. When pressing the button the light does one of two things, either it will get brighter until the button is released or it will go off until the button is released.
the set up runs swell, but trouble with the function. I thought if I could first turn turn the ATtiny85 to sleep and on from the button call it would be a start but I'm having trouble doing that.
#include<avr/sleep.h>
#include<avr/interrupt.h>
const int switchPin=2;
const int statusLED=1;
int buttonState=0;
int lastButtonState=0;
int buttonPushCounter=0;
uint8_t debounceRead(int pin)
{
uint8_t pinState=digitalRead(switchPin);
uint32_t timeout=millis();
while((millis()-timeout)<10)
{
if(digitalRead(switchPin)!=pinState)
{
pinState=digitalRead(switchPin);
timeout=millis();
}
}//while
return pinState;
}//uint8_t
void setup() {
// put your setup code here, to run once:
pinMode(switchPin,INPUT);
digitalWrite(switchPin,HIGH);
pinMode(statusLED,OUTPUT);
//Flash quick sequence so we know setup has started
for(int k=0;k<10;k=k+1){
if(k%2==0){
digitalWrite(statusLED,HIGH);
}
else{
digitalWrite(statusLED,LOW);
}
delay(250);
}//for
}//setup
void sleep(){
GIMSK|=_BV(PCIE);//enable pin change interrupts
PCMSK|=_BV(PCINT2); //Use PB2 as interrupt pin
ADCSRA&=~_BV(ADEN);//ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//replaces above statment
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
}//sleep
ISR(PCINT0_vect){
//This is called when the interrupt occurs, but it is not mandatory to do anything with it
}
void loop() {
sleep();
//read the pushbutton input pin:
buttonState=debounceRead(switchPin);
//compare the buttonState to its previous state
if(buttonState!=lastButtonState){
//check if the pushbutton is pressed.
//if it is, the buttonState is HIGH:
if(buttonState==HIGH){
buttonPushCounter++;
}
//turn LED on:
//digitalWrite(statusLED,HIGH);
}else{
//turn LED off:
lastButtonState=buttonState;
}
// digitalWrite(statusLED,LOW);
//sleep();
if(buttonPushCounter%2==0){
digitalWrite(statusLED,HIGH);
}else{
digitalWrite(statusLED,LOW);
sleep();
}
}
Thanks Arduino forums,
-Jesse