Does the loop stop working after adding tone () to the my void error()?

hello
I ran into a problem writing my code, I have no idea where I'm doing something wrong.
I wanted to void error (), beep by error. but after adding, void setup () will take place and nothing will happen. (The loop will probably stop working due to adding a tone (buzzer, 1000,250) to void error (), I think).
I could ask if you would help me solve this problem or do it differently.

Thank you in advance for your time and help.


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
//display set
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//inputs
const int DHTInput = 2;
const int DHTInput2 = 3;
const int DHTInput3 = 4;
const int Buzzer = 10;
const int VoltageInput = A0;
const int CurrentInput = A1;
//DHT type set
DHT dht1(DHTInput, DHT22);
DHT dht2(DHTInput2, DHT22);
DHT dht3(DHTInput3, DHT22);
//timing previous
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis5 = 0;
unsigned long previousMillis6 = 0;
//timing set
const long interval_current = 1000;
const long interval_voltage = 1000;
const long interval_temp = 2500;
const long interval_wattage = 1000;
const long interval_Error = 1000;
//voltage values
float adc_voltage = 0.0;
double in_voltage = 0.0;
const float R1 = 29000.0; //real value 33000
const float R2 = 8200.0; 
const float ref_voltage = 5.0;
unsigned int adc_value_voltage = 0;
//current values
const int sensitivity = 50;
unsigned int adc_Value_current= 0;
const int offsetVoltage = 4000;
double adcVoltage = 0;
double currentValue = 0;
//wattage
double watt = 0;
//Error values
const int overcurrent = 20;
const int overvoltage = 7; //
const double undervoltage = 2.6;
//time up
unsigned int sec;
unsigned int min;
unsigned int hours;

void setup() {
  Serial.begin(115200);
  
  
  dht1.begin();
  dht2.begin();
  dht3.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop() {
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval_current) {
    previousMillis = currentMillis;
    Current();
  }
  if (currentMillis - previousMillis2 >= interval_voltage) {
    previousMillis2 = currentMillis;
    Voltage();
  }  
  if (currentMillis - previousMillis3 >= interval_temp) {
     previousMillis3 = currentMillis;
    temp_hum();
    }
  if (currentMillis - previousMillis4 >= interval_wattage) {
     previousMillis4 = currentMillis;
     wattage();
  }
  if (currentMillis >= (previousMillis5)) {
     previousMillis5 = previousMillis5 + 1000;
     timeup();
  } 
  if (currentMillis - previousMillis6 >= interval_Error) {
     previousMillis6 = currentMillis;
     error();
    }
}
void error() {    
    if (currentValue < overcurrent) {
        Serial.println("OVERCURRENT");
             //tone(Buzzer, 1000, 50); //<-----------------------------------------------
             //noTone(Buzzer);//<-----------------------------------------------
        }
    if  (in_voltage < undervoltage) {
        Serial.println("UNDERVOLTAGE");
        }
    if (in_voltage > overvoltage) {
        Serial.println("OVERVOLTAGE");
        }
}
void timeup(){
  sec++;
     if (sec== 60)
     {
        sec= 0;
        min++;
     }
     if (min== 60)
     {
        min= 0;
        hours++;
     }
     if (hours== 13)
     {
        hours= 1;
     }
  Serial.print(hours, DEC);
  Serial.print(":");
  Serial.print(min,DEC);
  Serial.print(":");
  Serial.println(sec,DEC);
}
void wattage(){
  watt = (in_voltage * currentValue);
  Serial.print("watts = ");
  Serial.println(watt);
  }
