Hi everyone,
I'm building a variometer for paragliding using a 1.3 OLED display and a BMP280 sensor. I managed to find some code on the Internet, I changed things here and there and everything works fine. But... I'm trying to add a function who let me set the altitude, according to the weather of the day. I managed to program it, but as I set the altitude, the buzzer keeps beeping, as it thinks I'm going up or down. How can I mute it when I'm in "altitude setting" mode?
I enter the setting mode by pressing button 2 and 3 at the same time, and exit it by pressing button 1. You'll find what I tried to program under "Réglage d'altitude". Pardon my french
Sorry if the code's a mess, I'm a newbie here.
Currently running on an Arduino Mega 2560
#include <Wire.h> // Bibliothèque pour i2c
#include <Adafruit_BMP280.h> // Bibliothèque pour capteur de pression BMP280
#include "U8glib.h" // Bibliothèque pour écran monochrome
#include <EEPROM.h> // Bibliothèque pour écriture sur la mémoire
#define tone_out1 8
#define tone_out2 9
#include <VoltageReference.h> // Bibliothèque pour contrôle de la batterie
VoltageReference vRef;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
Adafruit_BMP280 sensor_bmp;
short speaker_pin1 = 8; // arduino speaker output -
short speaker_pin2 = 9; // arduino speaker output +
float vario_down = -1.; // valeur du bip pour le taux de descente
float vario_emergence = -5.; // alarme le taux de descente
float vario_up; // variable taux de montée
float alt[51];
float tim[51];
float beep;
float Beep_period;
float mux;
float Alt = 0;
float vario = 0;
float Altitude = 0;
float AltitudeCorr = 0;
float Temperature = 0;
unsigned long bounseInput4P = 0UL;
unsigned long time;
unsigned char samples = 10;
unsigned char maxsamples = 50;
unsigned char countPressVal = 0;
unsigned char tela = 0;
unsigned char menu = 2;
unsigned char memo = 1;
//Bouton
unsigned char buttonState;
unsigned long duration;
unsigned long resettime;
unsigned char estadobotao = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 20; //debounce bouton
int buttonPlus = 6;
int buttonMinus = 5;
int Etat_buttonPlus = 0;
int Etat_buttonMinus = 0;
bool ReglageAlt = false;
//Horloge
unsigned char relogio = 0;
unsigned char segundo = 0, minuto = 0, hora = 0;
unsigned long UtlTime;
unsigned long lastMillis;
//Variables qui définisent le moment du décollage
unsigned long temp_alt;
unsigned char temp_verif_alt = 0;
unsigned char ult_altitude;
//Variables pour enregistrement sur la mémoire
int num_memo = 0;
float Mvarioup = 00.0;
float Mvariodown = 00.0;
float MaltitudeMax = 0000.0;
float MaltitudeDec = 0000.0;
float Mganho = 0000.0;
float Mpouso = 0000.0;
int endfly;
int end_memo = 1;
unsigned long gravatempo;
int ler = 1;
//Variables pour les mémoires
float Rvarioup = 00.0;
float Rvariodown = 00.0;
float RaltitudeMax = 0000.0;
float Rganho = 0000.0;
unsigned char Rhora;
unsigned char Rminuto;
int Rend_memo = 1; //Lire la mémoire
int Rend_memo_display = 1;
unsigned char Nmeno_display = 1;
int decolagem = 0;
boolean thermalling = false;
void play_welcome_beep() // Bip de démarage
{
for (int aa = 100; aa <= 800; aa = aa + 100) {
tone(tone_out1, aa, 200);
tone(tone_out2, aa + 3, 200);
delay(50);
}
}
//oled image
const uint8_t rook_bitmap[] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc8, 0xc2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x4e, 0x1e, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xbe, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x3f, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf4, 0x1f, 0xfe, 0x4b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7, 0x1f, 0xfe, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0x9f, 0xfe, 0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xf5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xe7, 0xff, 0xff, 0xf9, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xfe, 0x1f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x7c, 0x0f, 0xbe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x78, 0x07, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x72, 0x13, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x06, 0x1c, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9e, 0x1e, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xff, 0x5f, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xfe, 0x5f, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xfe, 0x5f, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xfe, 0xcf, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0x83, 0xf0, 0x27, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x03, 0xf0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xe4, 0xc1, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xc0, 0x80, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xe6, 0x19, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
// Numéros d'écrans:
// #0 : image démarrage
// #1 : texte démarrage (inactif)
// #2 : écran principal
// #3 : vols enregistrés
// #4 : effacement mémoire (maintenir bouton 3 secondes)
// #5 : confirmation effacement mémoire (maintenir bouton 10 secondes)
void draw(void) {
u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
u8g.setFont(u8g_font_8x13Br); //para o alfabeto completo com caracteres tirar o r do font_8x13Br
if (tela == 0) {
u8g.drawBitmapP(0, 0, 16, 64, rook_bitmap);
} else if (tela == 1) {
u8g.setColorIndex(0);
u8g.setColorIndex(1);
// u8g.drawStr( 25, 30, "Thiagogo"); //acentos http://www.ascii-code.com/
u8g.drawStr(15, 30, "Varduino v1.1");
}
if (tela == 2) {
u8g.setColorIndex(0);
u8g.setColorIndex(1);
unsigned char posVar = 0;
unsigned char posAlt = 0;
//Température
u8g.setPrintPos(2, 14);
u8g.print(Temperature, 1);
u8g.drawStr(37, 14, "C"); //acentos http://www.ascii-code.com/
//Encadrement vario + unités
u8g.drawFrame(50, 0, 78, 35); //Encadrement vario
u8g.drawStr(103, 30, "M\057s"); //m/s
u8g.drawStr(120, 64, "M"); //M altimètre
//Sensibilité et horloge
if (relogio == 1) {
u8g.drawStr(2, 30, "v");
u8g.setPrintPos(12, 30);
u8g.print(mux);
} else {
String timex = "";
if (hora < 10) {
timex += "0";
}
timex += hora;
if (segundo % 2 == 0) {
timex += ":";
} else {
timex += " ";
}
if (minuto < 10) {
timex += "0";
}
timex += minuto;
u8g.setPrintPos(2, 34);
u8g.print(timex);
}
//Altimètre
if (Altitude >= 1000) {
posAlt = 26;
} else {
posAlt = 46;
}
u8g.setFont(u8g_font_fub20n); //se for so numeros usar o n no fim u8g.setFont(u8g_font_fub20n);
u8g.setPrintPos(posAlt, 64);
u8g.print(Altitude, 1);
//Vario
if (vario < 0) {
posVar = 52;
if (vario < -0.1) {
u8g.drawTriangle(113, 15, 122, 7, 105, 7);
}
} else {
posVar = 61;
if (vario > 0.1) {
u8g.drawTriangle(114, 6, 124, 15, 104, 15);
}
}
u8g.setPrintPos(posVar, 27);
u8g.print(vario, 1);
//Batterie
int vcc = vRef.readVcc();
u8g.drawFrame(4, 42, 4, 2);
u8g.drawFrame(2, 44, 8, 18);
if (vcc > 3600) {
u8g.drawStr(1, 60, ".");
}
if (vcc > 3700) {
u8g.drawStr(1, 55, ".");
}
if (vcc > 3800) {
u8g.drawStr(1, 50, ".");
}
}
//-------------------menu-------------
// "tela" = écran
if (tela == 3) {
u8g.setColorIndex(0);
u8g.setColorIndex(1);
//Récupérer les données de l'EEPROM
if (Rend_memo_display == 1) { //Indique quelle mémoire lire selon l'écran
Rend_memo = 2;
}
if (Rend_memo_display == 2) {
Rend_memo = 22;
}
if (Rend_memo_display > 2) {
Rend_memo = (Rend_memo_display * 20) + 2;
}
if (ler == 0) {
EEPROM.get(Rend_memo, Rhora);
delay(10);
Rend_memo = Rend_memo + 1;
EEPROM.get(Rend_memo, Rminuto);
delay(10);
Rend_memo = Rend_memo + 2;
EEPROM.get(Rend_memo, Rvarioup);
delay(10);
Rend_memo = Rend_memo + 4;
EEPROM.get(Rend_memo, Rvariodown);
delay(10);
Rend_memo = Rend_memo + 4;
EEPROM.get(Rend_memo, RaltitudeMax);
delay(10);
Rend_memo = Rend_memo + 4;
EEPROM.get(Rend_memo, Rganho);
delay(10);
ler = 1;
}
u8g.drawRFrame(2, 0, 128, 16, 4);
u8g.setPrintPos(6, 13);
u8g.print(Nmeno_display); // Numéro de vol
u8g.drawStr(18, 13, "Vol");
//Affiche la mémoire de vol
String Rtimex = ""; //Gérer le temps de vol
if (Rhora < 10) {
Rtimex += "0";
}
Rtimex += Rhora;
Rtimex += ":";
if (Rminuto < 10) {
Rtimex += "0";
}
Rtimex += Rminuto;
u8g.drawStr(45, 13, "Dur.");
u8g.setPrintPos(80, 13);
u8g.print(Rtimex); //Mémoire EEPROM
//u8g.drawStr(22, 31, ":");
u8g.drawTriangle(12, 22, 22, 31, 2, 31);
u8g.setPrintPos(28, 31);
u8g.print(Rvarioup, 1); //Mémoire EEPROM
//u8g.drawStr(82, 31, ":");
u8g.drawTriangle(72, 30, 81, 22, 64, 22);
u8g.setPrintPos(84, 31);
u8g.print(Rvariodown, 1); //Mémoire EEPROM
u8g.drawStr(2, 47, "Alt.Max:");
u8g.setPrintPos(70, 47);
u8g.print(RaltitudeMax, 1); //Mémoire EEPROM
u8g.drawStr(2, 64, "Alt +"); //Alt max moins hauteur décollage
u8g.drawStr(58, 64, ":");
u8g.setPrintPos(70, 64);
u8g.print(Rganho, 1); //Mémoire EEPROM
}
if (tela == 4) {
u8g.setColorIndex(0);
u8g.setColorIndex(1);
u8g.drawStr(25, 30, "Effacement");
u8g.drawStr(37, 50, "Memoire");
delay(500);
menu = 5;
}
if (tela == 5) {
u8g.setColorIndex(0);
u8g.setColorIndex(1);
u8g.drawStr(25, 35, "OK");
delay(500);
menu = 2;
}
}
void setup() {
Wire.begin(); // Initialisation de l'écran
pinMode(4, INPUT); // Bouton
pinMode(buttonMinus, INPUT); // Bouton "-"
pinMode(buttonPlus, INPUT); // bouton "+"
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
pinMode(tone_out1, OUTPUT); // Sortie PIN
pinMode(tone_out2, OUTPUT); // Sortie PIN
sensor_bmp.begin(); // Initialisation du capteur de pression
vRef.begin(); // Commencer la lécture du niveau de batterie
// Lire la mémoire et écrire
num_memo = EEPROM.read(1); // Dernier enregistrement avant le dernier arrêt
delay(10);
num_memo++;
if (num_memo > 20) { num_memo = 1; }
if (num_memo < 1) { num_memo = 1; }
Rend_memo_display = EEPROM.read(1);
play_welcome_beep();
}
void loop(void) {
Serial.begin(9600);
//oled display
time = millis() / 300;
u8g.firstPage();
// Periode d'étalonnage
if (time <= 15) {
tela = 0;
}
//else if(time >10 && time <= 25){
// tela = 1;
// }
else {
tela = menu;
//--------------Enregistrement des données-----------
if (vario > Mvarioup) { Mvarioup = vario; } //grava na variavel a maior acendente
if (vario < Mvariodown) { Mvariodown = vario; } //grava na variavel a maior descendente
if (Altitude > MaltitudeMax) {
MaltitudeMax = Altitude;
}
// Ecris dans la variable l'altitude et le gain les plus élevés par rapport au décollage
MaltitudeDec = Altitude;
if (millis() - temp_alt < 0) {
temp_alt = millis();
} else {
temp_verif_alt = int((millis() - temp_alt) / 1000); // Compteur de secondes
}
if (temp_verif_alt > 20 && decolagem == 0) { // Vérifier toutes les 20 secondes
temp_verif_alt = 0;
MaltitudeDec = Altitude;
if ((Altitude - ult_altitude < -5.0 || Altitude - ult_altitude > 5.0) && menu == 2) {
decolagem = 1;
}
}
ult_altitude = MaltitudeDec;
Mganho = MaltitudeMax - MaltitudeDec;
//si au bout de 5 secondes la variation de hauteur n'est pas supérieure à 2 mètres, enregistrer le vol en mémoire
if ((Mpouso - Altitude < 2 || Mpouso - Altitude > -2) && menu == 2 && endfly == 0 && decolagem == 1) {
gravatempo = millis();
endfly = 1;
}
Mpouso = Altitude;
if (Mpouso - Altitude < 2 && Mpouso - Altitude > -2 && menu == 2 && endfly == 1) {
if (millis() - gravatempo > 5000) {
//Enregistrer toute l'EEPROM
EEPROM.write(1, num_memo); // Enregistre le numéro de mémoire du dernier enregistrement de vol, pour écraser le plus ancien.
delay(10);
if (num_memo == 1) {
end_memo = 2;
}
if (num_memo == 2) {
end_memo = 22;
}
if (num_memo > 2) {
end_memo = (num_memo * 20) + 2;
}
EEPROM.put(end_memo, hora);
delay(10);
end_memo = end_memo + 1;
EEPROM.put(end_memo, minuto);
delay(10);
end_memo = end_memo + 2;
EEPROM.put(end_memo, Mvarioup);
delay(10);
end_memo = end_memo + 4;
EEPROM.put(end_memo, Mvariodown);
delay(10);
end_memo = end_memo + 4;
EEPROM.put(end_memo, MaltitudeMax);
delay(10);
end_memo = end_memo + 4;
EEPROM.put(end_memo, Mganho);
delay(10);
endfly = 0;
/*eeprom
* 1 mémoire principale de comptage de vols
* 2 heures
* 3 minutes
* 4 vides
* 5-8 maximum croissant
* 9-12 décroissant max
* 13-16 altitude max
* 17-20 - gain d'altitude
*/
}
}
}
////////////////////////---------------------------------- Réglage d'altitude -----------------------------------
Etat_buttonMinus = digitalRead(buttonMinus);
Etat_buttonPlus = digitalRead(buttonPlus);
if ((Etat_buttonPlus == LOW) && (Etat_buttonMinus == LOW)) {
ReglageAlt = true;
delay(1000);
}
if (digitalRead(4) == LOW && ReglageAlt == true) {
ReglageAlt = false;
}
if (ReglageAlt == true) {
Serial.println("Mode Réglage");
Serial.println(AltitudeCorr);
if (digitalRead(buttonPlus) == LOW) {
AltitudeCorr = AltitudeCorr + 0.5;
delay(100);
}
if (digitalRead(buttonMinus) == LOW) {
AltitudeCorr = AltitudeCorr - 0.5;
delay(100);
}
}
int currentState = (digitalRead(4));
if (currentState != buttonState) {
duration = millis();
resettime = millis();
}
if (currentState == LOW) {
if ((millis() - duration > 50) && (millis() - duration < 2000) && menu == 2 && estadobotao == 0) {
countPressVal++;
tone(tone_out1, 1800, 40);
delay(80);
if (countPressVal > 2) { countPressVal = 0; }
//Afficher l'horloge et le vario
lastMillis = millis();
relogio = 1;
estadobotao = 1;
}
if (millis() - duration > 3000 && menu == 2 && estadobotao == 1) { // Si bouton pressé plus de 3 sec et sur l'écran N2
ler = 0;
menu = 3; // Afficher l'écran N3
duration = millis();
}
if (millis() - duration > 3000 && menu == 3 && estadobotao == 1) { //Si bouton pressé plus de 3 sec et sur l'écran N3
menu = 2; // Afficher l'écran N2
Nmeno_display = 1;
Rend_memo_display = EEPROM.read(1);
duration = millis();
}
//Effacement mémoire
if (millis() - resettime > 9000 && (menu == 2 || menu == 3) && estadobotao == 1) { // Si bouton pressé plus de 9 sec et sur l'écran N2 ou N3
menu = 4; // Afficher l'écran N4
for (int i = 0; i < 512; i++) // Ecrire 0 sur les 512 octets de l'EEPROM
EEPROM.write(i, 0);
Nmeno_display = 1;
Rend_memo_display = EEPROM.read(1);
resettime = millis();
}
if ((millis() - duration > 5) && (millis() - duration < 2000) && menu == 3 && estadobotao == 0) { // Si bouton pressé plus de 5 msec et moins de 2 sec et sur l'écran N3
ler = 0;
Nmeno_display++; // Passer au numéro de vol suivant
Rend_memo_display--; //extrait la mémoire de vol du plus récent au plus ancien
if (Rend_memo_display < 1) { Rend_memo_display = 20; }
if (Nmeno_display > 20) { Nmeno_display = 1; }
estadobotao = 1;
}
}
if (currentState == HIGH) {
estadobotao = 0;
}
buttonState = currentState;
// == MENU SENSIBILITE DE LA MONTEE ==
if ((countPressVal) == 0) {
mux = 0.3;
} // 1 signal
if ((countPressVal) == 1) {
mux = 0.5;
} // 2 signal
if ((countPressVal) == 2) {
mux = 0.8;
} // 3 signal
vario_up = mux;
//Afficher l'horloge et le vario
if (lastMillis > 0 && (millis() - lastMillis > 3000)) {
relogio = 0;
lastMillis = 0;
}
float tempo = millis();
float N1 = 0;
float N2 = 0;
float N3 = 0;
float D1 = 0;
float D2 = 0;
Altitude = (sensor_bmp.readAltitude(1013.25 + AltitudeCorr));
Temperature = (sensor_bmp.readTemperature());
for (int cc = 1; cc <= maxsamples; cc++) { // moyenne
alt[(cc - 1)] = alt[cc];
tim[(cc - 1)] = tim[cc];
};
alt[maxsamples] = Altitude;
tim[maxsamples] = tempo;
float stime = tim[maxsamples - samples];
for (int cc = (maxsamples - samples); cc < maxsamples; cc++) {
N1 += (tim[cc] - stime) * alt[cc];
N2 += (tim[cc] - stime);
N3 += (alt[cc]);
D1 += (tim[cc] - stime) * (tim[cc] - stime);
D2 += (tim[cc] - stime);
};
vario = 1000 * ((samples * N1) - N2 * N3) / (samples * D1 - D2 * D2); // Calcul du son
if ((tempo - beep) > Beep_period) {
beep = tempo;
if (vario > vario_up && vario < 15) {
Beep_period = 350 - (vario * 10);
tone(tone_out1, (1000 + (100 * vario)), 300 - (vario * 5)); // Son durant la montée
tone(tone_out2, (1003 + (100 * vario)), 300 - (vario * 5));
thermalling = true;
} else if ((vario < 0) && (thermalling == true)) {
thermalling = false;
// tone_out2.play(200, 800); Predpotok // Son (facultatif)
} else if (vario < vario_down && vario > vario_emergence) { // Son durant la descente
Beep_period = 200;
tone(tone_out1, (300 - (vario)), 340);
tone(tone_out2, (303 - (vario)), 340);
thermalling = false;
} else if (vario < vario_emergence) { // Son d'urgence
Beep_period = 200;
tone(tone_out1, 2000, 250);
tone(tone_out2, 2003, 250);
delay(200);
tone(tone_out1, 1000, 250);
tone(tone_out2, 1003, 250);
thermalling = false;
}
}
//Horloge
if (decolagem == 1) { //Démarre le chrono dès qu'il détècte le décollage
if (millis() - UtlTime < 0) {
UtlTime = millis();
} else {
segundo = int((millis() - UtlTime) / 1000);
}
if (segundo > 59) {
segundo = 0;
minuto++;
UtlTime = millis();
if (minuto > 59) {
hora++;
minuto = 0;
}
}
} else {
segundo = 00;
minuto = 00;
hora = 00;
}
//-----------Afficher les écrans-----------
do {
draw();
} while (u8g.nextPage());
//fim oled display
}