Skip to content

Instantly share code, notes, and snippets.

@rat
Created June 24, 2016 13:41
Show Gist options
  • Save rat/694ec78471b79f58419ed516649767f5 to your computer and use it in GitHub Desktop.
Save rat/694ec78471b79f58419ed516649767f5 to your computer and use it in GitHub Desktop.
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#define RAIN_GAUGE_PIN 2
const byte 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 (500);
digitalWrite (LED, LOW);
delay (500);
}
pinMode (LED, INPUT);
} // end of flash
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void setup () {
Serial.begin(9600);
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
/** Interrupção do pluviômetro */
//pinMode(RAIN_GAUGE_PIN, INPUT);
//digitalWrite(RAIN_GAUGE_PIN, HIGH);
//attachInterrupt(0, rain_IRQ, FALLING);
}
void loop ()
{
flash ();
// disable ADC
ADCSRA = 0;
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and an interval
WDTCSR = bit (WDIE) | bit (WDP3) | bit (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 = bit (BODS) | bit (BODSE);
MCUCR = bit (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