const int trigPin = 6;
const int echoPin = 7;
const int btrigPin = 10;
const int bechoPin = 11;
long duration;
int distance;
long bduration;
int bdistance;
char Incoming_value = 0;
char Incoming_value1 = 0;
char Incoming_value2 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
pinMode(btrigPin, OUTPUT);
digitalWrite(btrigPin, LOW);
pinMode(bechoPin, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
digitalWrite(btrigPin, LOW);
delayMicroseconds(2);
digitalWrite(btrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(btrigPin, LOW);
bduration= pulseIn(bechoPin, HIGH);
bdistance= bduration*0.034/2;
Serial.println("distance: ");
Serial.print(distance);
Serial.print("|");
Serial.println("bdistance: ");
Serial.print(bdistance);
delay(300);
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
if(Incoming_value == '1')
digitalWrite(3, HIGH);
else if(Incoming_value == '0')
digitalWrite(3, LOW);
}
if(Serial.available() > 0){
Incoming_value1 = Serial.read();
Serial.print(Incoming_value1);
Serial.print("\n");
if(Incoming_value1 == '2')
digitalWrite(4, HIGH);
else if(Incoming_value1 == '3')
digitalWrite(4, LOW);
}
if(Serial.available() > 0){
Incoming_value2 = Serial.read();
Serial.print(Incoming_value2);
Serial.print("\n");
if(Incoming_value2 == '4')
analogWrite(5, 255);
else if(Incoming_value2 == '5')
analogWrite(5, 0);
}
}
this is my code for control 3 leds et measure 2 distance with HCSR04 and module bluetooth