void Current(){
  adc_Value_current = analogRead(CurrentInput);
  adcVoltage = (adc_Value_current / 1024.0) * 5000;
  currentValue = ((adcVoltage - offsetVoltage) / sensitivity);
  
  Serial.print("Raw Sensor Value = " );
  Serial.println(adc_Value_current);
  Serial.print("Real Sensor Value = " );
  Serial.println(currentValue);
}
void Voltage(){
   // Read the Analog Input
   adc_value_voltage = analogRead(VoltageInput);
   adc_voltage  = (adc_value_voltage * ref_voltage) / 1024.0; 
   in_voltage = adc_voltage / (R2/(R1+R2));
   
  Serial.print("Voltage = ");
  Serial.println(in_voltage);
}
void temp_hum(){
   //read temperature and humidity
    float t1 = dht1.readTemperature();
    float h1 = dht1.readHumidity();
   if (isnan(h1) || isnan(t1)) {
      Serial.println("Failed to read from DHT1 sensor!");
  }
  
  float t2 = dht2.readTemperature();
  float h2 = dht2.readHumidity();
  if (isnan(h2) || isnan(t2)) {
    Serial.println("Failed to read from DHT2 sensor!");
  }
  
  float t3 = dht3.readTemperature();
  float h3 = dht3.readHumidity();
  if (isnan(h3) || isnan(t3)) {
    Serial.println("Failed to read from DHT3 sensor!");
  }

  //clear display
  display.clearDisplay();

  // display temperature
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temp1: ");
  display.setTextSize(1);
  display.setCursor(0,10);
  display.print(t1);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(1);
  display.print("C");
  
  // display humidity
  display.setTextSize(1);       display.setTextSize(1);       display.setTextSize(1);       
  display.setCursor(0, 35);     display.setCursor(0, 35);     display.setCursor(0, 35);     
  display.print("Hum1: ");      display.print("Hum2: ");      display.print("Hum3: ");  
  display.setTextSize(2);       display.setTextSize(2);       display.setTextSize(2);       
  display.setCursor(0, 45);     display.setCursor(0, 45);     display.setCursor(0, 45);     
  display.print(h1);            display.print(h2);            display.print(h3);            
  display.print(" %");          display.print(" %");          display.print(" %");          
  
  display.display(); 
}

So the code posted works?

Can you also post the code that doesn't work.

Which arduino are you using? Does your buzzer pin have another role? (Pin 10 is slave select on a uno for example)

1 Like

Did not get to deep into the code. Are you sure you need to use floats the way you are using them and doubles?

I read the above as if currentvalue is less than overcurrent then serial print "OVERCURRENT". If currentvalue is less than overcurrent why print "OVERCURRENT"? Would it be over current when currentvalue is greater than or equal to overcurrent?

@koudysik, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Be aware that if you pass a duration parameter to tone(), it does not block until that time has elapsed, so having

tone(Buzzer, 1000, 50);
noTone(Buzzer);

will immediately turn off your buzzer, probably faster than you can hear it.

tone(Buzzer, 1000);
delay(50);
noTone(Buzzer);

I apologize for the delay in replying.
Without
// tone (Buzzer, 1000, 50); // noTone (Buzzer);
Yes, it works, but it doesn't. only Void setup () when I finished print ("something"); so it goes.
and code doesn't woks

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
//display set
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//inputs
const int DHTInput = 2;
const int DHTInput2 = 3;
const int DHTInput3 = 4;
const int Buzzer = 10;
const int VoltageInput = A0;
const int CurrentInput = A1;
//DHT type set
DHT dht1(DHTInput, DHT22);
DHT dht2(DHTInput2, DHT22);
DHT dht3(DHTInput3, DHT22);
//timing previous
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis5 = 0;
unsigned long previousMillis6 = 0;
//timing set
const long interval_current = 1000;
const long interval_voltage = 1000;
const long interval_temp = 2500;
const long interval_wattage = 1000;
const long interval_Error = 1000;
//voltage values
float adc_voltage = 0.0;
double in_voltage = 0.0;
const float R1 = 29000.0; //real value 33000
const float R2 = 8200.0; 
const float ref_voltage = 5.0;
unsigned int adc_value_voltage = 0;
//current values
const int sensitivity = 50;
unsigned int adc_Value_current= 0;
const int offsetVoltage = 4000;
double adcVoltage = 0;
double currentValue = 0;
//wattage
double watt = 0;
//Error values
const int overcurrent = 20;
const int overvoltage = 7; //
const double undervoltage = 2.6;
//time up
unsigned int sec;
unsigned int min;
unsigned int hours;

