Ok my setup is as follows in the picture and code:
-
A nano runs code that uses wdt to set pin 13 high and low every so often. The idea is to save power and only turn on the motor a few times a day for a few seconds, then turn it off. I believe the code is fine.
-
the nano runs off a solar recharging battery pack that for now will run without sun so as to ascertain about how much power (for how long given the timing in code) will this battery pack power the nano and the motor. The pack outputs 5V since its made for charging phones and what not. I tested at 5.08V fully charged.
-
the motor is a small dc vibrating motor.
I just want to make sure the wiring is correct before I power it up.
NANO CONNECTIONS
GND to common GROUND of battery pack and motor.
Vin to 5V from battery pack
pin 13 to red cable on motor
#include <avr/wdt.h> // library for default watchdog functions
#include <avr/interrupt.h> // library for interrupts handling
#include <avr/sleep.h> // library for sleep
#include <avr/power.h> // library for power control
int nbr_remaining;
#define led 13
#define motorPin 10
ISR(WDT_vect){
// not hanging, just waiting
// reset the watchdog
wdt_reset();
}
void configure_wdt(void){
cli(); // disable interrupts for changing the registers
MCUSR = 0; // reset status register flags
// Put timer in interrupt-only mode:
WDTCSR |= 0b00011000; // Set WDCE (5th from left) and WDE (4th from left) to enter config mode,
// using bitwise OR assignment (leaves other bits unchanged).
WDTCSR = 0b01000000 | 0b100001; // set WDIE: interrupt enabled
// clr WDE: reset disabled
// and set delay interval (right side of bar) to 8 seconds
sei(); // re-enable interrupts
}
void sleep(int ncycles){
nbr_remaining = ncycles; // defines how many cycles should sleep
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
power_adc_disable();
while (nbr_remaining > 0){ // while some cycles left, sleep!
sleep_mode();
// CPU is now asleep and program execution completely halts!
// Once awake, execution will resume at this point if the
// watchdog is configured for resume rather than restart
// When awake, disable sleep mode
sleep_disable();
// we have slept one time more
nbr_remaining = nbr_remaining - 1;
}
// put everything on again
power_all_enable();
}
void setup(){
// use led 13 and put it in low mode
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
delay(1000);
// configure the watchdog
configure_wdt();
// blink twice
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(motorPin, HIGH);
delay(20000);
}
void loop(){
// sleep for a given number of cycles (here, 5 * 8 seconds) in lowest power mode
sleep(100); //8sec x 5 cycles=40s....8sec x 1000 cycles=8000sec or 133 mins
// usefull stuff should be done here before next long sleep
// blink three times
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}