My ultrasonic sensor is using my conditions as its range

I'm having problems with my ultrasonic sensor. it apparently is using my if statement's conditions as its distance range. can any one help me I need to get this project done by Halloween. here's my code.

#include <NewPing.h>

#define TRIGGER_PIN A1 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A2 // 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.

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

#include <SPI.h>

//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>

//and the MP3 Shield Library
#include <SFEMP3Shield.h>

// Below is not needed if interrupt driven. Safe to remove if not using.
#include <TimerOne.h>
#elif defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer
#include <SimpleTimer.h>
SdFat sd;
SFEMP3Shield MP3player;

uint8_t result; //result code from some function as to be tested at later time.
long no_drop = 6;
long candy;

void setup() {
//Initialize the SdCard.
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if(!sd.chdir("/")) sd.errorHalt("sd.chdir");

//Initialize the MP3 Player Shield
result = MP3player.begin();
MP3player.setVolume(2, 2);

void loop() {
int track = random(1, 9);
candy = (sonar.ping_cm() / 2.54);
if(candy < 5 and candy > 3){//reads as range insted of checking if condison is true
Serial.print("0: ");
Serial.print(" < ");
Serial.print(" == ");
Serial.println(candy < no_drop);
//tell the MP3 Shield to play a track
//result = MP3player.playTrack(track);
while(MP3player.isPlaying() == true){

with this code its infinitely reading 4 instead of reading the actual distance.

do I need to replace it. if I do need to replace it what is a good alterative to detecting candy falling into a trick or treat bucket. I need answers to fix this problem I'm open to suggestions.

Can you elaborate? Also, what is the typical output for certain distances?

I would print candy before the if to see what is happening; currently you wil only see output if candy equals 4.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Oi! CODE TAGS, please?

Strange to be using analog pins for the task but possible. EXCEPT, where in your code are pins A0 and A2 are defined as digital outputs?

The NewPing library sets the pinMode()s in its constructor

That's the problem it somehow sets the men and max value of the sensor to the if statement condition. I can't figure it out.

Did the new ping example work for you?

Does the addition of the serial print provide you with any info that you can use to see why the if thingy not be doing so well?

Why are you dividing the ping return by 2.54 and storing the value into a long?

Of course the min max vaules are set to the if statement. It's what you wrote the program to do.

That limits the range of the processing of info to the number 4. It would be better if the statement was:
If( candy == 4 ) if you want to limit processing to only the number 4.

There's nothing in front of the sensor but yet it reads 4 because of the if statement forever (it should read 8 1/2 Inch but instead it reads 4 every millisecond). The if statement is (candy < 5 and candy > 3) and if I change it the min and the max of the sensor changes to (the code wants that if statement to be always true).

That will only ever print on 4. The whole number 4. That's what you programmed it to do.

Did the new ping example work for you? 2nd time asking that question.

There is no range of numbers to check. The variable candy is defined as a whole number. Only a whole number will fit into that variables location. You do not have a range of values. The only range is the whole number 4.

That variable as defined can only store a 1, or a 2, or a 3, or a 4, or a 5 or a 6. It cannot store 2.1, 3.1, 4.1, 5.1.

Your code limits the printing to ONLY when candy is 4.

I tried making it print before the if statement but it still changes the reader somehow.

good luck.

Do I need to buy a new one? I have had this sensor for a long time. Don't ask me when. I just know that I had it for a long time.

That was the last test of the sensor I've tried (candy < 8 and candy > 1), (candy < 5 and candy > 2) and (candy < 5 and candy > 3) and each time I changed the if statement the reading changes so that the if statement is always true no matter what I do.

Did you serial.print the value candy before the if statement?

What is the value of candy when it serial prints.

And what are you trying to accomplish?

And you understand that long candy will NEVER store a .5?

There is nothing wrong with that statement. You are asking that to print stuff when the values of candy are between 8 and 1.

What is wrong with that?

What are you trying to do?

What are you trying to do?

What are you trying to do?

What are you trying to do?

I've asked several times, why won't you print out candy to see what the actual values are?

Will you will answer some of the questions asked and you will try some of the suggestions given?

Do you understand that a long variable cannot hold a 8.5; except as a 85?

I've tried to do that ( print candy before if statement) and it changes as I change the if statement. I think that my ultrasonic sensor is broken.

Post that code in a new reply. And please use code tags this time.

what happens if you change this...

if(candy < 5 and candy > 3) this....

if(candy < 5 && candy > 3)

and, you should add


in loop, as in the basic NewPing example, so you don't ping too fast and get confusing echos.

Did you ever take the suggestion of running the ping example code? How'd that turn out?

So you are saying that a print of candy before the if statement causes candy to print out the values of the if statement?