Buonasera,
come da titolo ho un problema a gestire funzioni in parallelo (ovviamente so che non esiste la contemporaneità, quindi passiamo oltre), ovvero :
il codice non è altro che un controllo di 2 anemometri tachimetrici, dove viene visualizzata la velocità sul display oled e poi visualizzata in tappe su accessioni di led in sequenza.
Tralasciando la parte di codice riguardante il display, il problema mi arriva quando dopo essa, dove ho il primo if() dove mette in confronto le letture di ingresso a determinati valori (ES. Ingresso Analagico > 69). Adesso nel codice i led si accendono e basta però il mio quesito è:
come faccio a fare una funzione dove il led invece di essere accesso e basta, mi lampeggia per 5 secondi anche se nella prima struttura if diventa falsa.
Perchè essendo un loop continuo e non posso fermarlo se no si ferma anche il display, quando fa partire la struttura if, anche se fosse sempre vera, anche con la struttura millis() la funzione led ricomincerebbe di continuo senza fargli fare il lampeggio (continuo se è sempre vero, se è falsa farlo continuare per 5 sec.
ho provato varie logiche ma le conclusioni per adesso sono :
- o il led rimane sempre acceso, perchè la struttura loopa di continuo e riparte da capo.
- o il display si blocca e riparte.
come se non riuscissi a baypassare la struttura loop/if e do-while.
se potreste darmi qualche idea a riguardo.
ho pensato anche di metterci un secondo chip, tipo un tiny85 per gestire la funzione led separatamente, da input, forniti da output dell'arduino con chip ATmega328P che gestisce la main().
oppure mettere un registro a scorrimento con determinato clock per dare il tempo alla funzione di controllo per il lampeggio.
il codice per adesso è il seguente:
#include <MapFloat.h>
#include "U8glib.h"
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI con chip SH1306
// U8GLIB_SSD1306_128X64 u8g(13, 11, 8, 9, 10); // SPI connection
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_FAST); // I2C + SH1106 (chip usato)
// for SPI connection, use this wiring:
// GND > GND
// VCC > 5V
// SCL > 13
// SDA > 11
// RES > 10
// DC > 9
// CS > 8
/////////// definizione immagine ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const unsigned char epd_bitmap_anemoetri_photopea_2_anemoemtri_con_sengatori [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe0, 0x00, 0xee, 0x00, 0x1b, 0x80, 0x0e, 0xe0, 0x03, 0xb8, 0x00, 0xbb, 0x80, 0x2e, 0xe0, 0x26,
0xa0, 0x00, 0x2a, 0x00, 0x2a, 0x80, 0x08, 0xa0, 0x02, 0xa8, 0x00, 0xaa, 0x80, 0x22, 0xa0, 0x2a,
0xa0, 0x00, 0xea, 0x00, 0x2a, 0x80, 0x0e, 0xa0, 0x03, 0xa8, 0x00, 0xaa, 0x80, 0x2e, 0xa0, 0x2a,
0xa0, 0x00, 0x8a, 0x00, 0x3a, 0x80, 0x0a, 0xa0, 0x02, 0xa8, 0x00, 0xaa, 0x80, 0x28, 0xa0, 0x2e,
0xe0, 0x00, 0xee, 0x00, 0x0b, 0x80, 0x0e, 0xe7, 0x73, 0xb9, 0xdc, 0xbb, 0x80, 0x2e, 0xe0, 0x22,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x50, 0x01, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x10, 0x00, 0x04, 0x00, 0x01, 0x01, 0x50, 0x41, 0xd4, 0x10, 0x00, 0x04, 0x00, 0x02,
0x40, 0x00, 0x10, 0x00, 0x04, 0x00, 0x01, 0x01, 0x70, 0x40, 0x5c, 0x10, 0x00, 0x04, 0x00, 0x02,
0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00,
0x03, 0xe3, 0x7c, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0xfc, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x03, 0x27, 0x7c, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x62, 0xcc, 0x06, 0x00, 0x00, 0x00, 0x00,
0x03, 0x2f, 0x6c, 0xff, 0x80, 0x00, 0x00, 0x03, 0x00, 0x62, 0xcd, 0xff, 0x00, 0x00, 0x00, 0x00,
0x03, 0xeb, 0x6c, 0xab, 0xc0, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x79, 0x57, 0x80, 0x00, 0x00, 0x00,
0x03, 0x03, 0x0c, 0xff, 0x80, 0x00, 0x00, 0x03, 0x00, 0x60, 0xfd, 0xff, 0x00, 0x00, 0x00, 0x00,
0x03, 0x03, 0x1e, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x60, 0xcc, 0x06, 0x00, 0x00, 0x00, 0x00,
0x03, 0x03, 0x0c, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x60, 0xcc, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x03, 0x03, 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x60, 0xfc, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe0, 0x00, 0xee, 0x00, 0x1b, 0x80, 0x0e, 0xe0, 0x03, 0xb8, 0x00, 0xbb, 0x80, 0x2e, 0xe0, 0x26,
0xa0, 0x00, 0x2a, 0x00, 0x2a, 0x80, 0x08, 0xa0, 0x02, 0xa8, 0x00, 0xaa, 0x80, 0x22, 0xa0, 0x2a,
0xa0, 0x00, 0xea, 0x00, 0x2a, 0x80, 0x0e, 0xa0, 0x03, 0xa8, 0x00, 0xaa, 0x80, 0x2e, 0xa0, 0x2a,
0xa0, 0x00, 0x8a, 0x00, 0x3a, 0x80, 0x0a, 0xa0, 0x02, 0xa8, 0x00, 0xaa, 0x80, 0x28, 0xa0, 0x2e,
0xe0, 0x00, 0xee, 0x00, 0x0b, 0x80, 0x0e, 0xe7, 0x73, 0xb9, 0xdc, 0xbb, 0x80, 0x2e, 0xe0, 0x22,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x50, 0x01, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x10, 0x00, 0x04, 0x00, 0x01, 0x01, 0x50, 0x41, 0xd4, 0x10, 0x00, 0x04, 0x00, 0x02,
0x40, 0x00, 0x10, 0x00, 0x04, 0x00, 0x01, 0x01, 0x70, 0x40, 0x5c, 0x10, 0x00, 0x04, 0x00, 0x02,
0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x88, 0x88, 0x89,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x22, 0x23,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 1040)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
epd_bitmap_anemoetri_photopea_2_anemoemtri_con_sengatori
};
///////////////// definizioni variabili //////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ledpinW 13
#define ledpinG 12
#define ledpinY 11
#define ledpinR 10
float p = 0.00;
float w = 0.00;
float k = 0.00;
float mh = 0.00;
float i = 0.00;
float j = 0.00;
float l = 0.00;
float mk = 0.00;
int ami = 0;
int ame = 0;
//int time = 0;
//int accensione = 0;
//int spegnimento = 0;
char amii[5];
char amee[5];
//unsigned long tempo = 0;
//unsigned long tempoled = 0;
//unsigned long tempos;
void setup() {
Serial.begin(9600);
//u8g.setFont(u8g_font_tpssb); // no need to set the font, as we are not drawing any strings
u8g.setColorIndex(1); // set the color
u8g.setFont(u8g_font_6x12);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(ledpinW, OUTPUT);
pinMode(ledpinG, OUTPUT);
pinMode(ledpinY, OUTPUT);
pinMode(ledpinR, OUTPUT);
}
void loop() {
//anemometro 1
p = analogRead(A0); // read the potentiometer value, 0-1023 for 0-5V 1023 - x = 150 - 140
w = ((p * 5.0) / 1024);
k = mapFloat(w, 0, 5, 0, 150);
mh = k;
//anemometro 2
i = analogRead(A1); // read the potentiometer value, 0-1023 for 0-5V 1023 - x = 150 - 140
j = ((i * 5.0) / 1024);
l = mapFloat(j, 0, 5, 0, 150);
mk = l;
ami = k;
ame = l;
sprintf (amii, "%d", ami);
sprintf (amee, "%d", ame);
u8g.firstPage();
do {
u8g.setColorIndex(1); // white color
u8g.drawBitmapP( 0, 0, 128/8, 64, epd_bitmap_anemoetri_photopea_2_anemoemtri_con_sengatori);
u8g.setColorIndex(1);
u8g.drawStr(38, 36, amii);
u8g.drawStr(101, 36, amee);
u8g.drawBox(2, 15, mapFloat(p, 0, 920.7, 0, 126), 7);
u8g.drawBox(2, 54, mapFloat(i, 0, 920.7, 0, 126), 7);
} while ( u8g.nextPage() );
if((ami > 59) || (ame > 59)){
digitalWrite(ledpinW, HIGH);
} else {
digitalWrite(ledpinW, LOW);
}
if((ami > 69) || (ame > 69)){
digitalWrite(ledpinG, HIGH);
} else {
digitalWrite(ledpinG, LOW);
}
if((ami > 85) || (ame > 85)){
digitalWrite(ledpinY, HIGH);
} else {
digitalWrite(ledpinY, LOW);
}
if((ami > 95) || (ame > 95)){
digitalWrite(ledpinR, HIGH);
} else {
digitalWrite(ledpinR, LOW);
}
}
/////////// Funzione accensione led //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//void led(){
// digitalWrite(ledpin, HIGH);
// Serial.println(accensione);
// Serial.print("1");
//if(accensione == 1000){
// digitalWrite(ledpin, LOW);
// Serial.println(accensione);
// Serial.print("2");
//}
//if(accensione == 2000){
// digitalWrite(ledpin, HIGH);
// Serial.println(accensione);
// Serial.print("3");
//}
//if(accensione == 3000){
// digitalWrite(ledpin, LOW);
//}
//}