Commit 2cb243e9 authored by Brandon Fan's avatar Brandon Fan 💬
Browse files

add main_working

parent b82fe415
const int trigPin1 = 9;
const int echoPin1 = 8;
const int trigPin2 = 6;
const int echoPin2= 7;
long duration1;
long duration2;
int distance1;
int distance2;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.034/2;
Serial.print("Sensor 1:");
Serial.println(distance1);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2 * 0.034/2;
Serial.print("Sensor 2:");
Serial.println(distance2);
}
const int trig_pin_left = 9;
const int echo_pin_left = 8;
const int trig_pin_mid = 7;
const int echo_pin_mid= 6;
const int trig_pin_right = 5;
const int echo_pin_right = 4;
void setup() {
// put your setup code here, to run once:
// add reading of specific pins
pinMode(trig_pin_left, OUTPUT);
pinMode(echo_pin_left, INPUT);
pinMode(trig_pin_mid, OUTPUT);
pinMode(echo_pin_mid, INPUT);
pinMode(trig_pin_left, INPUT);
pinMode(echo_pin_left, INPUT);
Serial.begin(9600); // Starts the serial communication
}
long calculate_distance(long duration) {
return duration * 0.034 / 2;
}
long read_pin(int trig_pin, int echo_pin, int start_microseconds, int end_microseconds) {
digitalWrite(trig_pin, LOW);
delayMicroseconds(start_microseconds);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(end_microseconds);
digitalWrite(trig_pin, LOW);
return pulseIn(echo_pin, HIGH);
}
void read_inputs(long * datapoints) {
long duration_left = read_pin(trig_pin_left, echo_pin_left, 2, 10);
datapoints[0] = calculate_distance(duration_left);
long duration_mid = read_pin(trig_pin_mid, echo_pin_mid, 2, 10);
datapoints[1] = calculate_distance(duration_mid);
long duration_right = read_pin(trig_pin_right, echo_pin_right, 2, 10);
datapoints[2] = calculate_distance(duration_right);
}
void print_sensors(long * data) {
Serial.print("Sensor Data: ");
Serial.print(data[0]);
Serial.print(" ");
Serial.print(data[1]);
Serial.print(" ");
Serial.print(data[2]);
Serial.println();
}
void loop() {
long data[3];
read_inputs(data);
print_sensors(data);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment