R
R
Rinat_20012021-06-16 13:04:11
Arduino
Rinat_2001, 2021-06-16 13:04:11

How to add to the sketch for Arduino so that the LEDs turn on only under the necessary condition (below)?

I am a beginner Arduino programmer, who just a week ago began to delve into this topic. And here is my first problem and the first unsuccessful experience of fixing something myself. From you, I ask you to correct a little sketch (not mine) so that it meets the following parameters: when the Arduino UNO board is connected, the LEDs should not turn on, they should work only at that moment When one of the two actions occurs - the temperature is above 35 degrees - the red LED turns on, the temperature is below 20 degrees - the green LED. In all other cases, the LEDs must be off. Please help)).

#include <Wire.h>
#include <Wire.h>
#include <OneWire.h> 
#include <DallasTemperature.h> //подключение библеотек для DS18B20
#define ONE_WIRE_BUS 10 // pin к которому подключен DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <OLED_I2C.h>
OLED  myOLED(A4, A5, A4); // Подключение дисплея
extern uint8_t RusFont[]; // Русский шрифт
extern uint8_t BigNumbers[]; // Большие цифры
extern uint8_t SmallFont[]; // Маленький шрифт
void setup()
{
  pinMode(13, OUTPUT); // Объявить pin 13 как выход
  pinMode(2, OUTPUT);// Объявить pin 2 как выход
  pinMode(5, OUTPUT); // Объявить pin 5 как выход
  sensors.begin();// Включение датчика
  myOLED.begin();// Включение дисплея
  myOLED.setFont(RusFont);
}
void loop()
{
 if (sensors.getTempCByIndex(0)>35)//Проверка датчика температуры(при температуре >35 будет включаться лампочка и сирена)
{
   digitalWrite(5,HIGH);// Включение лампочки
   tone(13, 2500);// Включаем звук 2500 Гц
   delay(1000);// Пауза 1 сек
   digitalWrite(5,LOW);// Выключение лампочки
  }
else // иначе(при несоблюдении условия выше)
{
  if (sensors.getTempCByIndex(0)<20)//Проверка датчика температуры(при температуре <20 будет включаться лампочка и сирена)
  {
    digitalWrite(2,HIGH);// Включение лампочки
    tone(13, 2500); // Включаем звук 2500 Гц
    
  }
 else // иначе(при несоблюдении условий выше)
  {
    noTone(13);// выключение звука
    digitalWrite(2,LOW);
    digitalWrite(5,LOW);    
  }
}
sensors.requestTemperatures();
myOLED.clrScr(); // Очищаем экран
myOLED.setFont(RusFont);
myOLED.print("Ntvgthfnehf", CENTER, 0); // Вывод текста русскими буквами
myOLED.setFont(SmallFont);
myOLED.setFont(RusFont);
myOLED.print("Hbyfn Fqcbyjd", CENTER, 55); // Вывод текста 
myOLED.setFont(BigNumbers);
myOLED.print(String(sensors.getTempCByIndex(0) , 1), CENTER, 10);   // Отображение значения температуры, с точностью до десятых
myOLED.update();
delay(100);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kalapanga, 2021-06-16
@kalapanga

You have three temperature intervals (<20, 20-35, >35) and two indicators (red, green). You need to register the control of both indicators
for each interval. And now you control in one case only red, in the other only green. And only in one of the three cases with both (when both are turned off).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question