Cave-a-Papa/code/cavapapa/cavapapa.ino

145 lines
3.5 KiB
Arduino
Raw Permalink Normal View History

/*
Cave_a_Papa
Copyright (C) 2017 Pierrick C.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.
If not, see <http://www.gnu.org/licenses/>
*/
2017-01-07 21:31:31 +01:00
#ifdef __AVR__
#include <avr/power.h>
#endif
#define LED_R_PIN 11
#define LED_V_PIN 10
#define LED_B_PIN 9
#define RELAY_PIN 2
#define BAT_PIN A1 // Analog Input 0 = digital PIN 2
#define VMAX 1250
#define V_GREEN 1170 // tension pour vert pur
#define VMIN 950
#define V_ALERTE 970 // provoque clignotement 3 fois rapide
#define V_OFF 920 // provoque clignotement pendant 15s puis OFF
#define OFF_DELAY 10 // delais de clignotement (en secondes) avant extinction à l'atteinte de V_OFF
2017-01-07 21:31:31 +01:00
#define INIT_BLINK 100
byte timer = 100;
byte low_bat_counter = 0;
int abat = 0;
int analog = 0;
2017-01-07 21:31:31 +01:00
int red = 0;
int green = 0;
float vbat = 0;
2017-01-07 21:31:31 +01:00
bool warned = 0;
void blink(int n=3, int t=500) {
for(int i=0;i<n;i++) {
digitalWrite(RELAY_PIN, LOW);
delay(t);
digitalWrite(RELAY_PIN, HIGH);
delay(t);
}
}
void setup() {
analogReference(DEFAULT);
Serial.begin(9600);
2017-01-07 21:31:31 +01:00
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_R_PIN, OUTPUT);
pinMode(LED_V_PIN, OUTPUT);
pinMode(LED_B_PIN, OUTPUT);
analogWrite(LED_R_PIN, 0);
analogWrite(LED_V_PIN, 0);
analogWrite(LED_B_PIN, 0);
2017-01-07 21:31:31 +01:00
//pinMode(BAT_PIN, INPUT);
Serial.print("Démarrage...");
2017-01-07 21:31:31 +01:00
for (int i=0; i<4;i++) {
analogWrite(LED_B_PIN, 255);
2017-01-07 21:31:31 +01:00
delay(INIT_BLINK);
analogWrite(LED_B_PIN, 0);
2017-01-07 21:31:31 +01:00
delay(INIT_BLINK);
}
digitalWrite(RELAY_PIN, HIGH);
Serial.println("OK!");
2017-01-07 21:31:31 +01:00
}
void loop() {
//Mesure de VBAT : moyenne de 10 mesures à 20ms d'intervale
abat = 0;
2017-01-07 21:31:31 +01:00
for(int i=0;i<10;i++) {
analog = analogRead(BAT_PIN);
abat += analog;
/* Serial.print(i);
Serial.print(" : ");
Serial.println(analog);
*/
delay(10);
2017-01-07 21:31:31 +01:00
}
abat /= 10;
vbat = (0.8+(5.7*abat*5.0/1024))*100;
// Serial.print("Brut: ");
// Serial.println(abat);
Serial.print("Tension:");
Serial.print(vbat/100);
Serial.println(" V");
2017-01-07 21:31:31 +01:00
// Calcul de la couleur et affichage sur la neopixel
red = map(constrain(vbat, VMIN, V_GREEN), VMIN, V_GREEN, 255, 0);
green = map(constrain(vbat, VMIN, V_GREEN), VMIN, V_GREEN, 0, 255);
analogWrite(LED_R_PIN, red);
analogWrite(LED_V_PIN, green);
analogWrite(LED_B_PIN, 0);
Serial.print("Rouge: ");
Serial.println(red);
Serial.print("Vert: ");
Serial.println(green);
2017-01-07 21:31:31 +01:00
if(vbat<V_ALERTE && !warned) {
Serial.println("Alerte! batterie faible !");
2017-01-07 21:31:31 +01:00
blink();
warned = 1;
}
// Quand on atteint la limite basse, on incrémente un compteur
if(vbat<VMIN) { low_bat_counter++; }
// Si le compteur atteint la limite, la faible tension est validée : procédure d'extinction
if(vbat<VMIN && low_bat_counter>10) {
Serial.println("Extinction : batterie trop faible");
2017-01-07 21:31:31 +01:00
blink(OFF_DELAY, 500);
digitalWrite(RELAY_PIN, LOW);
while(1) {
for(int i=0;i<50;i++) {
analogWrite(LED_R_PIN, i);
delay(15);
}
for(int i=50;i>0;i--) {
analogWrite(LED_R_PIN, i);
delay(15);
}
}
2017-01-07 21:31:31 +01:00
}
delay(2000);
2017-01-07 21:31:31 +01:00
}