first commit
This commit is contained in:
134
ATMEGA_SLEEP_ADC_RADIO_LUM/ATMEGA_SLEEP_ADC_RADIO_LUM.ino
Executable file
134
ATMEGA_SLEEP_ADC_RADIO_LUM/ATMEGA_SLEEP_ADC_RADIO_LUM.ino
Executable file
@@ -0,0 +1,134 @@
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h> // Power management
|
||||
#include <avr/wdt.h>
|
||||
#include <Wire.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
const byte LED = 13;
|
||||
const byte LUMINOSITE_PIN=A0;
|
||||
const unsigned long activation = 111269;
|
||||
const unsigned long idLum=2269;
|
||||
const unsigned long desactivation = 962111;
|
||||
const unsigned int delai = 11;
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
// watchdog interrupt
|
||||
ISR (WDT_vect)
|
||||
{
|
||||
wdt_disable(); // disable watchdog
|
||||
} // end of WDT_vect
|
||||
|
||||
void myWatchdogEnable()
|
||||
{
|
||||
// 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
|
||||
|
||||
byte old_ADCSRA = ADCSRA;
|
||||
// disable ADC to save power
|
||||
ADCSRA = 0;
|
||||
|
||||
// slow clock to divide by 256
|
||||
// clock_prescale_set (clock_div_256);
|
||||
|
||||
// ready to sleep
|
||||
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
||||
sleep_enable();
|
||||
|
||||
// turn off brown-out enable in software
|
||||
MCUCR = bit (BODS) | bit (BODSE);
|
||||
MCUCR = bit (BODS);
|
||||
sleep_cpu ();
|
||||
|
||||
// cancel sleep as a precaution
|
||||
sleep_disable();
|
||||
power_all_enable (); // enable modules again
|
||||
// reenable ADC
|
||||
ADCSRA = old_ADCSRA;
|
||||
}
|
||||
|
||||
|
||||
void setup () {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
|
||||
mySwitch.enableTransmit(2);
|
||||
|
||||
long vcc = readVcc();
|
||||
pinMode (LED, OUTPUT);
|
||||
digitalWrite (LED, HIGH);
|
||||
|
||||
pinMode (LED, INPUT);
|
||||
int lum = analogRead(A0);
|
||||
|
||||
mySwitch.send(activation, 24);
|
||||
(delai); //delayMicroseconds
|
||||
// LUX
|
||||
// R=K*L^-gamma
|
||||
// R étant la résistance pour un niveau d'éclairement L.
|
||||
myMessageSend(idLum,(1000.0 * lum / 1024.0));
|
||||
|
||||
mySwitch.send(desactivation, 24);
|
||||
delay(delai);
|
||||
Serial.print(vcc);
|
||||
Serial.print(" ");
|
||||
Serial.println(lum);
|
||||
delay (100);
|
||||
digitalWrite (LED, LOW);
|
||||
delay(10);
|
||||
// sleep for a total of 64 seconds (8 x 8)
|
||||
//for (int i = 0; i < 8; i++) {
|
||||
myWatchdogEnable ();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void myMessageSend(long id, long value) {
|
||||
|
||||
Serial.print("Send id="); Serial.print(id);
|
||||
Serial.print(" value="); Serial.println(value);
|
||||
|
||||
mySwitch.send(id, 24); //"000000000001010100010001");
|
||||
delay(delai);
|
||||
mySwitch.send(value, 24); //"000000000001010100010001");
|
||||
delay(delai);
|
||||
|
||||
//delay(5000);
|
||||
//delayMicroseconds(TWOTIME*8);
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// Read current supply voltage
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
long readVcc() {
|
||||
bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
|
||||
long result;
|
||||
// Read 1.1V reference against Vcc
|
||||
#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
|
||||
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||
ADMUX = _BV(MUX3) | _BV(MUX2);
|
||||
#else
|
||||
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
|
||||
#endif
|
||||
// ADCSRB = 0;
|
||||
|
||||
delay(2); // Wait for Vref to settle
|
||||
ADCSRA |= _BV(ADSC); // Convert
|
||||
while (bit_is_set(ADCSRA,ADSC));
|
||||
result = ADCL;
|
||||
result |= ADCH<<8;
|
||||
result = 1126400L / result; // Back-calculate Vcc in mV
|
||||
// ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
|
||||
|
||||
// analogReference(DEFAULT);
|
||||
|
||||
return result; // Vcc in millivolts
|
||||
}
|
||||
Reference in New Issue
Block a user