Commit e4548f47 authored by ssshroff's avatar ssshroff
Browse files

Most recent working file with correct pins

parent 0f34802f
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_right, OUTPUT);
pinMode(echo_pin_right, INPUT);
Serial.begin(9600); // Starts the serial communication
}
long calculate_distance(long duration) {
return duration / 58.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();
}
long average(long a, long b) {
return (a + b) / 2;
}
bool check_against(long a, long b, long comp) {
long avg = average(a, b);
return abs(avg - comp) > 30;
}
void loop() {
long data[3];
read_inputs(data);
print_sensors(data);
bool check_right = check_against(data[0], data[1], data[2]);
bool check_left = check_against(data[1], data[2], data[0]);
bool check_mid = check_against(data[0], data[2], data[1]);
if (check_right || check_left || check_mid) {
Serial.println("There seems to be an object in the way...");
}
}
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