Hi,
I have been messing about with a LED light beacon that would run on an ATtiny85. It used the WDT to reduce power consumption. The base code for this is shamelessly borrowed from Nick Gammons piece on interrupts. The code runs great on a Uno which I have been using as a test bed but fails miserably at compiling with this error message:
exit status 1
'WDTCSR' was not declared in this scope
When the board is set for Atting25/45/85.
I'm assuming that WDTCSR is not valid for an ATtiny? What would the correct method? Any help gratefully accepted.
Best regards,
Steve.
//Includes
#include <avr/sleep.h> // Sleep functions
#include <avr/wdt.h> // Watch Dog Timer Functions
#include <avr/power.h> // Power Functions
//Definitions
#define LED_One 2 // first of the flashing LEDs. This is pin 2 on ATTing 85.
#define LED_Two 6 // Second of the flashing LEDs. This is pin 6 on ATTing 85.
#define ADC A3 //Light dependant resistor on pin 3
#define programJumper 7 //Programing jumper on pin 7
// Varibles
int lightLevel = 0; //current ligh level
int oldLevel = 0; // the old lightlevel
int storeLevel = 0; // the NV stored level
int prevRun = 0; // Non volatile run state
byte night = 0; // Night counter, dark for how long
int ledFlash = 0; // Test to see if it time to flash the LEDs
int hystLevel = 50; // What level of light is required to flash leds
int waitTime = 100; // The loop delays, to be replaced with WDT
byte start = 0; //Set started bit
int LED_Blink = 50; // The time the LEDs flash in mS used for delay
byte night_Count = 7; //Amount of time the night couter is incrementsed before LED flashing
//byte sleepTime = 0b000001;// Sleep set for 32ms
//byte sleepTime = 0b000010;// Sleep set for 64ms
//byte sleepTime = 0b000011;// Sleep set for 125ms
byte sleepTime = 0b000100;// Sleep set for 250ms
//byte sleepTime = 0b000101;// Sleep set for 500ms
// interrupt service routine for when button pressed
void wake ()
{
wdt_disable(); // disable watchdog
} // end of wake
// watchdog interrupt
ISR (WDT_vect)
{
wake ();
} // end of WDT_vect
void myWatchdogEnable (const byte interval)
{
noInterrupts (); // timed sequence below
MCUSR = 0; // reset various flags
WDTCSR |= 0b00011000; // see docs, set WDCE, WDE
WDTCSR = 0b01000000 | interval; // set WDIE, and appropriate delay
wdt_reset();
byte adcsra_save = ADCSRA;
ADCSRA = 0; // disable ADC
power_all_disable (); // turn off all modules
set_sleep_mode (SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
attachInterrupt (digitalPinToInterrupt (5), wake, LOW); // allow grounding pin D2 to wake us
interrupts ();
sleep_cpu (); // now goes to Sleep and waits for the interrupt
detachInterrupt (digitalPinToInterrupt (5)); // stop LOW interrupt on pin D2
power_all_enable (); // turn on all modules
ADCSRA = adcsra_save; // stop power reduction
} // end of myWatchdogEnable
//Led blinking blink_LED
void blink_LED(){
pinMode (LED_One, OUTPUT);
pinMode (LED_Two, OUTPUT);
digitalWrite (LED_One, HIGH);// flash led one
delay (LED_Blink);
digitalWrite (LED_One, LOW);
delay (LED_Blink);
digitalWrite (LED_Two, HIGH);//flash led two
delay (LED_Blink);
digitalWrite (LED_Two, LOW);
delay (LED_Blink);
} //end of blink_LED
//Intial start test
void started_Already(){
if (prevRun == 0){
for (int x = 0; x < 10; x++){
blink_LED(); //flash the LEDs to show first time turn on
} // end of blink
start = 1; // set the already started flag
prevRun = start;
}// end of flag store
}// end of start check
// Beacon mode where it flashes LEDS from time to time.
void beacon_Flash (){
if (lightLevel < 512)
{
night = night + 1;//
}
if (lightLevel > 512){
night = 0;
}
if (night > night_Count){ // check to see how long it has been dark for beacon
blink_LED(); //Drive the LEDs
night = 0; //reset counter
}
}// end of beacon LED flash
void setup (){
//print degug
//Serial.begin(115200); //setup serial port for debug strings
//pin setups
digitalWrite (5, HIGH); // pull-up on button
pinMode (ADC, INPUT); // Input for light level
pinMode (programJumper, INPUT); //Input for programming
analogReference (DEFAULT);
} // end of setup
void loop(){
wdt_reset(); // kill the dog
///const int storeLevel PROGMEM = {oldLevel};
started_Already(); // check for first time start
lightLevel = analogRead (ADC); //Get light level
//LED Drive
ledFlash = lightLevel - storeLevel;
if (ledFlash < 0){
ledFlash = 0;
}// Check for low light
if (ledFlash > hystLevel){
blink_LED();// Flash the LEDs
night = 0; //reset counter
}// End of light check
beacon_Flash(); // check for low light for a long time.
oldLevel = lightLevel;
storeLevel = oldLevel;
myWatchdogEnable (sleepTime); // 250mS seconds
}// end of loop