While loop not allowing other things to function? (help me set up while loops)

Ok, so I have my code nearly completed, first off I am using the capsense library from here:Arduino Playground - HomePage

In it's current state, the code can measure up to four triggers (from the touch sensor I am using). For the first 3, one led will light up, then the next, and so forth. Once it reaches the forth, I have a while loop to flash the first led while the counter is still at four triggers, however if I trigger it again, the loop continues, and does not go to the next while loop.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
const int led1 = 0;               // setting the pins used for LEDs (physical pins 6,7, and 8.
const int led2 = 1;
const int led3 = 2;
int counter = 0;           // Counter for when the sensor is touched.


void setup()                    
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                    
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1; // Increasing the counter by one each time the sensor is touched.
    delay(1000);          // A delay so that if the sensor is held it does not increase the counter multiple times.
  }
  if (counter ==0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  } 
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }
  while (counter == 4) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
  }
  while (counter == 5) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    delay(1000);
  }
  while (counter >= 6) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    delay(1000);
  }
}

I tried making a second while loop that looks for the sensor being triggered, and to change the counter thinking that might work, but for some reason it now flashes the first two LEDs, and doesn't change when I trigger it again.

// Capacitive sensor device using attiny 85v, made by Jesse Levesque (XOIIO).

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
const int led1 = 0;               // setting the pins used for LEDs (physical pins 6,7, and 8.
const int led2 = 1;
const int led3 = 2;
int counter = 0;           // Counter for when the sensor is touched.


void setup()                    
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                    
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1; // Increasing the counter by one each time the sensor is touched.
    delay(1000);          // A delay so that if the sensor is held it does not increase the counter multiple times.
  }
  if (counter ==0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  } 
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }
  while (counter == 4) {
      if (total1 > 500) {
    counter = counter +1;
    delay(1000); 
      }
  }
  while (counter == 4) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
  }
  while (counter == 5) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    delay(1000);
  }
  while (counter >= 6) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    delay(1000);
  }
}

So, how to I set up these while loops properly?

Your first program will not exit the while (counter == 4) loop because there is nothing in the while loop to update the counter.
What do you want to happen when counter == 4 and the user touches the sensor ? With lower values you increment counter and move on. Should this happen when counter == 4 ?

UKHeliBob:
Your first program will not exit the while (counter == 4) loop because there is nothing in the while loop to update the counter.
What do you want to happen when counter == 4 and the user touches the sensor ? With lower values you increment counter and move on. Should this happen when counter == 4 ?

Yes, thats what I tried to do adding a second while loop for the fourth count, to update the counter but that did not work.

I also need the LEDs to blink, but then the counter would only check for an update every 2 seconds, I'd like it to be less, that's why I put it in a different while loop that ended up not working.

If you need to read the sensor more frequently than every 2 seconds whilst the LEDs blink then don't use delay() because it blocks any other code executing. Use millis() as a time keeper instead

set start time variable to millis()
set interval variable to 1000
start of loop
  if millis() - start time > interval
    change LED from on to off or vice versa.  You need to choose which LED based on the value of counter.  An array is ideal for this
    set start time to millis()
  end of if
  do other stuff like reading a sensor and acting on it
end of loop

The BlinkWithoutDelay example in the IDE has code to do this

UKHeliBob:
If you need to read the sensor more frequently than every 2 seconds whilst the LEDs blink then don't use delay() because it blocks any other code executing. Use millis() as a time keeper instead

set start time variable to millis()

set interval variable to 1000
start of loop
 if millis() - start time > interval
   change LED from on to off or vice versa.  You need to choose which LED based on the value of counter.  An array is ideal for this
   set start time to millis()
 end of if
 do other stuff like reading a sensor and acting on it
end of loop




The BlinkWithoutDelay example in the IDE has code to do this

Ok I see what you are getting at. I changed the code, when I get to the fourth one though, all leds turn off and nothing blinks. I tried not turning them off if the counter == 4, but then they all stayed on.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
const int led1 = 0;               // setting the pins used for LEDs (physical pins 6,7, and 8.
const int led2 = 1;
const int led3 = 2;
int counter = 0;                 // Counter for when the sensor is touched.
long previousMillis = 0;
long interval = 1000;
int led1State = HIGH;
unsigned long currentMillis = millis();


void setup()                    
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                    
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1; // Increasing the counter by one each time the sensor is touched.
    delay(1000);          // A delay so that if the sensor is held it does not increase the counter multiple times.
  }
  if (counter ==0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  } 
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }

  if (counter == 4) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    while (counter == 4) {
    if(currentMillis - previousMillis > interval) {
        if (led1State == LOW)
          led1State = HIGH;
        else
          led1State = LOW;
      }
    }
    if (total1 > 500) {
      counter = counter +1;
      delay(1000);
    }
  }
}

You've still got the same problem - you're not changing the value of "counter" so the loop can never exit.

AWOL:
You've still got the same problem - you're not changing the value of "counter" so the loop can never exit.

that's what this line, inside the bottom of the loop is for.

    if (total1 > 500) {
      counter = counter +1;
      delay(1000);

That condition is not inside the while loop.

AWOL:
That condition is not inside the while loop.

Ahh, right you are, corrected it. Now I need the darn led to blink.

// Capacitive sensor device using attiny 85v, made by Jesse Levesque (XOIIO).

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
const int led1 = 0;               // setting the pins used for LEDs (physical pins 6,7, and 8.
const int led2 = 1;
const int led3 = 2;
int counter = 0;                 // Counter for when the sensor is touched.
long previousMillis = 0;
long interval = 1000;
int led1State = HIGH;
 unsigned long currentMillis = millis();


void setup()                    
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                    
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1; // Increasing the counter by one each time the sensor is touched.
    delay(1000);          // A delay so that if the sensor is held it does not increase the counter multiple times.
  }
  if (counter <= 0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  } 
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }

  if (counter == 4) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    while (counter == 4) {
    if(currentMillis - previousMillis > interval) {
        if (led1State == LOW)
          led1State = HIGH;
        else
          led1State = LOW;
      }
    if (total1 > 500) {
      counter = counter +1;
      delay(1000);
    }
  }
}
}

Did you take note of this in my previous post ?

change LED from on to off or vice versa. You need to choose which LED based on the value of counter. An array is ideal for this

UKHeliBob:
Did you take note of this in my previous post ?

change LED from on to off or vice versa. You need to choose which LED based on the value of counter. An array is ideal for this

yes, I just tried modifying the script so that it doesn't use an ledstate variable, but it still is not working. Do you mean something different when you say choose an led based on the counter?

Anyways here's the modified script.

// Capacitive sensor device using attiny 85v, made by Jesse Levesque (XOIIO).

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
const int led1 = 0;               // setting the pins used for LEDs (physical pins 6,7, and 8.
const int led2 = 1;
const int led3 = 2;
int counter = 0;                 // Counter for when the sensor is touched.

long previousMillis = 0;
long interval = 1000;


void setup()                    
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                    
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1; // Increasing the counter by one each time the sensor is touched.
    delay(1000);          // A delay so that if the sensor is held it does not increase the counter multiple times.
  }
  if (counter <= 0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  } 
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }

  if (counter == 4) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    while (counter == 4) {
      if (total1 > 500) {
        counter = counter +1;
      }
      unsigned long currentMillis = millis();

      if(currentMillis - previousMillis > interval) {
        if (led1 == LOW)
          digitalWrite(led1, HIGH);
        else
          digitalWrite(led1, LOW);
      }
    }
  }
}

This is basically a cross-post of: http://arduino.cc/forum/index.php/topic,157855.0.html

Why are you doing this? Didn't the other thread work for you?

Because this was only dealing with getting the loop to work, as the rest of the script was working properly, however if I wanted to count above 3 I needed to get the loop working properly, and wanted to avoid confusion if people thought I was talking about something already resolved.

Anyways, I have also tried using a variable and changing the last section to this:

  if (counter == 4) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    while (counter == 4) {
      if (total1 > 500) {
        counter = counter +1;
      }
      unsigned long currentMillis = millis();

      if(currentMillis - previousMillis > interval) {
        if (ledState1 == LOW)
          ledState1 = HIGH;
        digitalWrite(led1, HIGH);
      } 
      else
      {        
        ledState1 = LOW;
        digitalWrite(led1, LOW);
      }
    }
  }
}

that did not work either though.

Why don't you step through your code (with a pencil and paper if necessary) and figure out what it is doing, rather than posting here with a new random code shuffle every ten minutes?

I've looked through it, and I can't tell why it's not turning on the led, I've reached a dead end.

That was eight minutes.

const int led1 = 0;               // setting the pins used for LEDs (physical pins 6,7, and 8.

...

        if (led1 == LOW)
          digitalWrite(led1, HIGH);
        else
          digitalWrite(led1, LOW);

led1 is a constant, with a value 0, which is the same as LOW.

So it will always be LOW and there isn't any point testing if it is or not.

You are just thrashing around here, starting multiple threads, posting code which has errors which you agree were previously identified.

Slow down, and look at what you are doing.

Those are to set pins 0,1, and 2 as the led pins, not to assign a value, I gathered that from the blink example, I didn't know that it was interfering. I'll redo the last part of the script and see if I figure it out, anyways here it is with every instance of led1, led2, led3 changed to the pin number instead.

// Capacitive sensor device using attiny 85v, made by Jesse Levesque (XOIIO).

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);

int counter = 0;            // Counter for when the sensor is touched.
int ledState1 = LOW;
long previousMillis = 0;
long interval = 1000;


void setup()                    
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (0, OUTPUT);
  pinMode (1, OUTPUT);
  pinMode (2, OUTPUT);
}


void loop()                    
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1; // Increasing the counter by one each time the sensor is touched.
    delay(1000);          // A delay so that if the sensor is held it does not increase the counter multiple times.
  }
  if (counter <= 0) {
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
  }
  if (counter == 1) {
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
  } 
  if (counter == 2) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
  }
  if (counter == 3) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
  }

  if (counter == 4) {
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    while (counter == 4) {
      if (total1 > 500) {
        counter = counter +1;
      }
      unsigned long currentMillis = millis();

      if(currentMillis - previousMillis > interval) {
        if (ledState1 == LOW)
          ledState1 = HIGH;
        digitalWrite(0, HIGH);
      } 
      else
      {        
        ledState1 = LOW;
        digitalWrite(0, LOW);
      }
    }
  }
}

What is likely to happen here?

 while (counter == 4) {
      if (total1 > 500) {
        counter = counter +1;
      }

(bearing in mind the value of "total1" that allowed you to reach this point)

AWOL:
What is likely to happen here?

 while (counter == 4) {

if (total1 > 500) {
       counter = counter +1;
     }



(bearing in mind the value of "total1" that allowed you to reach this point)

Yes, but it drops back down when the sensor is not being touched, The counter is the only thing that moves the script forward.