Adaptation du code à Arduino Uno + LED RGV

A la place d'un ATtiny85 + neopixel
This commit is contained in:
arofarn 2021-07-28 21:47:01 +02:00
parent 167057089c
commit 0fa780b36e
1 changed files with 66 additions and 33 deletions

View File

@ -16,29 +16,30 @@ along with this program.
If not, see <http://www.gnu.org/licenses/> If not, see <http://www.gnu.org/licenses/>
*/ */
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__ #ifdef __AVR__
#include <avr/power.h> #include <avr/power.h>
#endif #endif
#define LED_PIN 0 #define LED_R_PIN 11
#define RELAY_PIN 1 #define LED_V_PIN 10
#define BAT_PIN 1 // Analog Input 1 = digital PIN 2 #define LED_B_PIN 9
#define VMAX 486 // = ~13.5V avec des résistances de 47k et 10k #define RELAY_PIN 2
#define V_GREEN 431 // = ~12V tension pour vert pur #define BAT_PIN A1 // Analog Input 0 = digital PIN 2
#define VMIN 395 // = ~11 V #define VMAX 1250
#define V_ALERTE 413 // = ~11.5V provoque clignotement 3 fois rapide #define V_GREEN 1170 // tension pour vert pur
#define V_OFF 388 // = ~10.8V provoque clignotement pendant 15s puis OFF #define VMIN 950
#define OFF_DELAY 15 // delais de clignotement (en secondes) avant extinction à l'atteinte de V_OFF #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
#define INIT_BLINK 100 #define INIT_BLINK 100
Adafruit_NeoPixel neopix = Adafruit_NeoPixel(1, LED_PIN, NEO_RGB + NEO_KHZ800);
byte timer = 100; byte timer = 100;
byte low_bat_counter = 0; byte low_bat_counter = 0;
int vbat = 0; int abat = 0;
int analog = 0;
int red = 0; int red = 0;
int green = 0; int green = 0;
float vbat = 0;
bool warned = 0; bool warned = 0;
void blink(int n=3, int t=500) { void blink(int n=3, int t=500) {
@ -51,48 +52,70 @@ void blink(int n=3, int t=500) {
} }
void setup() { void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__) analogReference(DEFAULT);
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif Serial.begin(9600);
// End of trinket special code
pinMode(RELAY_PIN, OUTPUT); 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);
//pinMode(BAT_PIN, INPUT); //pinMode(BAT_PIN, INPUT);
neopix.begin(); Serial.print("Démarrage...");
neopix.show(); // Initialize all pixels to 'off'
for (int i=0; i<4;i++) { for (int i=0; i<4;i++) {
neopix.setPixelColor(0, 0, 0, 255); analogWrite(LED_B_PIN, 255);
neopix.show();
delay(INIT_BLINK); delay(INIT_BLINK);
neopix.setPixelColor(0, 0, 0, 0); analogWrite(LED_B_PIN, 0);
neopix.show();
delay(INIT_BLINK); delay(INIT_BLINK);
} }
digitalWrite(RELAY_PIN, HIGH); digitalWrite(RELAY_PIN, HIGH);
Serial.println("OK!");
} }
void loop() { void loop() {
//Mesure de VBAT : moyenne de 10 mesures à 20ms d'intervale //Mesure de VBAT : moyenne de 10 mesures à 20ms d'intervale
vbat =0; abat = 0;
for(int i=0;i<10;i++) { for(int i=0;i<10;i++) {
vbat += analogRead(BAT_PIN); analog = analogRead(BAT_PIN);
delay(20); abat += analog;
/* Serial.print(i);
Serial.print(" : ");
Serial.println(analog);
*/
delay(10);
} }
vbat /= 10; 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");
// Calcul de la couleur et affichage sur la neopixel // Calcul de la couleur et affichage sur la neopixel
red = map(constrain(vbat, VMIN, V_GREEN), VMIN, V_GREEN, 255, 0); red = map(constrain(vbat, VMIN, V_GREEN), VMIN, V_GREEN, 255, 0);
green = map(constrain(vbat, VMIN, V_GREEN), VMIN, V_GREEN, 0, 255); green = map(constrain(vbat, VMIN, V_GREEN), VMIN, V_GREEN, 0, 255);
neopix.setPixelColor(0, red, green, 0); analogWrite(LED_R_PIN, red);
neopix.show(); analogWrite(LED_V_PIN, green);
analogWrite(LED_B_PIN, 0);
Serial.print("Rouge: ");
Serial.println(red);
Serial.print("Vert: ");
Serial.println(green);
if(vbat<V_ALERTE && !warned) { if(vbat<V_ALERTE && !warned) {
Serial.println("Alerte! batterie faible !");
blink(); blink();
warned = 1; warned = 1;
} }
@ -102,10 +125,20 @@ void loop() {
// Si le compteur atteint la limite, la faible tension est validée : procédure d'extinction // Si le compteur atteint la limite, la faible tension est validée : procédure d'extinction
if(vbat<VMIN && low_bat_counter>10) { if(vbat<VMIN && low_bat_counter>10) {
Serial.println("Extinction : batterie trop faible");
blink(OFF_DELAY, 500); blink(OFF_DELAY, 500);
digitalWrite(RELAY_PIN, LOW); digitalWrite(RELAY_PIN, LOW);
while(1); 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);
}
}
} }
delay(1000); delay(2000);
} }