Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/sleep.h>
- #include <avr/power.h>
- #include <EEPROM.h>
- #include <avr/wdt.h>
- #define LED_PIN (13)
- #define ENOUGH_READINGS 4 //how many readings til its time to write to eeprom
- #define E ENOUGH_READINGS
- int value =22;
- int value2=33;
- int wdtCtr,wdCtrLimit=0;
- int toEepromArCtr=0;
- int sensorPin = 0; //the analog pin the TMP36's Vout (sense
- int readingsAr[256];
- int toEepromAr[256]; //these values go into eeprom
- void setup() {
- Serial.begin(115200);
- Serial.println("Initialising...");
- delay(100); //Allow for serial print to complete.
- pinMode(LED_PIN,OUTPUT);
- MCUSR &= ~(1<<WDRF);
- /* In order to change WDE or the prescaler, we need to
- * set WDCE (This will allow updates for 4 clock cycles).
- */
- WDTCSR |= (1<<WDCE) | (1<<WDE);
- /* set new watchdog timeout prescaler value */
- // WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
- WDTCSR = 1<<WDP3; /* 4.0 seconds */
- /* Enable the WD interrupt (note no reset). */
- WDTCSR |= _BV(WDIE);
- Serial.println("Initialisation complete.");
- delay(100); //Allow for serial print to complete
- }
- void loop() {
- delay(550);
- // digitalWrite(LED_PIN, !digitalRead(LED_PIN));
- digitalWrite(LED_PIN,HIGH); //read some sensor
- //delay(500);
- int reading = analogRead(sensorPin); //do some sensor work
- // Serial.print("Reading>>");
- Serial.println(reading);
- readingsAr[wdtCtr]=reading;
- Serial.println(wdtCtr);
- if(wdtCtr>=ENOUGH_READINGS){
- Serial.println("Enough");
- printReadingsAr();
- Serial.println(meanReading());
- //toEepromArCtr++; //counts bytes going into eeprom
- EEPROM.write(toEepromArCtr++, meanReading());
- showEeprom();
- wdtCtr = 0;
- }
- // digitalWrite(LED_PIN,LOW);
- //Serial.print(value++);
- delay(3); //take out later
- enterSleep();
- //Serial.println(value2);
- }
- ISR(WDT_vect) {
- value2++;
- wdtCtr++;
- Serial.println("W");
- }
- void enterSleep(void)
- {
- set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
- sleep_enable();
- /* Now enter sleep mode. */
- digitalWrite(LED_PIN, !digitalRead(LED_PIN));//now sleeping (nearly)
- sleep_mode();
- /* The program will continue from here after the WDT timeout*/
- sleep_disable(); /* First thing to do is disable sleep. */
- /* Re-enable the peripherals. */
- power_all_enable();
- }
- void printReadingsAr(void) {
- for (int i =0;i<=ENOUGH_READINGS;i++) {
- Serial.print(readingsAr[i]);
- Serial.println("*");
- }
- }
- int meanReading (void){
- int total=0;
- for(int i =0;i<E;i++){
- total = total + readingsAr[i];
- }
- return total/E;
- }
- void showEeprom(void) {
- int value;
- for (int i =0;i<10;i++) {
- value = EEPROM.read(i);
- Serial.print(value);
- Serial.println("e");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment