The switch is A pulldown resistor,( if the switch is pushed the pin is high), I was working on more of the code. I add one more pin. (Outputpingpin 4) to resistor and led. But I don't know how to get the (if (distance > 10). How do I make this A High or low. So
my if (High or low, from distance > 10 && SwitchPin) digitalWrite (3,HIGH)
#define trigPin 12
#define echoPin 13
#define ledPin 3
#define switchPin 2
#define outputpingPin 4
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(outputpingPin, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
{
if (distance > 10)
digitalWrite (4,HIGH);
else
digitalWrite (4,LOW);
}
{
if (High or low, from distance > 10 && SwitchPin)
digitalWrite (3,HIGH)
else
digitalWrite (3,LOW);
}
delay(500);
}