void setup() {
  Serial.begin(115200);
  
  
  dht1.begin();
  dht2.begin();
  dht3.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop() {
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval_current) {
    previousMillis = currentMillis;
    Current();
  }
  if (currentMillis - previousMillis2 >= interval_voltage) {
    previousMillis2 = currentMillis;
    Voltage();
  }  
  if (currentMillis - previousMillis3 >= interval_temp) {
     previousMillis3 = currentMillis;
    temp_hum();
    }
  if (currentMillis - previousMillis4 >= interval_wattage) {
     previousMillis4 = currentMillis;
     wattage();
  }
  if (currentMillis >= (previousMillis5)) {
     previousMillis5 = previousMillis5 + 1000;
     timeup();
  } 
  if (currentMillis - previousMillis6 >= interval_Error) {
     previousMillis6 = currentMillis;
     error();
    }
}
void error() {    
    if (currentValue < overcurrent) {
        Serial.println("OVERCURRENT");
             tone(Buzzer, 1000, 50); 
             noTone(Buzzer);
        }
    if  (in_voltage < undervoltage) {
        Serial.println("UNDERVOLTAGE");
        }
    if (in_voltage > overvoltage) {
        Serial.println("OVERVOLTAGE");
        }
}
void timeup(){
  sec++;
     if (sec== 60)
     {
        sec= 0;
        min++;
     }
     if (min== 60)
     {
        min= 0;
        hours++;
     }
     if (hours== 13)
     {
        hours= 1;
     }
  Serial.print(hours, DEC);
  Serial.print(":");
  Serial.print(min,DEC);
  Serial.print(":");
  Serial.println(sec,DEC);
}
void wattage(){
  watt = (in_voltage * currentValue);
  Serial.print("watts = ");
  Serial.println(watt);
  }
void Current(){
  adc_Value_current = analogRead(CurrentInput);
  adcVoltage = (adc_Value_current / 1024.0) * 5000;
  currentValue = ((adcVoltage - offsetVoltage) / sensitivity);
  
  Serial.print("Raw Sensor Value = " );
  Serial.println(adc_Value_current);
  Serial.print("Real Sensor Value = " );
  Serial.println(currentValue);
}
void Voltage(){
   // Read the Analog Input
   adc_value_voltage = analogRead(VoltageInput);
   adc_voltage  = (adc_value_voltage * ref_voltage) / 1024.0; 
   in_voltage = adc_voltage / (R2/(R1+R2));
   
  Serial.print("Voltage = ");
  Serial.println(in_voltage);
}
void temp_hum(){
   //read temperature and humidity
    float t1 = dht1.readTemperature();
    float h1 = dht1.readHumidity();
   if (isnan(h1) || isnan(t1)) {
      Serial.println("Failed to read from DHT1 sensor!");
  }
  
  float t2 = dht2.readTemperature();
  float h2 = dht2.readHumidity();
  if (isnan(h2) || isnan(t2)) {
    Serial.println("Failed to read from DHT2 sensor!");
  }
  
  float t3 = dht3.readTemperature();
  float h3 = dht3.readHumidity();
  if (isnan(h3) || isnan(t3)) {
    Serial.println("Failed to read from DHT3 sensor!");
  }

  //clear display
  display.clearDisplay();

  // display temperature
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temp1: ");
  display.setTextSize(1);
  display.setCursor(0,10);
  display.print(t1);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(1);
  display.print("C");
  
  // display humidity
  display.setTextSize(1);       display.setTextSize(1);       display.setTextSize(1);       
  display.setCursor(0, 35);     display.setCursor(0, 35);     display.setCursor(0, 35);     
  display.print("Hum1: ");      display.print("Hum2: ");      display.print("Hum3: ");  
  display.setTextSize(2);       display.setTextSize(2);       display.setTextSize(2);       
  display.setCursor(0, 45);     display.setCursor(0, 45);     display.setCursor(0, 45);     
  display.print(h1);            display.print(h2);            display.print(h3);            
  display.print(" %");          display.print(" %");          display.print(" %");          
  
  display.display(); 
}

Nano right now.

  1. Yes as Double if only allows tenths.
  2. yes, this is correct, even if it could be used <= equal to or greater than or when it is greater than ...
    Edit. I was wrong in both...

So I apologize. I chose the wrong location

You can hear 50 ms, I would probably increase it by the end, but so that it doesn't bother me for so long

It never occurred to me, But it's not the same as tone (1000,50); tone (frequency, duration [Delay in one?]); That's how I understood it.

without problem.


And now is stuck. btw. Display is disconnected so there is an error message.

So you are running out of memory
Post the full code within code tags

Hi,
with your help. The whole problem was in Pin 10.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.