counter question

Hello guys, I'm working on an arduino project containing 2 buttons with 3 lights. 1 button turns the led on in the following order: 1-2-3-1-2-3. Now i added an extra button. The purpose of that button is to switch the order of the klights(3-2-1-3-2-1). But the thing is when i code it like this and lets say the light is at 1 it goes to 0 and when i press it again it goes to -1. How do i change it to jump back to 3 if its at 0?

const int rood = 13;
const int groen = 11;
const int blauw = 12;
const int knop = 3;
const int Knop = 4;
int teller;

void setup()
{
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}

void loop()
{
int knopstatus;
knopstatus = digitalRead(knop);
int Knopstatus;
Knopstatus = digitalRead(Knop);

if(knopstatus == LOW)
{
teller++;
}
else if(teller == 0)
{
digitalWrite(rood, LOW);
digitalWrite(groen, LOW);
digitalWrite(blauw, LOW);
}
else if(teller == 1)
{
digitalWrite(rood, HIGH);
digitalWrite(groen, LOW);
digitalWrite(blauw, LOW);
}
else if(teller == 2)
{
digitalWrite(rood, LOW);
digitalWrite(groen, HIGH);
digitalWrite(blauw, LOW);
}
else if(teller == 3)
{
digitalWrite(rood, LOW);
digitalWrite(groen, LOW);
digitalWrite(blauw, HIGH);
}
else
{
teller = 0;
}
if(Knopstatus == LOW)
{
teller--;
}

}

After

teller--;

just add a test

if (teller == -1) teller =3;

Not to do with your problem, but more of a warning: having two buttons Knop with a capital-K and knop with a small-k is asking for trouble.

One day you'll type Knop when you mean knop or knop when you mean Knop and spend the rest of the day wondering why something's not working.

Since they (in this case) determine the leds' direction, how about something which has meaning, like knop_af and knop_op maybe.

This is the voice of wisdom !!!

lesept:
This is the voice of wisdom !!!

Yeah... got that t-shirt....

teller changes +1 every time that the non-blocked void loop() runs.

Your code will run each step and then the next, etc for all 3 leds about 5 times a millisecond and you won't see anything.

To make the buttons always work even while the leds sequence =on time=, you need to use millis timing.

Here is how I would code this, using INPUT_PULLUP mode for the buttons :

const int rood  = 13;
const int groen = 11;
const int blauw = 12;
const int knop_up   = 3;
const int knop_down = 4;
int teller;

void setup()
{
  pinMode(knop_up,   INPUT_PULLUP);
  pinMode(knop_down, INPUT_PULLUP);
  pinMode(rood,  OUTPUT);
  pinMode(blauw, OUTPUT);
  pinMode(groen, OUTPUT);
}

void loop()
{
  bool up   = digitalRead(knop_up);
  bool down = digitalRead(knop_down);
  delay(20); // debounce

  if (up == LOW) {
    teller++;
    if (teller > 3) teller = 0;
  }
  if (down == LOW) {
    teller--;
    if (teller < 0) teller = 3;
  }

  switch (teller) {
    case 0:
      digitalWrite(rood,  LOW);
      digitalWrite(groen, LOW);
      digitalWrite(blauw, LOW);
      break;
    case 1:
      digitalWrite(rood,  HIGH);
      digitalWrite(groen, LOW);
      digitalWrite(blauw, LOW);
      break;
    case 2:
      digitalWrite(rood,  LOW);
      digitalWrite(groen, HIGH);
      digitalWrite(blauw, LOW);
      break;
    case 3:
      digitalWrite(rood,  LOW);
      digitalWrite(groen, LOW);
      digitalWrite(blauw, HIGH);
      break;
  }
}

The buttons should be connected like this: GND -- button -- pin
Hope this helps...

Thank you guys very much for the replies. the programm works now, and thanks for the advise. I'll keep the naming of variables in mind (changed it know to make it easier to understand also for me :])