1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include <Ultrasonic.h>
//Optimisation de la consommation
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
volatile int f_wdt=1;
// variables du projet
Ultrasonic ultrasonic1(2);
Ultrasonic ultrasonic2(4);
long distanceMesuree;
double tempsMax = 180.00; //temps en secondes
double tempsMin = 60.00; // temps en secondes
double tempsDesire, tempsIntervalUltrasonic;
double tempsIntervalUltrasonicMax = 30; //temps en seconde
// Watchdog Interrupt Service est exécité lors d’un timeout du WDT
ISR(WDT_vect) {
if(f_wdt == 0) {
f_wdt = 1; // flag global
}
}
// paramètre : 0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms, 6=1 sec,7=2 sec, 8=4 sec, 9=8 sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
// Clear the reset flag
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCSR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCSR = bb;
WDTCSR |= _BV(WDIE);
}
void enterSleep(void) {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode(); //Entre dans le mode veille choisi
// Le programme va reprendre ici après le timeout du WDT
sleep_disable(); // La 1ère chose à faire est de désactiver le mode veille
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
tempsMax = convertiSecondeToMilli(tempsMax);
tempsMin = convertiSecondeToMilli(tempsMin);
tempsIntervalUltrasonicMax = convertiSecondeToMilli(tempsIntervalUltrasonicMax);
//Optimisation de la consommation
//power_adc_disable(); // Convertisseur Analog / Digital pour les entrées analogiques
//power_spi_disable();
//power_twi_disable();
// Si pas besoin de communiquer par l’usb
//power_usart0_disable();
//Extinction des timers, attention timer0 utilisé par millis ou delay
//power_timer0_disable();
//power_timer1_disable();
//power_timer2_disable();
setup_watchdog(9);
}
void loop() {
// put your main code here, to run repeatedly:
if (f_wdt == 1) {
// Effectuer les mesures ici
f_wdt = 0; // Ne pas oublier d’initialiser le flag
Serial.println(" Sortie de veille ");
tempsDesire = random(tempsMin, tempsMax);
tempsIntervalUltrasonic = random(0, tempsIntervalUltrasonicMax);
Serial.print(" tempsDesiré : " );
Serial.println(tempsDesire);
Serial.print(" tempsIntervalUltrasonic : " );
Serial.println(tempsIntervalUltrasonic);
delay(tempsDesire);
emeteurOn();
Serial.println(" Mise en veille ");
enterSleep(); //Revenir en mode veille
} else {
/* Do nothing. */
}
}
void ultrasonicOn(int numUltrasonic) {
switch (numUltrasonic) {
case 1 :
ultrasonic1.MeasureInCentimeters();
Serial.println("Ultrasonic1 On");
delay(300);
break;
case 2 :
ultrasonic2.MeasureInCentimeters();
Serial.println( "Ultrasonic2 On" );
delay(300);
break;
default:
break;
}
}
void emeteurOn() {
ultrasonicOn(1);
delay(tempsIntervalUltrasonic);
ultrasonicOn(2);
}
double convertiSecondeToMilli(double valeur) {
double tempsMilli = valeur / 0.001;
return tempsMilli;
} |
Partager