Some help needed with my project

Hello! My name is Clara and I would like to have some help with my project.
I have never taken any programming classes in my entire life and this is also my first engineering class. I know my country kind of sucks :frowning:
For my first project I only copied and pasted the code from the arduino website.
I have another project and I have been working on some coding with a classmate but I am kind of lost because I would like to add a personal final touch to the project.
The first part of the code is for the humidity and temperature sensor. The second part is for the alarm (buzzer) and my classmate was trying to explain to me what the coding actually means but I am a slow learner and he is fast for me because he knows programming.
Now, I would like to add a part where when I push a button, the alarm stops.
I have been looking over the internet and everybody keeps talking about both the “on” and “off” button. I just want that button to stop the alarm, not to make it ring. I think it might be something easy to do but this coding is killing me.
One thing I noticed is that we have a lot of "void"and I read somewhere about the “break” It apparently can do what I want to accomplish but I am not so sure. Don’t get me wrong, I love what people can do with arduino and I’m all about taking risks but my classmate told me to be careful because I could easily break something or even burn my LCD screen if I keep playing around.

Oh! I would also like it to play some real music (I used my imagination for what it is doing right now. I did not care about the keys and all that! I personally think it is kind of annoying lol) or say something like “Danger! Please, check your 3D printing filament.” but I can worry about it later. Right now I’m only looking for a code for the stop button.
I don’t have my arduino with me right now but I think my pin 8 is free, if that’s a significant information.

Thank you in advance for your help :slight_smile:

Here is the code:

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7

void setup(){
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop()
{
int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print(“C”);
lcd.setCursor(0,1);
lcd.print(“Humidity: “);
lcd.print(DHT.humidity);
lcd.print(”%”);
delay(1000);
Serial.println(DHT.humidity);
buzzer();

}

void buzzer()
{
if (DHT.humidity >= 19 or DHT.temperature >= 25) {
tone (8,5000,1000);
delay(250);
tone (8,1500,1000);
//delay(250); //buzzer
delay(400);
tone (8,2500,1000);
delay(450);
tone(8,3500,1000);
delay(400);
tone(8, 4000, 1000);
delay(250);
tone(8, 4200, 1000);

//delay(500);// buzzer
}}

Please use code tags, </> above smilies.

‘void’ means that the function - the name before the ‘()’ - returns nothing to the function which called it.

In the IDE read up on these: File/examples/2.digital/ debounce & state change detection.

Pin 8 is a digital input. You can bring in your button there and process it through the items mentioned above to use in the sketch. FYI, The analog inputs A0-A5 can also be used as digital inputs.

There’s also a function called no tone() which stops tone output.

The biggest problem that you’re going to have is the fact that your buzzer routine blocks for its entirety. That means that it could only get back to check the button once every 1.75 seconds. Have a look at the Blink without delay example and the great thread about millis for newbies at the top of the programming questions section for some inspiration about how to handle your timing without blocking.

Hello guys!
Thank you for trying to help me out with my project but I guess not knowing any programming is the reason why I am stuck right now. I am pretty sure I did everything right with the wiring but coding is the only issue I am having now.
I would like someone to show me where exactly I am messing up with my code and what I should change. I have tried to google and check some other questions on arduino but it seems like not having any clue on programming is really the issue because there are some wording that I sincerely do not understand.
I’ve tried to check “void” and they keep telling me the same thing but I do not understand the meaning.

Here is my new code after doing some research. I added some comments so you could follow what I am doing.

#include <dht.h>
#include <LiquidCrystal.h>

const int pinBuz = 8;  //Buzzer Pin
const int pinSwi = 13; //Push-button pin

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7

void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600); //Opens Serial communication
  pinMode(pinBuz, OUTPUT); //Defines pinBuz as an Output
  pinMode(pinSwi, INPUT); //Defines pinSwi as an input
}

void loop()
{
  
  int chk = DHT.read11(DHT11_PIN);
  lcd.setCursor(0,0); 
  lcd.print("Temp: ");
  lcd.print(DHT.temperature);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(DHT.humidity);
  lcd.print("%");
  delay(1000); 
  Serial.println(DHT.humidity);
  buzzer();

}

void buzzer()
{ 
  int botao; //To save the last logic state of the button
    botao = digitalRead(pinSwi); //Put the reading value of the switch on botao
  Serial.println(botao); //Shows the logic state of the input on Serial Monitor
  if (botao == 1) //Pressed button, logic State HIGH (5V)
  {
    digitalWrite (pinBuz, 0); //Switch pressed, buzzer off
  }
  else
  {digitalWrite (pinBuz, 1); //If the switch isn't pressed, buzzer on
  }
if (DHT.humidity >= 15 or DHT.temperature >= 20) {
  tone (8,784,300); 
  delay(300);
  noTone(10);
  tone (8,659,300);
  //delay(300); //buzzer
  delay(300);
  noTone(10);
  tone (8,698,300);
  delay(300);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,300);
  delay(500);
  noTone(10);
  tone(8,880,300);
  delay(300);
  noTone(10);
  tone(8,988,300);
  delay(300);
  noTone(10);
  tone(8,1047,500);
  delay(500);
  noTone(10);
  tone(8,1047,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,659,300);
  delay(300);
  noTone(10);
  tone(8,698,300);
  delay(300);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,800,300);
  delay(300);
  noTone(10);
  tone(8,784,300);
  delay(300);
  noTone(10);
  tone(8,698,500);
  delay(500);
  noTone(10);
  tone(8,698,500);
  delay(500);
  noTone(10);
  tone(8,659,500);
  delay(500);
  tone(8,784,500);
  delay(500);
  tone(8,523,500);
  delay(500);
  tone(8,659,500);
  delay(500);
  tone(8,587,500);
  delay(500);
  tone(8,698,1000);
  delay(1000);
  tone(8,494,500);
  delay(500);
  tone(8,523,1000);
  delay(1000);
  noTone(20);
  tone(8,784,300);
  delay(300);
  noTone(10);
  tone(8,659,300);
  delay(300);
  noTone(10);
  tone(8,698,300);
  delay(300);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,880,300);
  delay(300);
  noTone(10);
  tone(8,988,300);
  delay(300);
  noTone(10);
  tone(8,1047,500);
  delay(500);
  noTone(10);
  tone(8,1047,500);
  delay(500);
  noTone(10);
  tone(8,659,300);
  delay(300);
  noTone(10);
  tone(8,698,300);
  delay(300);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,500);
  delay(500);
  noTone(10);
  tone(8,784,300);
  delay(300);
  noTone(10);
  tone(8,880,300);
  delay(300);
  noTone(10);
  tone(8,784,300);
  delay(300);
  noTone(10);
  tone(8,698,500);
  delay(500);
  noTone(10);
  tone(8,698,500);
  delay(500);
  noTone(10);
  tone(8,659,500);
  delay(500);
  tone(8,784,500);
  delay(500);
  tone(8,523,500);
  delay(500);
  tone(8,659,500);
  delay(500);
  tone(8,587,500);
  delay(500);
  tone(8,698,1000);
  delay(1000);
  tone(8,494,500);
  delay(500);
  tone(8,523,1000);
  delay(1000);
  
  
  //delay(500 or 300 or 1000);// buzzer
}}

Right now my melody/alarm is playing but I would like it to stop when I use the push-button.
Just like an alarm stops when you press a button. My temperature and humidity sensor has an if statement, which says that whenever the humidity or temperature reaches some value, the alarm goes off. The code I am looking for is to add on that task: whenever the alarm goes off, the push-button makes it stop even though it will play again whenever the temperature or humidity reaches the said value.

Yes, the problem with your alarm is that it uses delay. Imagine you have a little man doing things instead of a processor. You've told him to make a tone and then go to sleep for a time and then wake up and change the tone and then go to sleep for a while and then wake up and change the tone.

It would make more sense for you to tell him to keep doing all those other things, checking buttons and humidity and all that jazz and also keep checking each loop if we are in an alarm state and if it is time to change the note.

In my last reply I pointed you towards one example and a thread for newbies on timing. Have you read them yet? What parts are confusing you?

Clara96:
Hello guys!
Thank you for trying to help me out with my project but I guess not knowing any programming is the reason why I am stuck right now. I am pretty sure I did everything right with the wiring but coding is the only issue I am having now.
I would like someone to show me where exactly I am messing up with my code and what I should change. I have tried to google and check some other questions on arduino but it seems like not having any clue on programming is really the issue because there are some wording that I sincerely do not understand.
I've tried to check "void" and they keep telling me the same thing but I do not understand the meaning.

Well if you are sure that your problem is a lack of knowledge about coding then google "C++ tutorial" and spend a day or two learning the language.

Thank you so much for guiding me! I have found a solution for my coding and it is now doing everything I wanted it to do.
I did not use millis or anything like that, I used "break" and redefined some things.
You are absolutely right though! I should definitely spend some time learning C++ because it seems so much fun and it's just amazing all the stuff we can accomplish with it! :slight_smile:
I could share my code if anyone wanted to see what I was up to but I guess a lot of you guys are geniuses and it would definitely take you less time to accomplish what I spent days doing!

Once again, thank you guys! :slight_smile:

Clara96:
Thank you so much for guiding me! I have found a solution for my coding and it is now doing everything I wanted it to do.
I did not use millis or anything like that, I used "break" and redefined some things.
You are absolutely right though! I should definitely spend some time learning C++ because it seems so much fun and it's just amazing all the stuff we can accomplish with it! :slight_smile:
I could share my code if anyone wanted to see what I was up to but I guess a lot of you guys are geniuses and it would definitely take you less time to accomplish what I spent days doing!

Once again, thank you guys! :slight_smile:

Using 'break' to accomplish non-blocking code is novel. Care to share your final code?

btw, I'm certainly NOT one of the geniuses here!

Hi,
Yes please post your working code to help finish this post.

In the spirit of using the forum for advice posting your solution will be appreciated.

I may be used by someone with a similar hassle.

Thanks.. Tom.. :slight_smile:

Hello guys!
So, here is the code I’m using and this is what it is for:

  1. To display the temperature (in celsius) and humidity

  2. An alarm goes off when we reach a given (peak) temperature or humidity (I am trying to protect the 3D filament for my project and that’s why I need an alarm to let me know that it is in danger)

  3. The alarm stops completely when I use the switch to turn it off

  4. The alarm doesn’t go off when the temperature or humidity is below the peak/given (the one you chose since it is impossible to have a 99 degree Celsius for now as the peak temperature or a high humidity)

** I am presenting in a classroom and that’s why I am using random numbers just to prove that the code is working. I cannot have the real feel of 99 degree celsius since my project will be in mid-December in the USA.

P.S. My comments are probably not the best and I am not done commenting some parts!

I really hope this helps :wink:

#include <dht.h>            //
#include <LiquidCrystal.h>  //

const int pinBuz = 8; //Buzzer Pin
const int pinSwi = 9; //Switch (button) pin
 

//The line bellow is the frequencies that help to play the music. We have declared it
//An array is a collection of variables that are accessed with an index number. We have 55 different frequencies for this alarm.
int tones[]= {784, 659, 698, 784, 784, 784, 784, 880, 988, 1047, 1047, 784, 659, 698, 784, 784, 784, 800, 784, 698, 698, 659, 784, 523, 659, 587, 698, 494, 523, 784, 659, 698, 784, 784, 880, 988, 1047,1047, 659, 698, 784, 784, 784, 880, 784, 698, 698, 659, 784, 523, 659, 587, 698, 494, 523};

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  //The pins the lcd is connected to

dht DHT;  //The name of the humidity and temperature sensor in the Arduin library 

#define DHT11_PIN 7  //The pin connected to the temperature and humidity sensor

void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600); //Opens Serial communication
  pinMode(pinBuz, OUTPUT); //Defines pinBuz as an output
  pinMode(pinSwi, INPUT); //Defines pinSwi as an input
}

void loop() //The loop shows that the values displayed on the lcd will show forever
{
  
  int chk = DHT.read11(DHT11_PIN);
  lcd.setCursor(0,0); 
  lcd.print("Temp: ");
  lcd.print(DHT.temperature);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(DHT.humidity);
  lcd.print("%");
  delay(1000); 
  Serial.println(DHT.humidity);
  
 if(digitalRead(pinSwi)){
    buzzer(); //If the switch is turned on (is able to being read) then it calls the buzzer, also known as the melody (the melody/alarm goes off)
  }
  

}

void buzzer()  //This declares what we want the buzzer to do
{ 
//The next line is the statement that defines the peak temeperature and humidity 
if (DHT.humidity >= 100 or DHT.temperature >= 100) {
   //i=0 is the first frequency and we set i<55 because we have 55 notes. By setting i<54 or i<56 the alarm will miss a note or will add some buzz respectively. At i = 0  it counts the first frequency/melody
  for(int i=0;i<55;i++){
    tone (8,tones[i],300); //8 is the pin the buzzer is connected to; tones [i] means the melody starts with the first frequency, which is 784; 300 is the time in milliseconds of each tone
    delay(300); //This is the delay from one tone to another
    noTone(10);
    if(!digitalRead(pinSwi)) break;  //If the switch is turned off, the melody/alarm stops. That's what the "!" means
    
  }

}


}

This is not the final code! I fell like I still have to change something about it but if any of you catches my mistake, please let me know.
I will definitely continue working on it. I am just currently busy with my other classes.

**I will post my final update.

The break is a band-aid to deal with the fact that your buzzer is still blocking with delay and a for loop that insists on finishing the whole thing in one call. You may have solved your immediate issue, but you've painted yourself into a corner if you want to be able to do anything more. You've already been pointed towards reaources on writing non-blocking code. Your best bet would be to study and understand them.