Moyene glissante pour compte-tour #2
This commit is contained in:
parent
870a31e834
commit
617e76eb36
124
stroboduino.ino
124
stroboduino.ino
|
@ -3,36 +3,37 @@
|
|||
//------------------------------------------------------------------------------
|
||||
char Ver[] = "cg230417";
|
||||
|
||||
#include <TimerOne.h>
|
||||
#include <Encoder.h>
|
||||
#include <EEPROM.h>
|
||||
#include <LiquidCrystal.h>
|
||||
#include <TimerOne.h> // Timer1 --> timer entre étincelle et Flash
|
||||
#include <Encoder.h> // Bibiliothèque pour gestion encoder avec anti-rebond
|
||||
#include <EEPROM.h> // Lect/Ecrit dans l'EEPROM interne à l'ATMEL pour svg des param.
|
||||
#include <LiquidCrystal.h> // LCD
|
||||
#include <RunningAverage.h> // Calcul de moy. glissante pour lisser le compte-tour
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// PARAMETRES
|
||||
// PARAMETRES (stockés dans l'EEPROM)
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
byte D_Flash = 10; //Durée du flash en µs, autour de 10µs
|
||||
const int deg_Initial = 10; //Affiché au demarrage
|
||||
byte Ncyl = 2;
|
||||
byte Refresh = 100;
|
||||
//bool att_tour = 0; // true si on attend un tour supplémentaire avant le Flash
|
||||
byte D_Flash = 10; // Durée du flash en µs, autour de 10µs
|
||||
byte Ncyl = 2; // Nombre de cylindres
|
||||
byte Refresh = 100; // Refresh du LCD en millisec.
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// CONSTANTES
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#define Etin 2 // Entrée bobine
|
||||
#define Inter 5 // Inter du codeur
|
||||
#define Flash 11 // Flash
|
||||
#define Codeur_A 3 // D3 pour entrée codeur A sous interruption
|
||||
#define Codeur_B 4 // D4 pour entrée codeur B
|
||||
#define Etin 2 // Entrée bobine
|
||||
#define Inter 5 // Inter du codeur
|
||||
#define Flash 11 // Flash
|
||||
#define Codeur_A 3 // D3 pour entrée codeur A sous interruption
|
||||
#define Codeur_B 4 // D4 pour entrée codeur B
|
||||
const byte lissrpm = 3; // Nb de valeurs pour moy. glissante compte-tours
|
||||
const int deg_Initial = 10; // Affiché au demarrage
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
// VARIABLES
|
||||
//-------------------------------------------------------------------------------
|
||||
|
||||
bool debug = 0; // Variable pour debug
|
||||
bool debug = 0; // true si debug
|
||||
|
||||
int signed long T = 0; // Periode en cours(signée pour le calcul de D_deg)
|
||||
int unsigned long prec_H = 0; // Heure du front precedent en µs
|
||||
|
@ -42,11 +43,12 @@ int unsigned long N = 0; // N en tours/mn
|
|||
int unsigned long Nc = 0; // Pour le calcul de N
|
||||
unsigned long pm = 0; // Previous millis pour Refresh LCD
|
||||
byte modconfig = 0; // Flag pour savoir si on est en mode config
|
||||
int degT = 720 / Ncyl; // Degrés entre 2 étincelles
|
||||
int degT = 720 / Ncyl; // Degrés entre 2 étincelles
|
||||
|
||||
LiquidCrystal lcd( 9, 17, 15, 16, 14, 10);
|
||||
//lcd(RS, E, D4, D5, D6, D7)
|
||||
Encoder Enco(Codeur_A, Codeur_B);
|
||||
RunningAverage RArpm(lissrpm);
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// ISR
|
||||
|
@ -68,20 +70,23 @@ void isrFlash(){
|
|||
//Délai en µs pour les degrés demandés, periode suivante
|
||||
D_deg = float(T * deg_dem) / float(degT);
|
||||
|
||||
// Si retard demandé, on ajout un tour
|
||||
// Si retard demandé, on ajoute un tour
|
||||
if (deg_dem < 0){D_deg += float(T * 360) / float(degT);}
|
||||
|
||||
// On ajoute la valeur de T au calcul de moy. glissante
|
||||
RArpm.addValue(T);
|
||||
|
||||
// DEBUG
|
||||
if (debug) {Serial.print("T : "); Serial.print(T); Serial.print(" D_deg : "); Serial.println(D_deg);}
|
||||
if (debug) {Serial.print("degT : "); Serial.print(degT); Serial.print(" deg_dem : "); Serial.println(deg_dem);}
|
||||
//if (debug) {Serial.print("T : "); Serial.print(T); Serial.print(" D_deg : "); Serial.println(D_deg);}
|
||||
//if (debug) {Serial.print("degT : "); Serial.print(degT); Serial.print(" deg_dem : "); Serial.println(deg_dem);}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// FONCTIONS
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
void Affic_N_et_deg_dem(){
|
||||
N = Nc / T; // Calculer N en t/mn
|
||||
void Affic_N_et_deg_dem(){ // Affiche compte-tour et dégrés demandés
|
||||
N = Nc / RArpm.getAverage(); // Calculer N en t/mn
|
||||
lcd.setCursor(0, 0); lcd.print(" ");
|
||||
lcd.setCursor(0, 0); lcd.print(N); // Régime tr/min
|
||||
lcd.setCursor(0, 1); lcd.print(" ");
|
||||
|
@ -90,27 +95,23 @@ void Affic_N_et_deg_dem(){
|
|||
|
||||
void Affich_fond_ecran(){ // Affiche le fond d'écran au début ou après la config.
|
||||
lcd.clear();
|
||||
lcd.setCursor(5, 0);
|
||||
lcd.print("tpm");
|
||||
lcd.setCursor(5, 1);
|
||||
lcd.print(Ncyl);
|
||||
lcd.print("c");
|
||||
lcd.setCursor(5, 0); lcd.print("tpm");
|
||||
lcd.setCursor(5, 1); lcd.print(Ncyl); lcd.print("c");
|
||||
}
|
||||
|
||||
void Regl(byte &var, String titre){
|
||||
lcd.clear();
|
||||
lcd.print(titre);
|
||||
Enco.write(var*4);
|
||||
void Regl(byte &var, String titre){ // Macro de réglage pour param type byte
|
||||
lcd.clear(); lcd.print(titre);
|
||||
Enco.write(var*4); // Init l'encodeur avec la valeur actuelle
|
||||
while (digitalRead(Inter) == 1){ // tant qu'on appuie pas on régle
|
||||
var = Enco.read()/4;
|
||||
lcd.setCursor(3, 1);
|
||||
lcd.print(var);
|
||||
lcd.print(var); // Affiche la valeur
|
||||
lcd.print(" ");
|
||||
}
|
||||
while (digitalRead(Inter) == 0); // Si on a appuyé, on attend le relachement
|
||||
}
|
||||
|
||||
void Regl(bool &var, String titre){
|
||||
void Regl(bool &var, String titre){ // Macro de réglage pour param type bool
|
||||
lcd.clear();
|
||||
lcd.print(titre);
|
||||
lcd.setCursor(3, 1);
|
||||
|
@ -136,13 +137,17 @@ void ConfiG() {
|
|||
lcd.setCursor(7, 1);
|
||||
lcd.write(126);
|
||||
while (digitalRead(Inter) == 0); //toujours appuyé ? on attend
|
||||
Regl(Ncyl, "Nb cyl.");
|
||||
|
||||
// Réglages avec le codeur
|
||||
Regl(Ncyl, "Nb cyl.");
|
||||
Regl(D_Flash, "D Flash");
|
||||
Regl(Refresh, "Refresh");
|
||||
//Regl(att_tour, "FlaAp1tr");
|
||||
|
||||
// Sauvegarde des paramètres dans l'EEPROM
|
||||
EEPROM.write(0, Ncyl);
|
||||
EEPROM.write(1, D_Flash);
|
||||
EEPROM.write(2, Refresh);
|
||||
|
||||
IniT();
|
||||
}
|
||||
|
||||
|
@ -150,44 +155,51 @@ void ConfiG() {
|
|||
// SETUP
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void IniT() {
|
||||
Ncyl = EEPROM.read(0); // on charge les paramètres stockés en mémoire
|
||||
void IniT() {
|
||||
// On charge les paramètres stockés en mémoire
|
||||
Ncyl = EEPROM.read(0);
|
||||
D_Flash = EEPROM.read(1);
|
||||
Refresh = EEPROM.read(2);
|
||||
|
||||
|
||||
// Interruption détection d'étincelle
|
||||
attachInterrupt(digitalPinToInterrupt(Etin), isrEtincelle, RISING);
|
||||
Timer1.attachInterrupt(isrFlash); // Timer entre Etincelle et Flash
|
||||
|
||||
// Timer entre Etincelle et Flash
|
||||
Timer1.attachInterrupt(isrFlash);
|
||||
Timer1.stop();
|
||||
modconfig=0;
|
||||
|
||||
//Affichage titre
|
||||
// Affichage titre
|
||||
lcd.clear();
|
||||
lcd.print("Strobo"); lcd.setCursor(3, 1); lcd.print("duino");
|
||||
delay(1200);
|
||||
if (digitalRead(Inter) == 0) {modconfig++;} //si bouton appuyé
|
||||
|
||||
// Bouton appuyé
|
||||
if (digitalRead(Inter) == 0) {modconfig++;}
|
||||
|
||||
//Test Flash
|
||||
// Test Flash
|
||||
digitalWrite (Flash, 1); delayMicroseconds(D_Flash); digitalWrite (Flash, 0);
|
||||
|
||||
//Affichage version
|
||||
// Affichage version
|
||||
lcd.clear();
|
||||
lcd.print("Version"); lcd.setCursor(0, 1); lcd.print(Ver); // N° de version
|
||||
|
||||
lcd.print("Version");
|
||||
lcd.setCursor(0, 1); lcd.print(Ver); // N° de version
|
||||
delay(1000);
|
||||
if (digitalRead(Inter) == 0) {modconfig++;} // si bouton toujours appuyé
|
||||
|
||||
if (digitalRead(Inter) == 0) {modconfig++;} // si bouton toujours appuyé
|
||||
if (modconfig >= 2){ // on entre dans le mode config
|
||||
modconfig=0;
|
||||
ConfiG();
|
||||
}
|
||||
|
||||
Affich_fond_ecran();
|
||||
|
||||
Enco.write(deg_Initial*4);
|
||||
degT = 720 / Ncyl;
|
||||
Nc = 120000000 / Ncyl;
|
||||
T = 150000;
|
||||
degT = 720 / Ncyl;
|
||||
Nc = 120000000 / Ncyl;
|
||||
T = 150000;
|
||||
RArpm.addValue(T);
|
||||
D_deg = float(T * deg_dem) / float(degT); // Pour le premier cycle
|
||||
|
||||
|
||||
Affich_fond_ecran();
|
||||
Affic_N_et_deg_dem();
|
||||
}
|
||||
|
||||
|
@ -198,7 +210,7 @@ void setup() {
|
|||
pinMode(Codeur_A, INPUT_PULLUP);
|
||||
pinMode(Codeur_B, INPUT_PULLUP);
|
||||
pinMode(Inter, INPUT_PULLUP); // Le codeur met son inter à la masse quand poussé
|
||||
lcd.begin(8, 2);
|
||||
lcd.begin(8, 2); // LCD 8 colonnes, 2 lignes
|
||||
IniT();
|
||||
}
|
||||
|
||||
|
@ -206,13 +218,13 @@ void setup() {
|
|||
// LOOP
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void loop(){
|
||||
if (millis() - pm > Refresh) { // Mise à jour affichage LCD
|
||||
void loop(){
|
||||
if ((millis() - pm) > Refresh) { // Mise à jour affichage LCD
|
||||
Affic_N_et_deg_dem(); // Affichage tr/min et deg_demandés
|
||||
pm = millis();
|
||||
deg_dem = Enco.read()/4; // On lit les degrés demandés via l'encodeur
|
||||
}
|
||||
|
||||
|
||||
if (digitalRead(Inter) == 0) { // Si l'inter du codeur est poussé, geler l'affichage
|
||||
delay(1000); while (digitalRead(Inter) == 0); // Encore poussé, attendre
|
||||
delay(100); // Continuer
|
||||
|
|
Loading…
Reference in New Issue