Skip to content

Instantly share code, notes, and snippets.

@petermllrr
Created Feb 26, 2022
Embed
What would you like to do?
SMOLSAT-1 Microcontroller Code
#include <avr/sleep.h>
#include <avr/wdt.h>
const int PIN_LED = 0;
// ADCSRA disables ADC, saves ~230uA
// set_sleep_mode(SLEEP_MODE_PWR_DOWN) powers down everything and wakes
// up from WDT.
void setup() {
pinMode(PIN_LED, OUTPUT);
ADCSRA &= ~(1<<ADEN);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
}
// watchdog_counter++;
ISR(WDT_vect) {
}
void loop() {
setup_watchdog(8);
sleep_mode();
fade();
}
// Sets the watchdog timer to wake us up, but not reset
// 0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms
// 6=1sec, 7=2sec, 8=4sec, 9=8sec
// From: http://interface.khm.de/index.php/lab/experiments/sleep_watchdog_battery/
void setup_watchdog(int timerPrescaler) {
// Limit incoming amount to legal settings
if (timerPrescaler > 9 ) timerPrescaler = 9;
byte bb = timerPrescaler & 7;
//Set the special 5th bit if necessary
if (timerPrescaler > 7) bb |= (1<<5);
// This order of commands is important and cannot be combined
// Clear the watch dog reset
MCUSR &= ~(1<<WDRF);
// Set WD_change enable, set WD enable
// Set new watchdog timeout value
WDTCR |= (1<<WDCE) | (1<<WDE);
WDTCR = bb;
// Set the interrupt enable, this will keep unit from resetting after each int
WDTCR |= _BV(WDIE);
}
void fade() {
int brightness = 0;
int fadeAmount = 5;
while(true) {
brightness = brightness + fadeAmount;
analogWrite(PIN_LED, brightness);
if (brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
if (brightness <= 0) {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment