Поочередны вывод информации на дисплей без delay()

Нет ответов
Chakatun
Offline
Зарегистрирован: 10.06.2016

Здравствуйте! Нужна помощь. Сделал контроллер температуры и влажности. Железо Ардуино уно, 7 сегментный индикатор 4 цифры с контроллером по I2C   SAA1064 , датчик температуры DS18B20. датчик DHT22 и джойстик для в вода настроек температуры и влажности. Хочу выводить температуру и влажность поочередно с интервалом 3 секунды. При выводе только одного из двух, нажав на джойстик четко вхожу в настройки а при отображении двух поочередно кнопка глючит, редко получается войти в параметры настройки. Понял что это из-за delay().  Помогите пожалуйста вывести инфу без delay. или сделать кнопку на прерывание. Этот код я переделал из нескольких проектов а с прерыванием и с этим millis() не получается. 


#include <Wire.h> 
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>


#define ONE_WIRE_BUS 8
#define axis_X 0
#define dhtPin 5                        // DHT
#define DHTTYPE DHT22   
#define humidifer_pin 12 

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int value_X = 0;
float T; //переменная для хранения температуры
float humidity; // Влажность
int humidity1;
float temp2; // Температура DHT22
int T1;// переменная текущей температуры
int hist=0.1;//гистерезис
float Tset;
float set_humidity;
const int buttonPin1 = 2;//кнопка  режима установки температуры
byte meniu = 0;
int buzzerPin = 9;
int numTones=11;

DHT dht(dhtPin, DHTTYPE);
int tones[]= {261,277,294,311,330,349,370,392,415,440,2000};
byte saa1064 = 0x70 >> 1;  // define the I2C bus address for our SAA1064 (pin 1 to GND)
int digits[15]={
63, 6, 91, 79, 102, 109, 125,7, 127, 111, 99,92, 0, 119, 28}; // 204 original Schrank, 225 Platine SAA1064
// these are the byte representations of pins required to display each digit 0~9, °C sign, and blank digit, A, u
// right hand digit 180° flip - DP ist used as ° symbol :-) 204 is °c for big C use 143

void setup()

{
  for (int i=0; i< numTones; i++)
  {
    tone(buzzerPin,tones[i]);
    delay(50);
    
    noTone(buzzerPin);
  }
 
 Wire.begin(); // start up I2C bus
 delay(100);
 initDisplay();
 
  pinMode(13, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(humidifer_pin, OUTPUT);

  digitalWrite(buttonPin1, HIGH);
  digitalWrite(humidifer_pin, LOW);
  sensors.begin();
  dht.begin(); 
 }
void initDisplay()
// turns on dynamic mode and adjusts segment current to 2mA
{
  Wire.beginTransmission(saa1064);
  Wire.write(B00000000); // this is the instruction byte. Zero means the next byte is the control byte
  Wire.write(B01010111); // control byte (dynamic mode on, digits 1+3 on, digits 2+4 on, 21mA segment current
  // left 111 is relevant for current left hand "1" 12mA, middle 6mA and right 3mA - so you can tewak it
  Wire.endTransmission();
}

void ClearDisplay()
{
  int thousand1, hundred1, ten1, one1;
// breakdown number into columns
hundred1 = Tset /100;
ten1 = (Tset-(hundred1*100))/10; 
one1 = Tset-((hundred1*100)+(ten1*10));

  Wire.beginTransmission(saa1064);
  Wire.write(1); // start with digit 1 (right-hand side)
  Wire.write(digits[hundred1]); // blank digit 1
  Wire.write(digits[ten1]+128); // blank digit 2
  Wire.write(digits[one1]); // blank digit 3
  Wire.write(0); // blank digit 4
  Wire.endTransmission();
  delay(50);
}

 void ClearDisplayHUM()
{
  int thousand1, hundred1, ten1, one1;
// breakdown number into columns
hundred1 = set_humidity /100;
ten1 = (set_humidity-(hundred1*100))/10; 
one1 = set_humidity-((hundred1*100)+(ten1*10));

  Wire.beginTransmission(saa1064);
  Wire.write(1); // start with digit 1 (right-hand side)
  Wire.write(digits[hundred1]); // blank digit 1
  Wire.write(digits[ten1]+128); // blank digit 2
  Wire.write(0); // blank digit 3
  Wire.write(0); // blank digit 4
  Wire.endTransmission();
  delay(50);
}
void displayDigitsTEMP()
{
int thousand1, hundred1, ten1, one1;
// breakdown number into columns
hundred1 = Tset /100;
ten1 = (Tset-(hundred1*100))/10; 
one1 = Tset-((hundred1*100)+(ten1*10));


  
  Wire.beginTransmission(saa1064);
  Wire.write(1); // start with digit 1 (right-hand side)
  Wire.write(digits[hundred1]); // blank digit 1
  Wire.write(digits[ten1]+128); // blank digit 2
  Wire.write(digits[one1]); // blank digit 3
  Wire.write(digits[10]); // blank digit 4
  Wire.endTransmission();
  delay(50);
}


void displayDigitsHUM()
{
int thousand1, hundred1, ten1, one1;
// breakdown number into columns
hundred1 = set_humidity /100;
ten1 = (set_humidity-(hundred1*100))/10; 
one1 = set_humidity-((hundred1*100)+(ten1*10));


  
  Wire.beginTransmission(saa1064);
  Wire.write(1); // start with digit 1 (right-hand side)
  Wire.write(digits[hundred1]); // blank digit 1
  Wire.write(digits[ten1]+128); // blank digit 2
  Wire.write(digits[10]); // blank digit 3
  Wire.write(digits[11]); // blank digit 4
  Wire.endTransmission();
  delay(50);
}


void displayInteger1(int num, int zero)
{
  int hundred, ten, one;
  // breakdown number into columns
  hundred = num/100;
  ten = (num-(hundred*100))/10; 
  one = num-((hundred*100)+(ten*10));
  if (zero==1) // yes to leading zero
{
  Wire.beginTransmission(saa1064);
  Wire.write(1);
  Wire.write(digits[hundred]);
  Wire.write(digits[ten]+128); // 128 turns on DP
  Wire.write(digits[one]);
  Wire.write(digits[10]);     // print position 10 from arry - °C
  Wire.endTransmission();
  delay(10);
}
  else
  if (zero==0)                // no to leading zero
{
  if (hundred==0) { hundred=11; }
  if (hundred==0 && num<100) { hundred=16; }
  if (ten==0 && num<10) { ten=16; }

  Wire.beginTransmission(saa1064);
  Wire.write(1);
  Wire.write(digits[hundred]);
  Wire.write(digits[ten]+128);
  Wire.write(digits[one]);
  Wire.write(digits[10]);
  Wire.endTransmission();
  delay(10);
}


}

void displayInteger2(int num, int zero)
{
  int hundred, ten, one;
  // breakdown number into columns
  hundred = num/100;
  ten = (num-(hundred*100))/10; 
  one = num-((hundred*100)+(ten*10));
  if (zero==1) // yes to leading zero
{
  Wire.beginTransmission(saa1064);
  Wire.write(1);
  Wire.write(digits[hundred]);
  Wire.write(digits[ten]+128); // 128 turns on DP
  Wire.write(digits[10]);
  Wire.write(digits[11]);     // print position 10 from arry - °C
  Wire.endTransmission();
  delay(10);
}
  else
  if (zero==0)                // no to leading zero
{
  if (hundred==0) { hundred=11; }
  if (hundred==0 && num<100) { hundred=16; }
  if (ten==0 && num<10) { ten=16; }

  Wire.beginTransmission(saa1064);
  Wire.write(1);
  Wire.write(digits[hundred]);
  Wire.write(digits[ten]+128);
  Wire.write(digits[10]);
  Wire.write(digits[11]);
  Wire.endTransmission();
  delay(10);
}


}
  void loop(void) {
    
    temp2 = dht.readTemperature();
    humidity = dht.readHumidity();
    
      
  value_X = analogRead(axis_X);
  
  if (digitalRead(buttonPin1) == LOW) 
{ meniu = meniu + 1;
delay(1000);
}
if (meniu == 2) meniu = 0;

if (meniu == 0) {

  while (meniu == 0) {
    sensors.requestTemperatures(); // запрос на получение температуры
  T=(sensors.getTempCByIndex(0)); 
  
  T1 = T*10;
       
 temp2 = dht.readTemperature();
    humidity = dht.readHumidity();
    
    humidity1 = humidity*10;
    
 displayInteger1(T*10,0); //Вывод температуры на дисплей
  delay(5000);
  displayInteger2(humidity*10,0);
  delay(5000);
  if (T>(Tset/10)+hist)digitalWrite(13, HIGH);
  if (T<(Tset/10))digitalWrite(13, LOW );
  


if (digitalRead(buttonPin1) == LOW) 
{ meniu = 1;
delay(1000);
}

} 
delay(5);  
}


  if (meniu == 1) {
   while (meniu == 1) {
value_X = analogRead(axis_X);
  
    

   if(value_X > 700)
  {
    Tset++;
    
  
    tone(buzzerPin,tones[10]);
    delay(10);
    
    noTone(buzzerPin);
  
     
  }

  value_X = analogRead(axis_X);
     
 
   if (value_X < 300)
  {
    Tset--;
   
    
  
    tone(buzzerPin ,tones[10]);
    delay(10);
    
    noTone(buzzerPin);
  
     
  }

 displayDigitsTEMP();// Вывод настройки температуры
 
 ClearDisplay();
  
  if (digitalRead(buttonPin1) == LOW) 
  { meniu = 2;
  delay(1000);
  }

     }
    




  if (meniu == 2) {
   while (meniu == 2) {
value_X = analogRead(axis_X);
  
    

   if(value_X > 700)
  {
    set_humidity++;
    
  
    tone(buzzerPin,tones[10]);
    delay(10);
    
    noTone(buzzerPin);
  
     
  }

  value_X = analogRead(axis_X);
     
 
   if (value_X < 300)
  {
    set_humidity--;
   
    
  
    tone(buzzerPin ,tones[10]);
    delay(10);
    
    noTone(buzzerPin);
  
     
  }

 displayDigitsHUM();// Вывод настройки температуры
 
 ClearDisplayHUM();
  
  if (digitalRead(buttonPin1) == LOW) 
  { meniu = 0;
  delay(1000);
  }

     }
    

}
}}