Hmm... No luck as yet.
Using what I think is a fairly bare bones version of the code; it's still not working for me. I'm not sure if I've inserted the mentioned lines in the correct positions, but it still draws power throughout the sequence. If I try to inesert myServo.detach(); anywhere, the servo will not move at all.
Can anyone see where I am going wrong here? I tried to add Nicks lines too, however I obviously need to create some sort of variable for that to work?
Thanks,
Mark.
/*
* Watchdog Sleep Example
* Demonstrate the Watchdog and Sleep Functions
* LED on digital pin 0
*
* KHM 2008 / Lab3/ Martin Nawrath nawrath@khm.de
* Kunsthochschule fuer Medien Koeln
* Academy of Media Arts Cologne
*
* XXXXXXXXXXXXXXXXXXX originally by InsideGadgets (www.insidegadgets.com)
* to suit the ATtiny85 and removed the cbi( MCUCR,SE ) section
* in setup() to match the Atmel datasheet recommendations
*/
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#include "Servo8Bit.h"
Servo8Bit myServo; //create a servo object.
//a maximum of five servo objects can be created
int pinLed = 0;
//create the variable, init it to zero.
uint8_t originalValue = 0;
volatile boolean f_wdt = 1;
void setup(){
pinMode(pinLed,OUTPUT);
setup_watchdog(8); // approximately 4 seconds sleep
}
void loop(){
if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt=0; // reset flag
//Restore the TCCR1 register to re-enable the timer.
TCCR1 = originalValue;
//reattach the servo that was previously detached
myServo.attach(1,544,2200); //attach the servo to pin PB1
digitalWrite(pinLed,HIGH); // let led blink
for(int pos = 0; pos < 180; pos++) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(int pos = 180; pos > 1; pos--) // goes from 180 degrees to 0 degrees
{
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(pinLed,LOW);
//save the value of the Timer Counter Control Register 1
originalValue = TCCR1;
//turn off timer 1
TCCR1 = 0; //turn off timer 1
pinMode(pinLed,INPUT); // set all used port to intput to save power
system_sleep();
pinMode(pinLed,OUTPUT); // set all ports into state before sleep
}
}
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1; // set global flag
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.