#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
volatile int f_wdt = 0, flag = LOW;
int analogInPin = A1;
void setup() {
pinMode(0, OUTPUT);
pinMode(1, INPUT);
setup_watchdog(8); // approximately 4 seconds sleep
}
void loop() {
if (f_wdt >= 2) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt = 0; // reset flag
digitalWrite(0, HIGH); // let led blink
while (flag == HIGH) {
flag = digitalRead(1);
}
flag = LOW;
digitalWrite(0, LOW);
}
pinMode(0, INPUT); // set all used port to intput to save power
system_sleep();
pinMode(0, 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++; // set global flag
}
Actualy the pin 0 of Attiny should activate the mosfet but it does'nt work like this.
The output voltage seems to be too low to activate my Linkit ONE, I don't understand why but if I replace my attiny by an Arduino UNO it works correctly.
Are you sure the problem isn't just that you're doing something wrong with the sleep/wdt stuff? I'll bet the '45 is never even getting to the point where it tries to turn on the fet, due to some difference between '328p and '45.
If the Uno can drive the fet, there's no reason that the '45 can't too, running at the same voltage.
Also, you need a pulldown resistor on the gate of the mosfet, otherwise when you put it into input mode, the pin value will float, and that can turn the fet on (often only part-way on)
In fact, using the ATtiny , the MOSFET is active during a second, and replace in the off state .
I tested with LED instead of mosfet and LED shining before moving four seconds in standby and start over.
The tiny consumes so little in sleep that I have nothing to measure it. So I think it work fine.
For resistance, I must to put it betwen the pin 0 and the S1 pin of MOSFET ?
Is it possible that the tiny voltage is insufficient to close the mosfet ?
When you're driving motors, you need a diode (not a zener!) between the two wires going to the motor, such that it won't conduct normally. This clamps the back EMF when you turn off a motor (or other inductive load) - otherwise you can get a positive voltage spike on the negative side of the motor that exceeds Vcc when you turn off the motor, and this pulse could damage things. So you use diode to clamp it.
Decoupling caps prevent the part from being reset by brief voltage drops from things turning on off (including the part itself - without decoupling caps, parts can easily reset themselves). 0.1uf, ceramic (or tant), not electrolytic. Always put a decoupling cap on all IC's, as close to the IC as you can get it.