How to use ultrasonic sensor and activate LEDs through it?

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;
}

duration and inches are declared where?
Edit: missed them, they're local. Good.

You're missing a ton of }, and pinMode are typically in setup.
for loop syntax needs some revision.
Syntax generally needs some revision.
delay is void, so redeclaring it as int does you no favours

Why are the pinmodes being done in loop()?

What does the OP expect this to do:

long microsecondsToInches(long microseconds) 
   return microseconds / 74 / 2;

?

Why keep the error messages a secret?

I have no idea, ill change it

sorry I forget to add those: "error: error: expected ')' before 'int'

a function-definition is not allowed here before 'return'

a function-definition is not allowed here before 'return'

Compilation error: 'microsecondsToInches' was not declared in this scope

ino: In function 'void loop()':

error: 'microsecondsToInches' was not declared in this scope

note: suggested alternative: 'microsecondsToClockCycles'

error: expected ';' before ')' token

error: expected primary-expression before 'int'

error: expected ')' before 'int'"

Most of these are repeated many times.

I am trying to use delay in a if statement/bool but don't know how

Do you think that this error message

is related to this piece of code

return microseconds / 74 / 2;

?

For every ( there must be a ).

Where in your code is microsecondsToInches declared?

To be completely honest I have no idea; I copied some of the code and the other half I made myself. That part of the code is just to get the sensor working but I have no specific detail on what it actually does by itself.
I also don't know how to "declare" it.

I know this code is a mess as I started today. I am attempting to make a rail road crossing where the sensor will pick up the train and will make the LEDS flash back and forth. I have many errors and don't know how to fix them.

Any help is much appreciated, 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
   pinMode(REDLED1, OUTPUT);
   pinMode(REDLED2, OUTPUT);
   pinMode(pingPin, OUTPUT);
}

void loop() {
   long duration, inches, cm;
  
   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;
}

Error Messages:

error: expected ')' before 'int'

a function-definition is not allowed here before 'return'

a function-definition is not allowed here before 'return'

Compilation error: 'microsecondsToInches' was not declared in this scope

ino: In function 'void loop()':

error: 'microsecondsToInches' was not declared in this scope

note: suggested alternative: 'microsecondsToClockCycles'

error: expected ';' before ')' token

error: expected primary-expression before 'int'

error: expected ')' before 'int'

These are all repeated many times.

It looks like you are just making up code syntax.

That doesn't work well, so we recommend to start with the simple examples that come with the Arduino IDE, like how to blink LEDs, and learn the programming language in small steps.

File > Examples > (built in examples)

What does code syntax mean?

I'm a high school student with a project to do this. My teacher is not the most helpful guy and I don't have too much time. If you can give pointers or anything at all on what to change I'd be very thankful.

Your code must obey the language requirements for syntax: What is syntax in a programming language?

Pointers: start by studying the Arduino examples, look up C/C++ programming language tutorials on the web and study a few.

I have merged your cross-posts @notaclue52.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Sorry about that, didn't know but now I do!

1 Like

Will do, thanks.

This is where I would start:

My guess here is you are using a HC-SR04 ultrasonic sensor. Since you only care if the train is there or not there an ultrasonic sensor to measure distance really is not needed. Will it work? Yes, but not the best choice since it is a yes or no as in train present or train not present. You may want to Google "reflective optical sensor Arduino" which will give you some hits. That said using your sensor you have start simple as was suggested. Just get a basic chunk of code working and look at what works, follow the flow of the code and try to understand what is going on. Only once you have the basics working then move on. If the train is within a pre determined distance, do something. Else don't do something. Things take time and there is a learning curve.

Ron

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.