Hi guys,
I'm fairly new to the arduino and programming in general. I am trying to write a sketch that is using the ATtiny85(Data sheet here:http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf). I would like to incorporate the sleep and watchdog timer functions. I was wondering if I have a different switch case if I could have different times that the sketch will be in sleep. For each state, there are different delays between the execution of loop, I would like to power the board down in between the loops. Any help would be greatly appreciated!!
#include <avr/wdt.h>
#include <avr/sleep.h>
const int motor = 3;
const int piezo = 2;
const int button1 = 1;
const int button2 = 0;
int buttonPress1 = 0;
int buttonPress2 = 0;
int val;
int count = 1;
int push;
int lastButtonState1 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int old = 0;
int state = 0;
void setup()
{
wdt_disable();
pinMode(motor, OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
while (buttonPress2 != HIGH) {
buttonPress1 = digitalRead(button1);
if (buttonPress1 == 1) {
delay(50);
buttonPress1 = digitalRead(button1);
if (buttonPress1 == 0);
} else {
delay(100);
}
buttonPress2 = digitalRead(button2);
if (buttonPress2 == 1) {
delay(50);
buttonPress2 = digitalRead(button2);
if (buttonPress2 == 0);
} else {
delay(100);
}
val = digitalRead(button1);
if (val == HIGH) {
push = count ++;
delay(100);
lastButtonState1 = buttonState1;
if (count >= 4) {
count = 0;
delay(10);
}
}
buttonPress2 = digitalRead(button2);
if (buttonPress2 == 1) {
delay(50);
buttonPress2 = digitalRead(button2);
if (buttonPress2 == 0);
} else {
delay(100);
}
}
switch (push) {
case 1:
tone(500, 750);
delay(1500);
tone(600, 750);
delay(5500);
old = state;
break;
case 2:
tone(500, 750);
delay(1500);
tone(600, 750);
delay(10500);
old = state;
break;
case 3:
tone(500, 750);
delay(1500);
tone(600, 750);
delay(20500);
old = state;
break;
default:
tone(500, 750);
delay(1500);
tone(600, 750);
delay(5500);
old = state;
break;
}
}
void delayWDT(byte timer){
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
ADCSRA &= ~(1<<ADEN);
WDTCR |= 0b0100100;
WDTCR = 0b01100000 | timer;
wdt_reset();
sleep_cpu();
sleep_disable();
ADCSRA |= (1<<ADEN);
}
ISR (WDT_vect)
{
wdt_disable();
MCUSR = 0;
}