Commit f99b3352 authored by Brandon Fan's avatar Brandon Fan
Browse files

remove unnecessary main

parent 1c627606
const int trigPin1 = 13;
const int echoPin1 = 12;
const int trigPin2 = 9;
const int echoPin2 = 8;
const int trigPin3 = 5;
const int echoPin3 = 4;
const int led1 = 11;
const int led2 = 10;
const int led3 = 7;
long duration;
int distance1;
int distance2;
int distance3;
//no LEDs in this kit, just use readout in terminal for now
void setup() {
// put your setup code here, to run once:
//Set trig pins as Output and echo pins as Input
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
usSensor(trigPin1, echoPin1, 1);
usSensor(trigPin2, echoPin2, 2);
usSensor(trigPin3, echoPin3, 3);
if(distance1 < 30 && distance2 < 30) {
Serial.println("Large obstacle detected");
}
}
void usSensor(int trigPin, int echoPin, int sensorNum) {
int ledPin = 0;
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(100);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
int distance = duration * (0.034/2);
if (sensorNum == 1) {
distance1 = distance;
ledPin = led1;
}
else if (sensorNum == 2) {
distance2 = distance;
ledPin = led2;
}
else {
distance3 = distance;
ledPin = led3;
}
Serial.print("Sensor ");
Serial.print(sensorNum);
Serial.print(": ");
Serial.println(distance);
if (distance < 30) {
digitalWrite(ledPin, HIGH);
delay(distance * 15);
digitalWrite(ledPin, LOW);
//delay(distance * 15);
Serial.print("Obstacle detected: Sensor ");
Serial.println(sensorNum);
//delay(100);
}
else {
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(300);
}
}
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