pause sketch

I'm using a Parallax u-sound sensor to sense distance. I have 6 LEDs that light in sequence as the object gets closer, then they flash once all are lit. After the LEDs are all lit, I have them flash a set number of times and then turn off. I don't want them to sit there and flash forever. I have all of that working. I want to continue to monitor the sensor though. So if the object moves away and comes back, the sequence will begin again. I have tried do-while, for-else, if, etc.

If it's not obvious, we're in noob territory here. Be gentle with me.

for-else

Maybe you need an if loop.

What you really need to do is post your code.

int duration;
int sensor = 7; //pin 7 will pulse to initiate measurement, then will be read to determine distance
int pin1 = 3;
int pin2 = 5;
int pin3 = 6;
int pin4 = 9;
int pin5 = 10;
int pin6 = 11;
int pulsewidth = 255;

void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

}

void loop()
{

pinMode(sensor, OUTPUT); //set pin 7 to an output
digitalWrite(sensor, LOW); //clear sensor pin
delayMicroseconds(2);
digitalWrite(sensor, HIGH); //ping sensor pin
delayMicroseconds(5);
digitalWrite(sensor, LOW); //stop ping
pinMode(sensor, INPUT); //change sensor pin to input
duration = pulseIn(sensor, HIGH); //read sensor duration
Serial.println (duration);

if (duration <= 2000) analogWrite(pin1, pulsewidth); //turn LEDs on if distance is small
if (duration <= 1600) analogWrite(pin2, pulsewidth);
if (duration <= 1100) analogWrite(pin3, pulsewidth);
if (duration <= 800) analogWrite(pin4, pulsewidth);
if (duration <= 500) analogWrite(pin5, pulsewidth);
if (duration <= 300) analogWrite(pin6, pulsewidth);
delay (100);
if (duration <= 300) analogWrite(pin1, LOW); //turn LEDs off for flash function
if (duration <= 300) analogWrite(pin2, LOW);
if (duration <= 300) analogWrite(pin3, LOW);
if (duration <= 300) analogWrite(pin4, LOW);
if (duration <= 300) analogWrite(pin5, LOW);
if (duration <= 300) analogWrite(pin6, LOW);
delay (100);
if (duration > 2000) analogWrite(pin1, LOW); //turn LEDs off if distance is large
if (duration > 2000) analogWrite(pin2, LOW);
if (duration > 2000) analogWrite(pin3, LOW);
if (duration > 2000) analogWrite(pin4, LOW);
if (duration > 2000) analogWrite(pin5, LOW);
if (duration > 2000) analogWrite(pin6, LOW);

delay (100);

}

:roll_eyes:

When posting your code, make sure to wrap it in [code ] tags by highlighting it and pressing the # mark.

while(testCondition) ; //Semicolon just means do nothing. This would work, too:
//while(testCondition) {}

testCondition would be anything that returns a TRUE or FALSE. It could be a digitalRead, like so:

while(digitalRead(4));

or a compare

while ( analogRead (2) < 531 ) ;

or anything else really. Make sure that you remember that if you do something like this:

while ( val < 531 ) ;

val won’t get updated and will either pass through immediately or never. This would work though:

while (val < 531) {
  val = analogRead(5);
}
    if (duration <= 300) analogWrite(pin1, LOW);  //turn LEDs off for flash function
    if (duration <= 300) analogWrite(pin2, LOW);
    if (duration <= 300) analogWrite(pin3, LOW);
    if (duration <= 300) analogWrite(pin4, LOW);
    if (duration <= 300) analogWrite(pin5, LOW);
    if (duration <= 300) analogWrite(pin6, LOW);

Why are you performing 6 evaluations of a value that can’t change between evaluations?

    if (duration <= 300)
    {
      analogWrite(pin1, LOW);  //turn LEDs off for flash function
      analogWrite(pin2, LOW);
      analogWrite(pin3, LOW);
      analogWrite(pin4, LOW);
      analogWrite(pin5, LOW);
      analogWrite(pin6, LOW);
    }

Of course, the use of arrays would shorten this.

You are using PWM pins to turn the LEDs ON or OFF. What a waste…

Thanks for the advice.

The reason I’m doing 6 evaluations of a value that can’t change is because I don’t know what I’m doing. Still learning…

I don't know what I'm doing.

Using PWM pins is covered in chapter 8. Using digitalWrite() to turn a pin on or off is covered in chapter 1. What chapter are you on?

chapter of what? :~