Hello,
I'm relatively new to Arduino and I'm trying to use 2 sensors on one board.
Right now I am getting no error in the console yet my serial monitor is not returning any value.
I'm basically trying to splice 2 separate sketches that worked independently. Is it a problem with the structure of my sketch?
Also I'm wanting to be able to access the values of each sensor, how can I return the value to use with other functions?
My sensors
1 - HC-SR04 "classic"
2 - HC-SR501
My code
//this is the head program containing sections for
//ping
//pir
//speaker
//led's
////import any libs needed
//#include <Wire.h>
//initial global variables
//pir variables
int pir_val = 0;
int pir_vcc = 6;
int pir_sig = 7;
int pir_gnd = 8;
int pirState = LOW; // we start, assuming no motion detected
//ping variables
int ping_val = 0;
int ping_vcc = 2;
int ping_trig = 3;
int ping_echo = 4;
int ping_gnd = 5;
//speaker
int speaker = 10;
void setup(){
Serial.begin(9600);
pir_setup();
ping_setup();
}
void loop(){
ping();
delay(10);
pir();
delay(10);
}
void ping_setup(){
pinMode (ping_trig, OUTPUT);//attach pin 2 to vcc
pinMode (ping_echo,INPUT);//attach pin 5 to GND
}
void pir_setup(){
pinMode(pir_vcc, OUTPUT);
pinMode(pir_gnd, OUTPUT);
pinMode(pir_sig, INPUT); // declare sensor as input
}
void ping(){
digitalWrite(ping_vcc, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
// pinMode(ping_trig, OUTPUT);// attach pin 3 to Trig
digitalWrite(ping_trig, LOW);
delayMicroseconds(2);
digitalWrite(ping_trig, HIGH);
delayMicroseconds(5);
digitalWrite(ping_trig, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
// pinMode (ping_echo, INPUT);//attach pin 4 to Echo
duration = pulseIn(ping_echo, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
ping_val = cm;
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
//return cm;
digitalWrite(ping_vcc, LOW);
delay(25);
}
void pir(){
digitalWrite(pir_vcc, HIGH);
pir_val = digitalRead(pir_sig); // read input value
if (pir_val == HIGH) { // check if the input is HIGH
delay(15);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
delay(30);
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
digitalWrite(pir_vcc, LOW);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}