Hello,
How do i combine 2 Arduino sketches for the supersonic sensor?
Theire are 2 senors that do the same thing only in diffrent places
1 sensor is left other sensor is 50 cm to the right
This is 1 sketch:
#define trigger 9
#define echo 8
#define LED 13
#define MOSFET 12
float time=0,distance=0;
void setup()
{
Serial.begin(9600);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
pinMode(LED,OUTPUT);
pinMode(MOSFET,OUTPUT);
delay(1000);
}
void loop()
{
measure_distance();
if(distance<5)
{
digitalWrite(LED,HIGH);digitalWrite(MOSFET,HIGH);
delay(10000);
digitalWrite(LED,LOW);digitalWrite(MOSFET,LOW);
delay(1000);
}
else
{
digitalWrite(LED,LOW);digitalWrite(MOSFET,LOW);
}
delay(500);
}
void measure_distance()
{
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
}
This is the second sketch:
#define trigger 7
#define echo 6
#define LED 13
#define MOSFET 12
float time=0,distance=0;
void setup()
{
Serial.begin(9600);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
pinMode(LED,OUTPUT);
pinMode(MOSFET,OUTPUT);
delay(1000);
}
void loop()
{
measure_distance();
if(distance<5)
{
digitalWrite(LED,HIGH);digitalWrite(MOSFET,HIGH);
delay(25000);
digitalWrite(LED,LOW);digitalWrite(MOSFET,LOW);
delay(1000);
}
else
{
digitalWrite(LED,LOW);digitalWrite(MOSFET,LOW);
}
delay(500);
}
void measure_distance()
{
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
}
Thanks in advance!