I already know this code is a mess but I am new to this as of today. What I'm attempting to do is to use the ultrasonic distance sensor and a pair of LEDS. The function of this is for a train stop so when the train gets close enough the sensor will pick up and make the LEDS flash back and forth.
I have too many error messages to count; if somebody could help that'd be great!
Thanks.
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 8; // Echo Pin of Ultrasonic Sensor
const int REDLED1 = 12;
const int REDLED2 = 13;
void setup() {
Serial.begin(9600); // Starting Serial Terminal
}
void loop() {
long duration, inches, cm;
pinMode(REDLED1, OUTPUT);
pinMode(REDLED2, OUTPUT);
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if(cm > 25)
for(int digitalRead(REDLED1,HIGH));
(int delay(500));
(int digitalRead(REDLED1,LOW));
(int digitalRead(REDLED2,HIGH));
(int delay(500));
(int digitalRead(REDLED2,LOW));
long microsecondsToInches(long microseconds)
return microseconds / 74 / 2;
long microsecondsToCentimeters(long microseconds)
return microseconds / 29 / 2;
}