Programming thermometer, buzzer, LED and photoresistor at once.

Hello, I’m new to Arduino and I need help with a project I’m doing. The first part is to make Arduino record temperatures while the LED blinks and the buzzer must beep once at the beginning and once at the end of the recording. The recording also takes place when the value of the photoresistor drops below a certain threshold, in other words the aforementioned process takes place in the dark and stops when in light.

My problem is that while I had the LED, photoresistor and thermometer working in those conditions, I can’t seem to program the buzzer. All I have managed is for it to blink once when the process starts but not when it ends. My code is like this:

/*

int sensorPin = 0;            
unsigned int sensorValue = 0;  
float tempK=0, tempC=0, tempF=0;


void setup()
{
  pinMode(7, OUTPUT);
  
  Serial.begin(9600);       
  pinMode(9, OUTPUT); 
}

void loop()
{
  
  sensorValue = analogRead(sensorPin);  

  if (sensorValue==399){
    beep(200);
  }
  else if (sensorValue==400) {
    beep(200);
  }
 
      
 
     
  if(sensorValue<400) {

    
   
  digitalWrite(7, HIGH);  
  delay(1000);              
  digitalWrite(7, LOW);   
  delay(1000);
  {
 
  tempK = (analogRead(3) * 0.004882812 * 100)-10 ;    
 
  tempC = tempK - 273.15;    
 
  tempF = ((tempK) * 9 / 5) - 459.67;   
 
 
  Serial.print("Kelvin: "); Serial.println(tempK);
  Serial.print("Celsius: "); Serial.println(tempC);
  Serial.print("Fahrenheit: "); Serial.println(tempF);
  Serial.println();   
 
  delay(1000);   
}
  }  
  else {
  digitalWrite(7, LOW);
  }  
  
  
}

void beep(unsigned char delayms){
  analogWrite(9, 20);      
  delay(delayms);          
  analogWrite(9, 0);      
  delay(delayms);          
}

Could maybe someone take a look? Also I feel like the second if statement is redundant.

Hey, and welcome to the forum!

What are pins 7 and 9 that declared as outputs in void setup?

marco

Well met!

7 is for the LED and 9 is for the buzzer.

Is this your classmate?

Seems everyone has something on pin 7 and is testing for sensor == 399 today. :slight_smile:

Actually yes, we're classmates! Also we're making the project together but she worded the topic a bit plain. Info is missing and so I thought I would make a different one to clarify the whole project.

lionik:
Actually yes, we’re classmates! Also we’re making the project together but she worded the topic a bit plain. Info is missing and so I thought I would make a different one to clarify the whole project.

Great. So could you clarify it? Starting with, what is so special about 399? Then, please make the requirements more explicit.

Nothing actually. But I guessed it's the first value the sensor is going to get when the recording starts. We will have the Arduino outside and leave it there for a day and a night. So as the lighting slowly starts to reduce so will the value the sensor gets. I figured that since we've put the threshold for the recording as <400 the first value to be used will be 399 and so using this I was going to program the buzzer to off.

I could certainly be wrong in my thought process though.

I could certainly be wrong in my thought process though.

Yes, indeed. You could KNOW the first value, by reading the sensor in setup().

lionik:
Nothing actually. But I guessed it’s the first value the sensor is going to get when the recording starts. We will have the Arduino outside and leave it there for a day and a night. So as the lighting slowly starts to reduce so will the value the sensor gets. I figured that since we’ve put the threshold for the recording as <400 the first value to be used will be 399 and so using this I was going to program the buzzer to off.

I could certainly be wrong in my thought process though.

Well, C++ has a “<” operator. Why not use it?

PaulS:
Yes, indeed. You could KNOW the first value, by reading the sensor in setup().

I don’t quite follow you, could you maybe explain what you mean a bit more.

aarg:
Well, C++ has a “<” operator. Why not use it?

If I use a “<” wouldn’t the buzzer be going off as long as the value is indeed <400, meaning it would be beeping all night since the whole function is under loop? That’s why I figured an absolute value would be needed.

could you maybe explain what you mean a bit more.

Do you know the difference between setup() and loop()?
Have you tried reading the analog pin in setup() to see what the initial value IS?

If not, why not?

If I use a “<” wouldn’t the buzzer be going off as long as the value is indeed <400, meaning it would be beeping all night since the whole function is under loop? That’s why I figured an absolute value would be needed.

Only if you fail to program a delay into the beep. You can’t just test for a value because there is noise in the readings and it might slip past, like 401, 400, 398… even if that were not true, you can not predict how long it will remain at 399, so your beep might be too short to hear, or annoyingly long.

This is why, you need to develop a much clearer idea of the requirements of the behaviour of the program, before you begin coding.

I asked you to state them, and you haven’t yet. In clear, precise language, what must this program do?

aarg:
Only if you fail to program a delay into the beep. You can't just test for a value because there is noise in the readings and it might slip past, like 401, 400, 398...even if that were not true, you can not predict how long it will remain at 399, so your beep might be too short to hear, or annoyingly long.

This is why, you need to develop a much clearer idea of the requirements of the behaviour of the program, before you begin coding.

The program has to record the temperatures at night time and stop in daylight(when the photoresistor sensor gets a lower and higher value than a threshold respectively) and while the recording takes place the LED must be blinking. Also the buzzer must go off once that process starts and once it ends.

The program has to record the temperatures at night time and stop in daylight(when the photoresistor sensor gets a lower and higher value than a threshold respectively) and while the recording takes place the LED must be blinking. Also the buzzer must go off once that process starts and once it ends.

So, what is the problem? Do you know what the "it got dark" threshold is? Do you know what the "it got light" threshold is? It may not be the same as the "it got dark" threshold.

Do you know how to make the LED blink WITHOUT using delay()? There is an example...

Do you know how to make the buzzer make some noise?

PaulS:
So, what is the problem? Do you know what the "it got dark" threshold is? Do you know what the "it got light" threshold is? It may not be the same as the "it got dark" threshold.

Do you know how to make the LED blink WITHOUT using delay()? There is an example...

Do you know how to make the buzzer make some noise?

The lux levels that I'm using, particularly 400, is the sunset/sunrise level and so the one, I think, I should be using. I have made the LED blink without delay using examples and also made the buzzer ring.

The problem I'm having with my code is that, while it beeps once for when the recording starts, it doesn't beep when it ends. Maybe I should set the second sensorValue higher and not exactly 400?

For the beep, you should look at the state change example in the IDE examples. I tire of hinting, so I'll flat out tell you to make it a timed beep.

The problem I'm having with my code is that, while it beeps once for when the recording starts, it doesn't beep when it ends. Maybe I should set the second sensorValue higher and not exactly 400?

Maybe you should start over. Now that you have some stuff working, it isn't that difficult to do it again, right this time.

Make a function to get the light level.
Make a function to get the temperature.
Make a function to blink the LED.
Make a function to buzz the buzzer.
Make two functions to return true or false, with respect to changes in light level.

Then, loop() can be quite simple:

void loop()
{
   if(gotDark())
      buzz();

   if(gotLight())
      buzz();

   if(isDark())
   {
      wasteTimeBlinking();
      recordTemperature();
   }
}

Thanks for the help guys, I'll try your recommendations as soon as possible and report my results.