I just started using arduino. I am using 2 sensors (a ping and a PIR) and can get them to work separately but how can I combine the sketchs to use the sensors at the same time on my arduino uno? Thank you for your guidance in advance!
PING SKETCH;
//pin which triggers ultrasonic sound
const int pingPin = 13;
//pin which delivers time to receive echo using pulseIn()
const int maxD = 100;
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers red LED
int safeZone = 50;
//LED pin numbers
int greenLed = 3, redLed = 2;
void setup() {
// initialize serial communication
Serial.begin(9600);
}
void loop()
{
//raw duration in milliseconds, cm is the
//converted amount into a distance
long duration, cm;
//initializing the pin states
pinMode(pingPin, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
//printing the current readings to ther serial display
Serial.print(cm);
Serial.print("cm");
Serial.println();
//checking if anything is within the safezone, if not, keep
//green LED on if safezone violated, activate red LED instead
if (cm > safeZone)
{
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
} else {
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
delay(100);
}
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;
}
PIR Sketch
//Connect PIR sensor to pin 9
//Connect what ever you want to go high to pin 12
int val = 0;//sets value variable to 0
void setup() {
pinMode (12,OUTPUT);//pin 12 is the led pin
pinMode (9, INPUT);//pin 9 reads the high signal from the PIR
}
void loop () {
val = digitalRead(9);//Pin 9 is initiallly 0
digitalWrite(12,val);//pin thirteen is equal to whatever pin 9 is
if (val == 1
);//writes thirteen high if pir is high
digitalWrite(12,LOW);//led turns off
}
how can I combine the sketchs to use the sensors at the same time on my arduino uno?
Copy and paste the code that does stuff you want from one sketch into a new sketch. Then copy and paste code from the other sketch that does stuff you want into the new sketch.
Since you have not defined what the combined sketch should do, you'll have to create the new one on your own.
I was able to combine them (setup and loop)...however my only problem is that the led output (D12 for the PIR LED) for is not turning on? do I have to define that in the beginning? everything else works fine....this is what I did;
//pin which triggers ultrasonic sound
const int pingPin = 13;
//pin which delivers time to receive echo using pulseIn()
const int maxD = 100;
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers red LED
const int pirLed = 12;
const int pirPin = 9;
int safeZone = 50;
//LED pin numbers
int greenLed = 3, redLed = 2;
int val = 0;
void setup() {
// initialize serial communication
Serial.begin(9600);
pinMode (pirLed, OUTPUT);//pin 12 is the led pin
pinMode (pirPin, INPUT);//pin 9 reads the high signal from the PIR
}
void loop()
{
//raw duration in milliseconds, cm is the
//converted amount into a distance
long duration, cm;
//initializing the pin states
pinMode(pingPin, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
//printing the current readings to ther serial display
Serial.print(cm);
Serial.print("cm");
Serial.println();
//checking if anything is within the safezone, if not, keep
//green LED on if safezone violated, activate red LED instead
if (cm > safeZone)
{
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
} else {
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
delay(100);
}
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;
val = digitalRead(pirPin);//Pin 9 is initiallly 0
digitalWrite(pirLed,val);//pin 12 is equal to whatever pin 9 is
if (val == 1); //writes 12 high if pir is high
digitalWrite(pirLed,LOW);//led turns off
}
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;
val = digitalRead(pirPin);//Pin 9 is initiallly 0
digitalWrite(pirLed,val);//pin 12 is equal to whatever pin 9 is
if (val == 1); //writes 12 high if pir is high
digitalWrite(pirLed,LOW);//led turns off
}
Return means "Go away. We're done here.". Nothing after the return statement ever gets executed.