UPDATE
Fixed it! thanks to everyone who replied, couldn’t have done it without this community!
Fixed code:
const int buzzer = 8; //Buzzer connected to pin 8 of Arduino uno / mega
int sensor1; //Variable to store analog value (0-1023)
const int hot = -1; //set hot parameter
const int cold = -5; //set cold parameter
void setup() {
{
Serial.begin(9600); //Only for debugging
pinMode(buzzer, OUTPUT);
}
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(5, OUTPUT); //green
pinMode(4, OUTPUT); //red
Serial.begin(9600);
}
void loop() {
{
sensor1 = analogRead(A0);
//While sensor is not moving, analog pin receive 1023~1024 value
if (sensor1<1022){
tone(buzzer, 500);
Serial.print("Sensor Value: ");
Serial.println(sensor1);
}
else{
noTone(buzzer);
Serial.print("Sensor Value: ");
Serial.println(sensor1);
}
int sensor = analogRead(A2);
float voltage = (sensor / 1024.0) * 5.0;
float tempC = (voltage - .5) * 100;
float tempF = (tempC * 1.8) + 32;
Serial.print("temp: ");
Serial.print(tempF);
if (tempF < cold) { //cold
digitalWrite(2, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (tempF >= hot) { //hot
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
Serial.println(" It's Fine.");
}
delay(10);
}
}
Hi!
I’m new to using Arduino and I’m trying to get a temperature sensor to change three LED’s, a vibration sensor to make a buzzer buzz when it detects movement and for a motor to start when the vibration sensor detects movement for longer than 3 seconds. So far I’ve been trying to combine two codes I’ve found online to do the heat sensor and the buzzer parts but after two days I still can’t do this for the life of me. If anyone knows how to do all of this or just wants to help me with the code splicing I’d really appreciate the help.
I’ve made my own attempt but I think the problem is with the sensors, which is why I’ve tried to change the sensor for the buzzer from sensor to buzzerSensorPin.
MY CODE
const int buzzer = 8; //Buzzer connected to pin 8 of Arduino uno / mega
int buzzerSensorPin = A0; //Variable to store analog value (0-1023)
const int hot = -1; //set hot parameter
const int cold = -5; //set cold parameter
void setup() {
{
Serial.begin(9600); //Only for debugging
pinMode(buzzer, OUTPUT);
}
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(5, OUTPUT); //green
pinMode(4, OUTPUT); //red
Serial.begin(9600);
}
void loop() {
{
buzzerSensorPin = analogRead(buzzerSensorPin);
//While sensor is not moving, analog pin receive 1023~1024 value
if (buzzerSensorPin<1022){
tone(buzzer, 500);
Serial.print("Sensor Value: ");
Serial.println(buzzerSensorPin);
}
else{
noTone(buzzer);
Serial.print("Sensor Value: ");
Serial.println(buzzerSensorPin);
}
int sensor = analogRead(A2);
float voltage = (sensor / 1024.0) * 5.0;
float tempC = (voltage - .5) * 100;
float tempF = (tempC * 1.8) + 32;
Serial.print("temp: ");
Serial.print(tempF);
if (tempF < cold) { //cold
digitalWrite(2, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (tempF >= hot) { //hot
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
Serial.println(" It's Fine.");
}
delay(10);
}
}
P.S. I’m using a tilt switch sensor, a standard buzzer and an LM35 temperature sensor and only have 220R, 10K and 4.7K resistors.
ORIGINAL CODES
TEMPERATURE CODE
const int hot = 87; //set hot parameter
const int cold = 75; //set cold parameter
void setup() {
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(3, OUTPUT); //green
pinMode(4, OUTPUT); //red
Serial.begin(9600);
}
void loop() {
int sensor = analogRead(A2);
float voltage = (sensor / 1024.0) * 5.0;
float tempC = (voltage - .5) * 100;
float tempF = (tempC * 1.8) + 32;
Serial.print("temp: ");
Serial.print(tempF);
if (tempF < cold) { //cold
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (tempF >= hot) { //hot
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
Serial.println(" It's Fine.");
}
delay(10);
}
BUZZER CODE
const int buzzer = 8; //Buzzer connected to pin 8 of Arduino uno / mega
int sensor; //Variable to store analog value (0-1023)
void setup()
{
Serial.begin(9600); //Only for debugging
pinMode(buzzer, OUTPUT);
}
void loop()
{
sensor = analogRead(A0);
//While sensor is not moving, analog pin receive 1023~1024 value
if (sensor<1022){
tone(buzzer, 500);
Serial.print("Sensor Value: ");
Serial.println(sensor);
}
else{
noTone(buzzer);
Serial.print("Sensor Value: ");
Serial.println(sensor);
}
delay(100); //Small delay
}