Hello,
My Arduino nano doesn't work as it should everytime I connect the buzzer to it.
My Arduino nano is connected to a LM35, a buzzer and 3 LEDs. My program blinks a specific LED and makes a noise for each temperature condition it meets. The problem is when I run my program, it blinks all the LEDs and make all the noise while my serial monitor outputs random temperature readings. When using another program to read the temperature only, the temperature is precise and is not random unlike the program that I'm using.
When I removed the buzzer, the LEDs work as they should depending on the temperature. I tried changing the pins for the LM35, buzzer and each LEDs but the same problem remains.
The code that I have:
/* Sensors */
int tempSensor = A6; //LM35 temp sensor
/* LEDs */
int ledNormalPin = 10;
int ledHypoPin = 11;
int ledFeverPin = 12;
/* Buzzer */
int buzzerPin = 9;
/*State*/
int ledNormalState = LOW;
int ledHypoState = LOW;
int ledFeverState = LOW;
/* Notes */
const int nHypo = 5500;
const int fHypo = 4000;
const int nFever = 700;
const int fFever = 900;
/* Duration */
const int dnHypo = 140;
const int dfHypo = 80;
const int dnFever = 140;
const int dfFever = 80;
/* Constants */
const float celcius = 0.48828125; //500/1024
const float hypoTemp = 36.5;
const float feverTemp = 38.0;
/* Two "independant" timed events */
const long eventTime_0_temp = 1000; //in ms
const long eventTime_normal_on = 300; //in ms
const long eventTime_normal_off = 1000; //in ms
const long eventTime_hypo_on = 300; //in ms
const long eventTime_hypo_off = 1000; //in ms
const long eventTime_fever_on = 300; //in ms
const long eventTime_fever_off = 1000; //in ms
/* past millis() */
unsigned long previousTime_0 = 0;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
void setup() {
Serial.begin(9600);
pinMode(ledNormalPin, OUTPUT);
pinMode(ledHypoPin, OUTPUT);
pinMode(ledFeverPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(tempSensor, INPUT);
}
void loop() {
/* Updates time frequently */
unsigned long currentTime = millis();
float temp = analogRead(tempSensor)*celcius;
/* Temperature reading */
if ( currentTime - previousTime_0 >= eventTime_0_temp) {
Serial.print("Temp: ");
Serial.println(float(analogRead(tempSensor)*celcius));
/* Update the timing for the next event*/
previousTime_0 = currentTime;
}
/* Normal */
if ((ledNormalState == LOW)&&(currentTime - previousTime_1 >= eventTime_normal_on)&&(temp > hypoTemp)&&(temp < feverTemp)) {
//noTone(buzzerPin);
ledNormalState = HIGH;
//tone(buzzerPin, nHypo, dnHypo);
Serial.print("Normal state: High\n");
//Serial.println(float(analogRead(tempSensor)*celcius));
digitalWrite(ledNormalPin, ledNormalState);
/**/
/**/
previousTime_1 = currentTime;
}
else if ((ledNormalState == HIGH)&&(currentTime - previousTime_1 >= eventTime_normal_off)) {
//noTone(buzzerPin);
ledNormalState = LOW;
//tone(buzzerPin, fHypo, dfHypo);
Serial.print("Normal state: Low\n");
//Serial.println(float(analogRead(tempSensor)*celcius));
digitalWrite(ledNormalPin, ledNormalState);
/**/
/**/
previousTime_1 = currentTime;
}
/* Cold */
/**/
if ((ledHypoState == LOW)&&(currentTime - previousTime_2 >= eventTime_hypo_on)&&(temp <= hypoTemp)) {
//for(int i = 0; i <=1; i++ ){
noTone(buzzerPin);
ledHypoState = HIGH;
tone(buzzerPin, nHypo, dnHypo);
Serial.print("Hypo state: High\n");
//Serial.println(float(analogRead(tempSensor)*celcius));
digitalWrite(ledHypoPin, ledHypoState);
//}
/**/
/**/
previousTime_2 = currentTime;
}
else if ((ledHypoState == HIGH)&&(currentTime - previousTime_2 >= eventTime_hypo_off)) {
noTone(buzzerPin);
ledHypoState = LOW;
tone(buzzerPin, fHypo, dfHypo);
//Serial.print("Hypo state: Low\n");
//Serial.println(float(analogRead(tempSensor)*celcius));
digitalWrite(ledHypoPin, ledHypoState);
/**/
/**/
previousTime_2 = currentTime;
}
/**/
/* Hot */
/**/
if ((ledFeverState == LOW)&&(currentTime - previousTime_3 >= eventTime_fever_on)&&(temp >= feverTemp)) {
//for(int i = 0; i <=1; i++ ){
noTone(buzzerPin);
ledFeverState = HIGH;
tone(buzzerPin, nFever, dnFever);
//Serial.print("Fever state:High\n");
Serial.println(float(analogRead(tempSensor)*celcius));
digitalWrite(ledFeverPin, ledFeverState);
//}
/**/
/**/
previousTime_3 = currentTime;
}
/**/
/**/
else if ((ledFeverState == HIGH)&&(currentTime - previousTime_3 >= eventTime_fever_off)) {
ledFeverState = LOW;
noTone(buzzerPin);
tone(buzzerPin, fFever, dfFever);
//Serial.print("Fever state:Low\n");
//Serial.println(float(analogRead(tempSensor)*celcius));
digitalWrite(ledFeverPin, ledFeverState);
/**/
/**/
previousTime_3 = currentTime;
}
/**/
}
Apologies for the comments, it helps with commenting out codes to test the program.
What can I do to solve this problem? Are there any issues with my code? do I have to change something in my output? add something to my circuit?