Function statement being called unintentionally

I’ve just started out with Arduino and I’m trying to work on a simple project with a Parallax Ping Sensor HC-SR04. I’m hoping to trip a couple LEDs into a flashing sequence when the sensor reads below ‘triggerDistance’. However, it just goes right into the flash sequence, seemingly without going through the if statement. I’ve got some Serial.print statements following the if statements with the function and none of them are being triggered, so it seems like it’s being called outside of the main loop.

Here’s how my sketch looks right now:

/* HC-SR04 Sensor
   https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
  
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* VCC connection of the sensor attached to +5V
	* GND connection of the sensor attached to ground
	* TRIG connection of the sensor attached to digital pin 2
	* ECHO connection of the sensor attached to digital pin 4
 
 
   Original code for Ping))) example was created by David A. Mellis
   Adapted for HC-SR04 by Tautvidas Sipavicius
 
   This example code is in the public domain.
 */
 
// input trigger distance in inches here
const int inchTrigDistance = 20;

// this identifies the trigger distance in cm
const int triggerDistance = (inchTrigDistance * 2.54);

// constant ints to determine assign pins to variables
const int trigPin = 2;
const int echoPin = 4;
const int yellowLed = 10;
const int redLed = 11;

// these are constant & identify which pins the LEDs are connected to
const byte yellowLED = 10;
const byte redLED = 11;

// timeframe for blinks in ms
const unsigned long yellowLEDinterval = 500;
const unsigned long redLEDinterval = 1000;

// variable that holds the current timer value
unsigned long yellowLEDtimer;
unsigned long redLEDtimer;





void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
   // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  pinMode(yellowLed, OUTPUT);
  digitalWrite(yellowLed, LOW);
  pinMode(redLed, OUTPUT);
  digitalWrite(redLed, LOW);
  
 
   if (cm < triggerDistance) 
   {
     Serial.print('Trigger Distance if statement met');
     // Handling the blink of one LED.
     if ( (millis () - yellowLEDtimer) >= yellowLEDinterval)
     {
     Serial.print('If Statement #2 condition met, LED toggling...');
     toggleYellowLED ();
     }
     // The other LED is controlled the same way. Repeat for more LEDs
     if ( (millis () - redLEDtimer) >= redLEDinterval)
    {
    // Red LED being toggled
      toggleRedLED ();
     }
   }
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
  
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}

void toggleYellowLED()
{
  if (digitalRead (yellowLED) == LOW)
  {
    digitalWrite (yellowLED, HIGH);
  }
  else
  {
    digitalWrite (yellowLED, LOW);
  }
    
  // remember when we toggled it
  
  yellowLEDtimer = millis();
}
// end of toggleGreenLED

void toggleRedLED()
{
   if (digitalRead (redLED) == LOW)
   {
      digitalWrite (redLED, HIGH);
   }
   else
   {
      digitalWrite (redLED, LOW);
   }
   
  // remember when we toggled it
  redLEDtimer = millis();
} 
// end of toggleRedLED

I appreciate any insight you can provide me, even if it’s just pointing me off to another post or a video. I’m just lost and out of ideas. I’ve got a few books that I’ve been thumbing through, but I’m just not quite sure what I need to read about.

One last thing - just to give credit where credit’s due, I used an article at Modified Arduino Ping))) example to work with 4-Pin HC-SR04 Ultrasonic Sensor Distance Measuring Module · GitHub for the flashing sequence in that sketch.

You have not assigned a value to cm before you apply your check, so it would contain junk. You need to read the distance before you apply your range check. This might explain why it's flashing when you don't expect it to.

I can't explain why your trace messages aren't appearing. However, these trace messages are the only output from your sketch so it may be that you just have the serial settings wrong. I suggest you add something like this to setup, after you have initialised the serial port:

Serial.println(__FILE__ " " __DATE__ " " __TIME__);

This will prove the serial output works, and also tell you what sketch is running.

     Serial.print('Trigger Distance if statement met');

Single quotes are for single characters. Please post a picture of your keyboard, circling the ONE key that you pressed to get the one character in the single quotes.

Peter - Good catch. I can't believe I didn't notice that as long as I looked at that code last night! That works as I'd expect it to now.

Paul - I spent a few minutes looking for that key, but I can't seem to locate it so I went ahead and put double quotes around the statement and it's working now.

It must have been calling on that cm variable that basically wasn't there, and initiating the sequence, and I never saw the trace messages because I had them entered incorrectly. That makes sense now.

I appreciate all the help!

I do have another question pertaining to this project if someone could maybe just get me pointed in the right direction; how do I tie this flash sequence into a timer without the loop being able to stop it? I had a timer initiated at one point, but I never quite figured it out right. The loop always shuts it off as soon as the target is pulled away from the trigger distance. Thanks again for the help!

cardinalphin: how do I tie this flash sequence into a timer without the loop being able to stop it?

Sorry, can't make sense of the question. Can you explain what behaviour you are trying to achieve?

If you put the "flash sequence" (whatever that means) in a function, and call that function, the function will execute to completion (or until interrupted, but then it will resume). Nothing else will stop that function from executing.

I appreciate your help gents. I simply meant I was trying to run the LEDs for a period of 30 seconds (or whatever I decide later). By "flash sequence", I was referring to all the code pertaining to making the LEDs flash, to include the actual functions and the code in the main loop.

Anyways, I believe I figured out what I screwed up. I THOUGHT the main loop was interrupting my function but I've seen the light now. I'm fairly certain I just wasn't properly forcing my previous function to loop until completion like I thought I was. Ha - I had no doubt I was messing it up; just didn't know where till now.

Thanks again!