Function statement being called unintentionally


i've started out arduino , i'm trying work on simple project parallax ping sensor hc-sr04. i'm hoping trip couple leds flashing sequence when sensor reads below 'triggerdistance'. however, goes right flash sequence, seemingly without going through if statement. i've got serial.print statements following if statements function , none of them being triggered, seems it's being called outside of main loop.

here's how sketch looks right now:
code: [select]

/* hc-sr04 sensor
  https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
 
  sketch reads hc-sr04 ultrasonic rangefinder , returns the
  distance closest object in range. this, sends pulse
  sensor initiate reading, listens pulse
  return.  the length of returning pulse proportional
  distance of object sensor.
   
  circuit:
* vcc connection of sensor attached +5v
* gnd connection of sensor attached ground
* trig connection of sensor attached digital pin 2
* echo connection of sensor attached digital pin 4


  original code ping))) example created david a. mellis
  adapted hc-sr04 tautvidas sipavicius

  example code in public domain.
*/

// input trigger distance in inches here
const int inchtrigdistance = 20;

// identifies trigger distance in cm
const int triggerdistance = (inchtrigdistance * 2.54);

// constant ints determine assign pins variables
const int trigpin = 2;
const int echopin = 4;
const int yellowled = 10;
const int redled = 11;

// these constant & identify pins leds connected to
const byte yellowled = 10;
const byte redled = 11;

// timeframe blinks in ms
const unsigned long yellowledinterval = 500;
const unsigned long redledinterval = 1000;

// variable holds current timer value
unsigned long yellowledtimer;
unsigned long redledtimer;





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

void loop()
{
  // establish variables duration of ping,
 // , distance result in inches , centimeters:
 long duration, inches, cm;

 // sensor triggered high pulse of 10 or more microseconds.
 // give short low pulse beforehand ensure clean high pulse:
 pinmode(trigpin, output);
 digitalwrite(trigpin, low);
 delaymicroseconds(2);
 digitalwrite(trigpin, high);
 delaymicroseconds(10);
 digitalwrite(trigpin, low);

 // read signal sensor: high pulse whose
 // duration time (in microseconds) sending
 // of ping reception of echo off of 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 blink of 1 led.
    if ( (millis () - yellowledtimer) >= yellowledinterval)
    {
    serial.print('if statement #2 condition met, led toggling...');
    toggleyellowled ();
    }
    // other led controlled same way. repeat more leds
    if ( (millis () - redledtimer) >= redledinterval)
   {
   // red led being toggled
     toggleredled ();
    }
  }

 // convert time 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 parallax's datasheet ping))), there are
 // 73.746 microseconds per inch (i.e. sound travels @ 1130 feet per
 // second).  this gives distance travelled ping, outbound
 // , return, divide 2 distance of obstacle.
 // see: http://www.parallax.com/dl/docs/prod/acc/28015-ping-v1.3.pdf
 return microseconds / 74 / 2;
}

long microsecondstocentimeters(long microseconds)
{
 // speed of sound 340 m/s or 29 microseconds per centimeter.
 // ping travels out , back, find distance of the
 // object take half of distance travelled.
 return microseconds / 29 / 2;
 
}

void toggleyellowled()
{
 if (digitalread (yellowled) == low)
 {
   digitalwrite (yellowled, high);
 }
 else
 {
   digitalwrite (yellowled, low);
 }
   
 // remember when toggled it
 
 yellowledtimer = millis();
}
// end of togglegreenled

void toggleredled()
{
  if (digitalread (redled) == low)
  {
     digitalwrite (redled, high);
  }
  else
  {
     digitalwrite (redled, low);
  }
 
 // remember when toggled it
 redledtimer = millis();
}
// end of toggleredled



i appreciate insight can provide me, if it's pointing me off post or video. i'm lost , out of ideas. i've got few books i've been thumbing through, i'm not quite sure need read about.

one last thing - give credit credit's due, used article @ https://gist.github.com/flakas/3294829 flashing sequence in sketch.

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

i can't explain why trace messages aren't appearing. however, these trace messages output sketch may have serial settings wrong. suggest add setup, after have initialised serial port:

code: [select]

serial.println(__file__ " " __date__ " " __time__);


this prove serial output works, , tell sketch running.


Arduino Forum > Using Arduino > Programming Questions > Function statement being called unintentionally


arduino

Comments

Popular posts from this blog

Authentication error while password reset - Raspberry Pi Forums

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums