Skip to content

Instantly share code, notes, and snippets.

@rat
Created June 27, 2016 20:35
Show Gist options
  • Save rat/5bca3477f090dcd4c1d39b0facfa3883 to your computer and use it in GitHub Desktop.
Save rat/5bca3477f090dcd4c1d39b0facfa3883 to your computer and use it in GitHub Desktop.
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
#define RAIN_GAUGE_PIN 2
#define LED 13
volatile unsigned long last_rain_IRQ = 0; /* Debouncing */
volatile unsigned int rain_clicks = 0;
ISR(INT0_vect)
{
if (millis() - last_rain_IRQ > 10) {
last_rain_IRQ = millis();
rain_clicks++;
Serial.println("haha");
}
}
float get_rainfall()
{
float rainfall = (rain_clicks * 0.1);
rain_clicks = 0; /* Reseta a quantidade de chuva após cada consulta */
return rainfall;
}
void flash ()
{
pinMode (LED, OUTPUT);
for (byte i = 0; i < 10; i++)
{
digitalWrite (LED, HIGH);
delay (50);
digitalWrite (LED, LOW);
delay (50);
}
pinMode (LED, INPUT);
} // end of flash
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void setup () {
Serial.begin(9600);
delay(100);
pinMode(2, INPUT);
digitalWrite (2, HIGH);
EICRA = (0 << ISC00) | (0 << ISC01) | (0 << ISC10) | (0 << ISC11) ; //INT0 e INT1 no nível zero.
EIMSK = (0 << INT1) | (1 << INT0); //habilita INT0
sei(); //habilita interrupções globais, ativando o bit I do SREG
}
void loop ()
{
flash ();
// disable ADC
ADCSRA = 0; // Economia de energia
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = (1 << WDCE) | (1 << WDE);
// set interrupt mode and an interval
WDTCSR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0); // set WDIE, and 8 seconds delay
wdt_reset(); // pat the dog
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
noInterrupts (); // timed sequence follows
sleep_enable();
// turn off brown-out enable in software
MCUCR = (1 << BODS) | (1 << BODSE);
MCUCR = (1 << BODS);
interrupts (); // guarantees next instruction executed
sleep_cpu ();
// cancel sleep as a precaution
sleep_disable();
Serial.println(get_rainfall());
} // end of loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment