https://wokwi.com/projects/347478624767574611
const byte numSensors = 3;
//const mean constant variables, byte means small space in memmory, numSensors is my variabe name.
const byte ECHO_PIN[numSensors] = {2, 5, 8};
const byte TRIG_PIN[numSensors] = {3, 4, 7};
*/ We create an ARRAY to be used in a for loop/*
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
for (byte s=0; s< numSensors; s++) {
/*Due to foor loop we reduce pinMode instructions from 6 to only 2
pinMode(3, OUTPUT);
PinMode(2, INPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
/*
pinMode(TRIG_PIN[s], OUTPUT);
pinMode(ECHO_PIN[s], INPUT);
}
}
float readDistanceCM(byte s) {
/*This is a function wit a parameter (s) snd return a valuetaht dependson the duration of a pulseIn fucion and matehematical calculs with the velociy of sound*/
digitalWrite(TRIG_PIN[s], LOW);
/* We need to have the trigger pin controled to a Low value in order to measure the duration ofthe high value that is proporcional to he distance of the sensor. */
delayMicroseconds(2);
digitalWrite(TRIG_PIN[s], HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN[s], LOW);
int duration = pulseIn(ECHO_PIN[s], HIGH);
return duration * 0.034 / 2;
//Divided bay 2 because the ultrasound goes and returns back.
}
void loop() {
bool isNearby = false;
Serial.print("Measured distance: ");
for (byte s=0; s < numSensors; s++) {
float distance = readDistanceCM(s);
isNearby |= distance < 100;
Serial.print(distance);
Serial.print(" ");
}
Serial.println();
digitalWrite(LED_BUILTIN, isNearby);
delay(100);
}
delay(100);
}
//https://www.tinkercad.com/things/1DIoZ7eNnYq-super-borwo/editel?sharecode=z4ZpapaceZIsQmAQ1eV_SMaoCBl1y3wKd38v4AneO8o
int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int trigPin1 = 6;
int echoPin1 = 7;
int trigPin2 = 8;
int echoPin2 = 9;
int trigPin3 = 10;
int echoPin3 = 11;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
// Trigger: el emissor del ultraasound.
pinMode(echoPin1, INPUT);
//Echo: reecbeix l'informació que emiteix el trigg sobre la posició de l'objecte que detecta el ultrasound.
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin3, HIGH);
}
void firstsensor(){ // This function is for first sensor.
int duration1, distance1;
digitalWrite (trigPin1, HIGH);
delayMicroseconds (10);
//Temps que tarda en arribar la inormació de trigg a echo. ()
digitalWrite (trigPin1, LOW);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (duration1/2) / 29.1; //Velocidad del soniddo (29.1)
Serial.print("1st Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
if (distance1 < 50) { // Change the number for long or short distances.
digitalWrite (ledPin1, HIGH);
} else {
digitalWrite (ledPin1, LOW);
}
}
void secondsensor(){ // This function is for second sensor.
int duration2, distance2;
//I create two vaiables to olcal variable to hav an space in my microcontroler memory.
digitalWrite (trigPin2, HIGH);
delayMicroseconds (10); //Eimts ultrasound for 10 microsenconds.
digitalWrite (trigPin2, LOW); //Stop eistion of ultrasoound
duration2 = pulseIn (echoPin2, HIGH); //Activates echopin in high to reciebe a ultrasound backwards.
//duration2 keeps the value obtinde by echopin2
//It converte value of the ultrasound durations like a time of fly into a distance with using a mathematic formule.
distance2 = (duration2/2) / 29.1;
Serial.print("2nd Sensor: ");
Serial.print(distance2);
Serial.print("cm ");
if (distance2 < 50) { // Change the number for long or short distances.
digitalWrite (ledPin2, HIGH);
}
else {
digitalWrite (ledPin2, LOW);
}
}
void thirdsensor(){ // This function is for third sensor.
int duration3, distance3;
digitalWrite (trigPin3, HIGH);
delayMicroseconds (10); //Temps que tarda en arribar la infromació de trigg a echo en microsegons.
digitalWrite (trigPin3, LOW);
duration3 = pulseIn (echoPin3, HIGH);
distance3 = (duration3/2) / 29.1;
Serial.print("3rd Sensor: ");
Serial.print(distance3);
Serial.print("cm ");
if (distance3 < 50) { // Change the number for long or short distances.
digitalWrite (ledPin3, HIGH);
}
else {
digitalWrite (ledPin3, LOW);
//"if"i "else": son entendidos por el propio ordenador mediate su chip y conecta la biblioteca a el codigo.
}
}
void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
delay(100);
}