paralax range finder and blink without delay

Hi all,

i’m trying to learn how to incorporate the blink without delay function with a paralax range finder. Basically, what i’m hoping to achieve is as distance decreases from the range finder, the led blinks faster. This is the code that i’ve come up with so far but i’m having some issues. Thus far the primary issue is figuring out why my variable “ledState” is giving me the error," ‘ledState’ was not declare in this scope". I’m not sure where to declare it then to make sure it initilizes to my preferred LOW state and then updates not as a local variable but as a global variable.(at least that’s what I think I need). Can anyone educate me on why this declaration is out of scope or where I should look for how to declare it properly? Feel free to analyze the rest of the code and give any advice you think might help.


const int ledPin = 13;  //the number of the LED Pin(to be substit-
//tuted with the vibrator when moving to prototype.

int letState = LOW; //the initilized state of the LED
long previousMillis = 0; //will store the last time the LED was
//updated or blinked

long interval = 0;// the interval at which to blink (milliseconds)

class Ultrasonic
          Ultrasonic(int pin);
    void DistanceMeasure(void);
          long microsecondsToInches(void);
          int _pin;//pin number of the Arduino that is connected
      //with the SIG pin of Ultrasonic    
    long duration;// the Pulse time received;  

Ultrasonic::Ultrasonic(int pin)
     _pin = pin; 

//Begin the detection and get the pulse signal back:
void Ultrasonic::DistanceMeasure(void)
    pinMode(_pin, OUTPUT);
	digitalWrite(_pin, HIGH);
	duration = pulseIn(_pin,HIGH);

//The measured distance from the range 0 to 157 inches:
long Ultrasonic::microsecondsToInches(void)
      return duration/74/2;

Ultrasonic ultrasonic(7);

void setup(){
  //set up the digital pin as output:
    pinMode(ledPin, OUTPUT);
  //setup the 9600 monitor to track the distance measured:

void loop()
  //code for the ultrasonic to read distance:
    long RangeInInches;
    ultrasonic.DistanceMeasure();//gets the current signal time;
    RangeInInches = ultrasonic.microsecondsToInches();//converts
    //the time to inches
    //the output to 9600 of distance to obstacle:
    Serial.println("The distance to the obstacle in fron is: ");
    Serial.print(RangeInInches);//range to obstace 0-157 in
    Serial.println(" inch");
  //the if loop to blink the LED based on distance to obstacle:
  if(RangeInInches > 20){   
    interval = 5000;
      Serial.println("I'm more than 20 away");
  else if( RangeInInches > 10){
    interval = 2000;
  Serial.println("I'm less than 20 away but more than 10 away");
 else if(RangeInInches > 5 ){
    interval = 1000;
  Serial.println("I'm less than 10 away but more than 5 away");
  else if(RangeInInches > 3){
    interval = 500;
  Serial.println("I'm less than 5 away but more than 3 away");
  else  {
     interval = 100;
   Serial.println("I'm less than 3 away");

 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval){
   //this saves the last time the LED blinked:
   previousMillis = currentMillis;
   //this turns on the LED if it's off and the interval has passed
   //or off it it's on:
   if(ledState == LOW)
     ledState = HIGH;
     ledState = LOW;
     //set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
int letState = LOW;

"let" or led"?


and of came down to a spelling error.

Ugh...I read it four times and didn't catch that. Thank you Hackscribble, that did it for that error.

warmonger0: Feel free to analyze the rest of the code and give any advice you think might help.

All variables associated with millis() should be [u]unsigned[/u] longs. It makes them able to hold numbers twice as big!

  if(ledState == LOW)
     ledState = HIGH;
     ledState = LOW;

can be reduced to:ledState = !ledState;  //inversion