Help with something fairly basic

Hey everyone, I’ve used this: https://create.arduino.cc/projecthub/Salmanfarisvp/ultrasonic-range-detector-with-arduino-46c96c
Article as a base for getting started with the ultrasonic sensor.

My goal:

Make the led turn on if the sensor is reading back a range of less than a set value, for example, 50cm.

My physical setup:
Small LED connected to pin 3, with the ultrasonic sensor echo on pin 2, and trigger pin on 15.

I’d really like to learn more about the Arduino, but I’m 16 and have little money, so I’m learning part by part, along with the code and library’s that go with them.

So, I’m not sure how well I’ve done with my code, but here it is:

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  15  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int LED = 3; // LED on pin 3
char sr = Serial.read();

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(LED, OUTPUT); //set pin 3 as output (LED)
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("");
  if (sr < 50)
  {
    digitalWrite(LED, HIGH);
  }
  else (sr > 50);
  {
    digitalWrite(LED, LOW);
  }
}

Now, when I use this code, the LED turns on no matter what. And the console read works fine.

Thanks everyone!

EDIT: Problem solved. Project complete. Thanks everyone.

For anyone interested, here is my complete working code. This will trigger the LED connected to pin 3 to turn on if the sensor is reading farther than 12 inches.

#include <NewPing.h>

#define TRIGGER_PIN  15  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int LED = 3; // LED on pin 3
char s = Serial.read();

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(LED, OUTPUT); //set pin 3 as output (LED)
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping_in(); // Send ping, get ping time in microseconds (uS).
  Serial.println(uS);
  if (uS >= 12)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}

You need to have a corresponding digitalWrite ( LED, LOW ) to turn the led off. Look at the “else” statement (part of “if the else” ).

Actually, I just added that, after looking at it after a quick break.

I added and changed the original post to make more sense, because the first one was slightly unrelated values.

  if (sr < 50)
  {
    digitalWrite(LED, HIGH);
  }
  else (sr > 50);
  {
    digitalWrite(LED, LOW);
  }
}

Now the LED just flickers rapidly, as if going from low to high very fast, however the value is over 100.

Well the semi colon there is not welcome... if (sr > 5)[color=red];[/color] and if I were you I would also check what sr really is versus what you want to do...

Hint what do you think this does as part of global variables?? char sr = Serial.read();

Side questions:

  • Do you have a current limiting resistor on your LED?
  • How does the LED go back off again?

tinkerist:
I added and changed the original post to make more sense,

Never do that again... when you change your original post all comments made after become useless for a new comer to the discussion. You are wasting then everyone’s time

J-M-L:
Well the semi colon there is not welcome… if (sr > 5)[color=red];[/color] and if I were you I would also check what sr really is versus what you want to do…

Hint what do you think this does as part of global variables??

char sr = Serial.read();

Side questions:

  • Do you have a current limiting resistor on your LED?
  • How does the LED go back off again?

Yes I have a resistor, LED goes off with the new code (post 2) , which I only realized I forgot after re-checking.

Yeah, I only edited the OP because there where no reply’s yet, otherwise I wouldn’t have done that.

Also, I realized I had my <>'s backwards, which I’ve fixed to:

 if (sr > 50)
  {
    digitalWrite(LED, HIGH);
  }
  else (sr < 50);
  {
    digitalWrite(LED, LOW);
  }

Also, I’ve change ‘sr’ to ‘s’.

I believe that char s = Serial.read(); calls Serial.read(), no?

Remove this from your “else” statement:

(sr < 50 );

Leaving only the else

No results from doing that. LED is just off.

Post your complete amended sketch.

Ooops, I meant to include it with my previous post :confused:

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  15  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int LED = 3; // LED on pin 3
char s = Serial.read();

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(LED, OUTPUT); //set pin 3 as output (LED)
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("");
  if (s > 50)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}

Your LED follows s, which comes from a serial read.

aarg:
Your LED follows s, which comes from a serial read.

If I reverse the digitalWrite’s the LED turns on, but not off, which makes me think that there is something wrong with my Serial.read, It outputs the distance in cm to serial, and it goes under 50, but still doesn’t work.

tinkerist:
If I reverse the digitalWrite's the LED turns on, but not off, which makes me think that there is something wrong with my Serial.read, It outputs the distance in cm to serial, and it goes under 50, but still doesn't work.

I'm confused. Do you not want the LED to turn on/off as the distance goes above/below 50?

aarg:
I'm confused. Do you not want the LED to turn on/off as the distance goes above/below 50?

I want the LED to turn on when it reads 50 or less. :slight_smile:

tinkerist:
I want the LED to turn on when it reads 50 or less. :slight_smile:

Then maybe you should test the reading from the sensor, not some character that you read in once. You also need to keep the result from the conversion from uS to cm. You now just print it and toss it in the bit bucket.

aarg:
Then maybe you should test the reading from the sensor, not some character that you read in once. You also need to keep the result from the conversion from uS to cm. You now just print it and toss it in the bit bucket.

How exactly can I adapt my code to read directly from the sensor with NewPing library? I'm pretty new at this still :confused:

tinkerist:
How exactly can I adapt my code to read directly from the sensor with NewPing library? I’m pretty new at this still :confused:

Look at the example sketches that come with the library. But you already read the value into the variable “uS”. That is another hint.

aarg:
Look at the example sketches that come with the library. But you already read the value into the variable “uS”. That is another hint.

Okay, I think I’ve got it reading from the sensor? Am I on the right track here? It’s still not functioning correctly though.

#include <NewPing.h>

#define TRIGGER_PIN  15  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int LED = 3; // LED on pin 3
char s = Serial.read();

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(LED, OUTPUT); //set pin 3 as output (LED)
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping_in(); // Send ping, get ping time in microseconds (uS).
  Serial.println(uS);
  if (uS > '12')
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}

LED is off no matter the reading shown in console, I’m pretty confused here, because I think I’m doing it right, but there’s obviously something I’m doing wrong.

Oh, Also I’ve changed from the default cm to in.

This simply isn't C.

  if (uS > '12')

It seems as if you're making a lot of guesses. Are you using any kind of language guide or tutorials? You need to.

aarg:
This simply isn’t C.

  if (uS > '12')

It seems as if you’re making a lot of guesses. Are you using any kind of language guide or tutorials? You need to.

Nope. Thanks for the help! I Figured it out.

I see what I did wrong there, https://www.arduino.cc/reference/en/language/structure/control-structure/else/

I’ll post my fixed code in the OP aswell as here:

#include <NewPing.h>

#define TRIGGER_PIN  15  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int LED = 3; // LED on pin 3
char s = Serial.read();

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(LED, OUTPUT); //set pin 3 as output (LED)
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping_in(); // Send ping, get ping time in microseconds (uS).
  Serial.println(uS);
  if (uS >= 12)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}