Salve a tutti!! Sto implementando una stazioncina meteo artigianale e sebbene lo sketch che utilizzo funzioni abbastanza bene lo vorrei migliorare... l'idea è quella di utilizzare un interruttore per fare in modo di "cambiare schermata" sul display LCD. ora come ora visualizzo la Temperatura esterna e l'umidità in una, mentre nell'altra la pressione atmosferica.. con lo sketch che vi metto sotto (molto rudimentale in questa parte appunto) devo attendere 5 secondi affinchè arduino possa leggere il pin8 e possa cambiare schermata.. ecco la mia idea sarebbe quella di aggiornare i dati ogni 5-10 minuti e quando li vado a stampare sul LCD vorrei che il dato da leggere (ad esempio temperatura ed umidità nella stessa schermata) fosse sempre lo stesso a meno che non clicchi il pulsante e mi vada quindi a leggere il valore della pressione ad esempio. avevo pensato di aggiungere tipo un contatore o uno switch ma sinceramente, nonostante sia un ingegnere elettrico, in programmazione non sono propriamente un fenomeno =( eheh
lo sketch è questo! (premetto che è un po' incasinato in effetti, comunque la parte da mettere a posto ovviamente è quella in fondo). Ah! per informazione, sto usando un SHT21 ed un BMP085!! LCD è un 16x2... grazie mille in anticipo!
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SHT2x.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available())
;
return Wire.read();
}
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2)
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
#include <Wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 0; // Oversampling Setting
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5;
short temperature;
long pressure;
// Use these for altitude conversions
const float p0 = 101325; // Pressure at sea level (Pa)
float altitude;
void setup()
{ pinMode(8,INPUT);
Serial.begin(9600);
Wire.begin();
bmp085Calibration();
}
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
// Read the uncompensated temperature value
unsigned int bmp085ReadUT()
{
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp085ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp085ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on OSS
delay(2 + (3<<OSS));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 3);
// Wait for data to become available
while(Wire.available() < 3)
;
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
// Calculate temperature given ut.
// Value returned will be in units of 0.1 deg C
short bmp085GetTemperature(unsigned int ut)
{
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += ((x1 + x2 + 3791)>>4)+200;
return p;
}
float SHTtemp;
float SHTRH;
float temperature1;
float pressure1;
void stampaparametri()
{
Serial.print("Outdoor Temperature: ");
Serial.print(SHTtemp);
Serial.print(" C");
Serial.print(" Humidity: ");
Serial.print(SHTRH);
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(pressure1);
Serial.print(" hPa");
Serial.print(" Indoor Temperature: ");
Serial.print(temperature1);
Serial.println(" C");
}
int elena;
void loop()
{
SHTtemp=SHT2x.GetTemperature();
SHTRH=SHT2x.GetHumidity();
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
temperature1=temperature*0.1;
pressure1=pressure*0.01;
stampaparametri();
elena=digitalRead(8);
if ((elena==LOW)) {
lcd.begin(16,2);
lcd.print("Out Temp:");
lcd.setCursor(10,0);
lcd.print(SHTtemp);
lcd.setCursor(15,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("RH:");
lcd.setCursor(10,1);
lcd.print(SHTRH);
lcd.setCursor(15,1);
lcd.print("%");
delay (5000);
}
else if((elena==HIGH)){
lcd.begin(16,2);
lcd.print("Pressione:");
lcd.setCursor(0,1);
lcd.print(pressure1);
lcd.setCursor(13,1);
lcd.print("hPa");
delay(5000);
}
}
Col tastino modify in alto a destra puoi rieditare anche il tuo post precedente.
C'è anche un'altra funzione interessante dell'IDE nel menù strumenti: formattazione automatica. Permette di indentare lo sketch rendendo il codice più leggibile.
Ho fatto una cosa simile a quella che chiedi.
Qui trovi lo sketch di un mio progetto --> [Progetto] Palloncini con led - Generale - Arduino Forum
Se vedi lo switch case è comandato dalla variabile buttonPushCounter.
Quella variabile può essere modificata in due modi.
Il primo è premendo il pulsante, in sostanza
Tu devi implementare un controllo simile dove la tua variabile comanda uno switch con le varie visualizzazioni del display.
Nel secondo caso, a posto del random che credo non ti interessi, metterai sempre un incremento di tipo sequenziale.
p.s. devi imparare a usare la funzione millis().
Guarda l'esempio del blink whitout delay nell'IDE.
Ho provato a prendere un po' spunto dal tuo sketch e dai tuoi consigli e qualcosa sono riuscito a fare (counter con la funzione millis() per stampare a video i dati ogni 2 secondi ad esempio)! Pero' quello che ho fatto purtroppo non funziona proprio egregiamente . Spiego meglio cosa vorrei:
-se schiaccio una volta il bottone vorrei che la schermata rimanesse quella di umidità e temperatura e mi aggiornasse i dati (ad esempio ogni 2 secondi)
-se schiaccio una seconda volta vorrei vedere la pressione (aggiornata anch'essa con la frequenza precedente) e mi piacerebbe che la schermata rimanesse fino a quando rischiaccio il bottone, per passare di nuovo alla schermata con T ed RH.
E' solo leggermente differente da quello che avevo scritto qualche giorno fa pero' insomma il concetto è quasi uguale XD
lo sketch funziona si e no perchè: non sempre schiacciando mi passa da una schermata all' altra, la schermata della pressione rimane fintanto che tengo premuto il bottone poi torna di default a T ed RH, i dati si aggiornano sono nel momento in cui schiaccio il bottone. Mi sapresti/e indicare migliorie da apportare per farlo funzionare nella maniera desiderata?
Ho aggiunto il caso default vuoto perché è quello che viene chiamato quando nessuno dei casi precedenti non corrisponde. In questo caso il programma prosegue senza fare niente invece di piantarsi.
Il mio programma era fatto per funzionare con la pull'up sul pulsante. Se tu non la usi, o meglio usi una pull'down, devi cambiare tutti gli HIGH in LOW e i LOW in HIGH, ovvero invertire la logica di azionamento del pulsante.
super gentilissimo!!! grazie mille!!! Pero' rimangono 2 problemini: anche se ora mi è possibile cambiare schermata da pulsante, in automatico mi cambia ancora schermata da solo ogni secondo( tempo di aggiornamento dei dati) e l' lcd sembra che faccia i refresh con la velocità del loop rendendo praticamente impossibile la lettura... si sta rivelando piu' complicato del previsto
Occhei, dopo incavolature su incavolature sono riuscito ad ottenere quello che volevo! Per chi fosse interessato lo sketch intero è questo! Ricordo che i sensori che ho utilizzato sono SHT21 e BMP085 (ho messo un +200hPa nella calibrazione perchè era starato). Il programma acquisisce ogni 60 secondi e stampa sullo schermo seriale. Premendo il bottone collegato all'input 8 è possibile cambiare schermata e visualizzare temp ext ed umidità in una, pressione nell'altra! appena acceso segnerà tutto 0.00... bisogna aspettare 1 minuto! ciaoooo
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SHT2x.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available())
;
return Wire.read();
}
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2)
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
#include <Wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 0; // Oversampling Setting
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5;
short temperature;
long pressure;
// Use these for altitude conversions
const float p0 = 101325; // Pressure at sea level (Pa)
float altitude;
void setup()
{ pinMode(8,INPUT);
Serial.begin(9600);
Wire.begin();
bmp085Calibration();
}
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
// Read the uncompensated temperature value
unsigned int bmp085ReadUT()
{
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp085ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp085ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on OSS
delay(2 + (3<<OSS));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 3);
// Wait for data to become available
while(Wire.available() < 3)
;
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
// Calculate temperature given ut.
// Value returned will be in units of 0.1 deg C
short bmp085GetTemperature(unsigned int ut)
{
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += ((x1 + x2 + 3791)>>4)+200;
return p;
}
float SHTtemp;
float SHTRH;
float temperature1;
float pressure1;
void stampaparametri()
{
Serial.print("Outdoor Temperature: ");
Serial.print(SHTtemp);
Serial.print(" C");
Serial.print(" Humidity: ");
Serial.print(SHTRH);
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(pressure1);
Serial.print(" hPa");
Serial.print(" Indoor Temperature: ");
Serial.print(temperature1);
Serial.println(" C");
}
byte buttonPushCounter = 1;
boolean buttonState = LOW;
boolean lastButtonState = LOW;
const byte maxCase = 2;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long pTime = 0;
unsigned long cTime = 0;
unsigned long p2Time = 0;
unsigned long c2Time = 0;
const unsigned long iTime = 60000UL;
const unsigned long i2Time = 1000UL;
void loop()
{
currentMillis = millis();
cTime = currentMillis;
c2Time = currentMillis;
buttonState = digitalRead(8);
if (cTime - pTime >= iTime)
{
pTime = cTime;
SHTtemp = SHT2x.GetTemperature();
SHTRH = SHT2x.GetHumidity();
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
temperature1 = temperature * 0.1;
pressure1 = pressure * 0.01;
stampaparametri();
}
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
buttonPushCounter++;
if (buttonPushCounter > maxCase) buttonPushCounter = 1;
}
lastButtonState = buttonState;
}
if (c2Time - p2Time >= i2Time)
{
p2Time = c2Time;
if(buttonPushCounter == 1){
lcd.begin(16, 2);
lcd.print("Pressione:");
lcd.setCursor(0, 1);
lcd.print(pressure1);
lcd.setCursor(13, 1);
lcd.print("hPa");
}
if(buttonPushCounter == 2){
lcd.begin(16, 2);
lcd.print("Out Temp:");
lcd.setCursor(10, 0);
lcd.print(SHTtemp);
lcd.setCursor(15, 0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("RH:");
lcd.setCursor(10, 1);
lcd.print(SHTRH);
lcd.setCursor(15, 1);
lcd.print("%");
}
}
}