Temperature and Vibration

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
}

Welcome

Please read this post Read this before posting a programming question and follow the instructions regarding posting code then post your best effort at doing what you want and describe the problems

The original post has been edited to include my attempt as per the instructions for posting : )

jmac01:
The original post has been edited to include my attempt as per the instructions for posting : )

Did you read the topic that I linked to ?

Why is there a smiley in your code I wonder ?

UKHeliBob:
Did you read the topic that I linked to ?

Why is there a smiley in your code I wonder ?

The actual post has my attempt and I've changed my question to be more specific to the problem (being that it is picking up sensor from both the temperature and vibration sensors)

Did you read about using code tags to prevent the code being interpreted as HTML commands ?

If so, then why did you not use them to prevent the smileys in the code ?

UKHeliBob:
Did you read about using code tags to prevent the code being interpreted as HTML commands ?

If so, then why did you not use them to prevent the smileys in the code ?

so sorry, so is this correct now? I'm genuinely sorry it wasn't done correctly the first time

Looks better now, but it is a shame that you did not Auto format it in the IDE. Perhaps next time you post code.

Do you see how much easier it is to read and copy when using code tags ? Making it easier means that you are more likely to get help

So what happens when you run the program ?

UKHeliBob:
Looks better now, but it is a shame that you did not Auto format it in the IDE. Perhaps next time you post code.

Do you see how much easier it is to read and copy when using code tags ? Making it easier means that you are more likely to get help

So what happens when you run the program ?

At the moment it makes the buzzer continuously buzz with no regard to the vibration sensor. The heat sensor is working though, so I assume that every time the heat sensor updates the buzzer is detecting that a the vibration sensor.
Also, I'm not sure if this helps but both codes that I'm splicing together want to use the Gnd port

This is not what you want:

    buzzerSensorPin = analogRead(buzzerSensorPin);

You read an analog pin, and store the result in the variable referencing the input pin.
After the first reading, the pin number is altered, and next time you will read an unknown pin.

Create a separate variable for storing the result of analogRead().

Hi,
Welcome to the forum.
Did you have the two codes working before you started to combine them?

Tom... :slight_smile:

leongjerland:
This is not what you want:

    buzzerSensorPin = analogRead(buzzerSensorPin);

You read an analog pin, and store the result in the variable referencing the input pin.
After the first reading, the pin number is altered, and next time you will read an unknown pin.

Create a separate variable for storing the result of analogRead().

Thanks so much for the reply! So I did some research into variables and I tried making an 'int buzzerPin = 0' to use as the new variable and then changed all the 'buzzerSensorPin' to the new variable, but now I'm getting an error message saying the serial port is busy. I'm not sure if that's a good sign that both sensors are being picked up or if I've just done this completely wrong?

now I’m getting an error message saying the serial port is busy.

The Serial interface uses pin 0. Choose another one and post your revised code here

Breakthrough! So I tried to change the sensor in my original buzzer code and then put everything together again and it seems to have worked, except for one problem. Both require the 5V port and whichever sensor is plugged in works. Is there any way to change it so one sensor uses a different port than the 5V?
see new 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);
}
}

leongjerland:
This is not what you want:

    buzzerSensorPin = analogRead(buzzerSensorPin);

You read an analog pin, and store the result in the variable referencing the input pin.
After the first reading, the pin number is altered, and next time you will read an unknown pin.

Create a separate variable for storing the result of analogRead().

Ignore my other response, I've managed to fix it. Thanks so much for your reply, I don't think I would've figured it out without it! I've put the finished code in the original post.
Thanks again!

  pinMode(2, OUTPUT); //blue
  pinMode(5, OUTPUT); //green
  pinMode(4, OUTPUT); //red
      digitalWrite(2, LOW);
      digitalWrite(5, LOW);
      digitalWrite(4, HIGH);

Just think how much easier to understand the code would be if you gave those pins meaningful names instead of relying on comments