Hello guys i'm starting programming and I need help for my project.
I need to know if something is not here and I press the button to turn on 1 led else the other led is turn on
atm I just know how to turn on 1 led with button and how to use sensor ultrasound.
void setup()
{
Serial.begin(9600); //Initialisation de la communication avec le moniteur série
pinMode(10,INPUT); //On indique à l’Arduino le mode du pin (entrée)
}
void loop()
{
boolean a=digitalRead(10);// et on l’affecte à la variable "a"
Serial.println(a); // on l'affiche sur le moniteur
}
and sensor ultrasound code
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}