Up Down Counter.

Good morning,

I am currently embarking on a project to control a heater using 2 push buttons, 1 to increase the heat and 1 to decrease the heat. i have 5 levels of heat and the heater controller is expecting to see a PWM signal in. I have 5 LED's which i would like to turn on (in a bar graph style) to indicate the level at which the heater is operating.

When the unit is powered up i want nothing to happen (all Outputs Low and PWM at 0)

I have previosly created a project in which i switched audio sources using a push button and an internal counter either sending outputs high or low dependant on the value of the internal counter (using the if function) so i thought this would be a good starting point. The difference being this project counted up then when it reached the 4th output it returned to the first one after you press the button.

Mistakenly i thought it would be a case of just adding an aditional function and push button to decrease the counter rather than just using 1 push button to increase the counter then have it reset, unfortunately this does not appear to be the case.

Currently my hardware is as such.

INPUTS
UP push button connected to D2 with a pull down and switching to +5v
DOWN push button connected to D3 with a pull down and switching to +5v

OUTPUTS
1st LED Through a resistor on D4 (To indicate the LOWEST heat setting)
2nd LED Through a resistor on D5
3rd LED Through a resistor on D6
4th LED Through a resistor on D7
5th LED Through a resistor on D8 (To indicate the HIGHEST heat setting)
Input of my heater controller connected to D9 (PWM signal which controlls how hot the heater gets)

(I am aware i will probablyneed to use transistors to switch the LED's but just to simplify things for the time being i am not.)

Once the counter reaches 5 by pressing the UP button ideally i would like the counter to stop counting and just remain at 5 regardless of how many times the UP button is pressed until the DOWN button is pressed which will make the counter go down and similarly when the DOWN button is pressed i would like the counter to stop at 0 untill the UP button is pressed. - This is another thing i am strugling with as my previos project which this is based on just looped back arround hence the

{if(count > 4)
{count = 0;}

function came from in this project.

I have also put in some serial commands just so i can see what the counter is doing through the serial monitor however once the project is working i will probably remove these commands.

i have posted my code below as i just cant quite get my head round what i am doing wrong, i have been looking at this for days and it may be somethng simple but i just cant figure it out. any help would be gratefully received. If there is any further info you need please let me know. Hardware i am very competant but i am not too good with software/code as you may be able to tell below.

// Variables That will change:
int count = 0;  // counter for the number of button presses
int UpState = 0;         // current state of the up button
int UpButtonState = 0;     // previous state of the up button
int DownState = 0;        // current state of the down button
int DownButtonState = 0;  // previous state of the down button
void setup() {
  // initialize input/output:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  
 // initialize serial communication:
 Serial.begin(9600);
  
}

void loop()

//If The Count Value Reaches 4 Force The Count To Start Again
{if(count > 4)
{count = 0;}

       
 // read the up pushbutton input pin:
 UpState = digitalRead(2);

  // compare the buttonState to its previous state
  if (UpState != UpButtonState) {
  // if the current state is HIGH then the button went from off to on
  if (UpState == HIGH) {
  // if the state has changed, increment the counter up
  count++;

  Serial.print("Count:  ");
  Serial.println(count);
  }else {
  // if the current state is LOW then the button went from on to off:
    
  // save the current state as the last state, for next time through the loop
  UpButtonState = UpState;
  
  // read the down pushbutton input pin:
 DownState = digitalRead(3);

  // compare the buttonState to its previous state
  if (DownState != DownButtonState) {
  // if the current state is HIGH then the button went from off to on
  if (DownState == HIGH) {
  // if the state has changed, increment the counter down
  count--;

  Serial.print("Count:  ");
  Serial.println(count);
  }else {
  // if the current state is LOW then the button went from on to off:
   
  // save the current state as the last state, for next time through the loop
  DownButtonState = DownState;

  //if the counter is at 1
  if (count == 1){
    
    digitalWrite(4, HIGH);
    analogWrite(9, 51);
    
    //if the counter is at 2
   }else if (count == 2){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    analogWrite(9, 102);
   
    //if the counter is at 3
  } else if (count == 3){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    analogWrite(9, 153);
    
    //if the counter is at 4
   }else if (count == 4){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    analogWrite(9, 204);
    
     //if the counter is at 5
   }else if (count == 5){
    
     
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    analogWrite(9, 255);
    
  //if the counter is at 0 or has just been turned on default to
   }else{
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 0);
    
  
   }}}}}}

Thanks and best regards,

Phil.

A simple solution would be to move this:

if(count > 4)
{count = 0;}

Down in your code to after where you're adjusting count, i.e. after this

count--;

and change it to this:

if(count > 5)
  {
  count = 5;
  }
if(count < 0)
  {
  count = 0;
  }

Alternatively, take a look at the constrain function.

1st LED Through a resistor on D4 (To indicate the LOWEST heat setting)
2nd LED Through a resistor on D4
3rd LED Through a resistor on D4
4th LED Through a resistor on D4
5th LED Through a resistor on D4 (To indicate the HIGHEST heat setting)

D4 is working pretty hard, eh?

  //if the counter is at 1
  if (count == 1){
    
    digitalWrite(4, HIGH);
    analogWrite(9, 51);
    
    //if the counter is at 2
   }else if (count == 2){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    analogWrite(9, 102);
   
    //if the counter is at 3
  } else if (count == 3){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    analogWrite(9, 153);
    
    //if the counter is at 4
   }else if (count == 4){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    analogWrite(9, 204);
    
     //if the counter is at 5
   }else if (count == 5){
    
     
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    analogWrite(9, 255);
    
  //if the counter is at 0 or has just been turned on default to
   }else{
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 0);

Can't you turn this into a loop? Imagine if you had 20 LEDs!

Something like:

// turn them all off
for (int i = 0; i < 5; i++)
  digitalWrite (i + 4, LOW);

// turn the relevant ones on
for (int i = 0; i < count; i++)
  digitalWrite (i + 4, HIGH);

Whoops, coppied and pasted, should have been D4, D5, D6, D7, D8.

I have tried adding the lines

if(count > 5)
  {
  count = 5;
  }
if(count < 0)
  {
  count = 0;
  }

and the counter goes up to 6 so i presume an easy fix is to change the 5 to 4 to limit teh upper count value to 5? however it then stays at 6 and i still cant get it to count down. again. can anyone see something fundimantally wrong with the functions i have used to incrament teh counter with teh push buttons as i have looked at Nick's sugestion

// turn them all off
for (int i = 0; i < 5; i++)
  digitalWrite (i + 4, LOW);

// turn the relevant ones on
for (int i = 0; i < count; i++)
  digitalWrite (i + 4, HIGH);

and i am a little confused. This could be due to teh fact in my description of my circuit i labeled all outputs as D4.

Thanks,

Phil.

Please post your modified version of the sketch.

The modified version of my sketch is as below

// Variables That will change:
int count = 0;  // counter for the number of button presses
int UpState = 0;         // current state of the up button
int UpButtonState = 0;     // previous state of the up button
int DownState = 0;        // current state of the down button
int DownButtonState = 0;  // previous state of the down button
void setup() {
  // initialize input/output:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
 }

void loop()
{
  //Constrain the counter to go no lower than 0 and no higher than 5
  count =  constrain(count,0,5);

{// read the up pushbutton input pin:
 UpState = digitalRead(2);

  // compare the buttonState to its previous state
  if (UpState != UpButtonState) {
  // if the current state is HIGH then the button went from off to on
  if (UpState == HIGH) {
  // if the state has changed, increment the counter up
  count++;
  Serial.print("Count:  ");
  Serial.println(count);
  }
  // if the current state is LOW then the button went from on to off:
    
  // save the current state as the last state, for next time through the loop
  UpButtonState = UpState;}
 
 {// read the Down pushbutton input pin:
 DownState = digitalRead(3);

  // compare the buttonState to its previous state
  if (DownState != DownButtonState) {
  // if the current state is HIGH then the button went from off to on
  if (DownState == HIGH) {
  // if the state has changed, increment the counter Down
  count--;
  Serial.print("Count:  ");
  Serial.println(count);
  }
  // if the current state is LOW then the button went from on to off:
    
  // save the current state as the last state, for next time through the loop
  DownButtonState = DownState;
  
  //if the counter is at 1
  if (count == 1){
    
    digitalWrite(4, HIGH);
    analogWrite(9, 51);
    
    //if the counter is at 2
   }else if (count == 2){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    analogWrite(9, 102);
   
    //if the counter is at 3
  } else if (count == 3){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    analogWrite(9, 153);
    
    //if the counter is at 4
   }else if (count == 4){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    analogWrite(9, 204);
    
     //if the counter is at 5
   }else if (count == 5){
    
     
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    analogWrite(9, 255);
    
  //if the counter is at 0 or the unit has just been turned on default to
   }else{
  
     digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 0);
  }}}}}

So, this now counts up every time i press teh up button untill it reached 6 despite the constrain function but none of the LED'S illuminate! every time i press the down button, it counts down but alwyas misses 5 but continues to -1 which i am a bit confused about!? The LEDs all seem to illuminate once i press the down button but it is all of them! and they dont extinguish untill i reach 0 on the counter. again pressing teh up steps teh counter up but does not illuminate the leds?

Thanks,

Phil.

If you go down from five to four (or four to three, or...), I don't see where you turn off a LED.

  }}}}}

Don't you hate it when that happens?

So, this now counts up every time i press teh up button untill it reached 6 despite the constrain function

That's because you constrain it and then change it. Should be the other way round. Put it before both instances of this line:

  Serial.print("Count:  ");

I Have Modified My Code and incase it is of use or intrest to anyone i have posted it below:

// Variables That will change:
int count = 0;  // counter for the number of button presses
int UpState = 0;         // current state of the up button
int UpButtonState = 0;     // previous state of the up button
int DownState = 0;        // current state of the down button
int DownButtonState = 0;  // previous state of the down button
void setup() {
  // initialize input/output:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
 }

void loop()

{// read the Down pushbutton input pin:
 UpState = digitalRead(2);

  // compare the buttonState to its previous state
  if (UpState != UpButtonState) {
  // if the current state is HIGH then the button went from off to on
  if (UpState == HIGH) {
  // if the state has changed, increment the counter Down
  count++;
  //Constrain the counter to go no lower than 0 and no higher than 5
  count =  constrain(count,0,5);
  Serial.print("Count:  ");
  Serial.println(count);
  }
  // if the current state is LOW then the button went from on to off:
    
  // save the current state as the last state, for next time through the loop
  UpButtonState = UpState;
  
  //if the counter is at 1
  if (count == 1){
    
   digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 51);
    
    //if the counter is at 2
   }else if (count == 2){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 102);
   
    //if the counter is at 3
  } else if (count == 3){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 153);
    
    //if the counter is at 4
   }else if (count == 4){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    analogWrite(9, 204);
    
     //if the counter is at 5
   }else if (count == 5){
    
     
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    analogWrite(9, 255);
    
  //if the counter is at 0 or the unit has just been turned on default to
   }else{
  
     digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 0);
    
   } }else if  // read the Down pushbutton input pin:
 (DownState = digitalRead(3));

  // compare the buttonState to its previous state
  if (DownState != DownButtonState) {
  // if the current state is HIGH then the button went from off to on
  if (DownState == HIGH) {
  // if the state has changed, increment the counter Down
  count--;
  //Constrain the counter to go no lower than 0 and no higher than 5
  count =  constrain(count,0,5);
  Serial.print("Count:  ");
  Serial.println(count);
  }
  // if the current state is LOW then the button went from on to off:
    
  // save the current state as the last state, for next time through the loop
  DownButtonState = DownState;
  
  //if the counter is at 1
  if (count == 1){
    
   digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 51);
    
    //if the counter is at 2
   }else if (count == 2){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 102);
   
    //if the counter is at 3
  } else if (count == 3){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 153);
    
    //if the counter is at 4
   }else if (count == 4){
    
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    analogWrite(9, 204);
    
     //if the counter is at 5
   }else if (count == 5){
    
     
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    analogWrite(9, 255);
    
  //if the counter is at 0 or the unit has just been turned on default to
   }else{
  
     digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    analogWrite(9, 0);
  }}}

it does seem a bit long and drawn out but everything seems to work which is the important bit. Once again been saved by the forum. would just like to say thanks to you all for your help and input.

All The Best Phil.

it does seem a bit long and drawn out

Only because it is :slight_smile:

A few people have mentioned using a loop instead of all that testing but you haven't implemented that. Here's a similar example

for (int i = 4; i < 9; i++) {
    digitalWrite(i, i <= count ? HIGH : LOW);
}

That should replace about 50 lines of code and you can do it twice.


Rob

for loops and arrays, mon frere, for loops and arrays

I would like to replace a load of code with

for (int i = 4; i < 9; i++) {
    digitalWrite(i, i <= count ? HIGH : LOW);
}

But if i am totaly honest i have no idea what to replace and where.

Phil.

Just as i thought all was going well (despite my messy code) after operating teh buttons a few times, the program seems to crash for want of a better word, it will just freeze wherever it feels like and not do anything regardless of input states. does anyone have any ideas?

Thanks,

Phil.

Start by fixing the atrocious formatting. Put each curly brace on its own line and use the Auto Format Tool in the Arduino software.

Like so?

// Variables That will change:
int count = 0;  // counter for the number of button presses
int UpState = 0;         // current state of the up button
int UpButtonState = 0;     // previous state of the up button
int DownState = 0;        // current state of the down button
int DownButtonState = 0;  // previous state of the down button
void setup() 

{
  // initialize input/output:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()

{
  // read the Down pushbutton input pin:
  UpState = digitalRead(2);

  // compare the buttonState to its previous state
  if (UpState != UpButtonState) 
  {
    // if the current state is HIGH then the button went from off to on
    if (UpState == HIGH) {
      // if the state has changed, increment the counter Down
      count++;
      //Constrain the counter to go no lower than 0 and no higher than 5
      count =  constrain(count,0,5);
      Serial.print("Count:  ");
      Serial.println(count);
    }
    // if the current state is LOW then the button went from on to off:

    // save the current state as the last state, for next time through the loop
    UpButtonState = UpState;

    //if the counter is at 1
    if (count == 1)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 51);

      //if the counter is at 2
    }
    else if (count == 2)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 102);

      //if the counter is at 3
    } 
    else if (count == 3)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 153);

      //if the counter is at 4
    }
    else if (count == 4)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);
      analogWrite(9, 204);

      //if the counter is at 5
    }
    else if (count == 5)
    {


      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      analogWrite(9, 255);

      //if the counter is at 0 or the unit has just been turned on default to
    }
    else
    {

      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 0);

    } 
  }
  else if  // read the Down pushbutton input pin:
  (DownState = digitalRead(3));

  // compare the buttonState to its previous state
  if (DownState != DownButtonState) 
  {
    // if the current state is HIGH then the button went from off to on
    if (DownState == HIGH) {
      // if the state has changed, increment the counter Down
      count--;
      //Constrain the counter to go no lower than 0 and no higher than 5
      count =  constrain(count,0,5);
      Serial.print("Count:  ");
      Serial.println(count);
    }
    // if the current state is LOW then the button went from on to off:

    // save the current state as the last state, for next time through the loop
    DownButtonState = DownState;

    //if the counter is at 1
    if (count == 1)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 51);

      //if the counter is at 2
    }
    else if (count == 2)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 102);

      //if the counter is at 3
    } 
    else if (count == 3)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 153);

      //if the counter is at 4
    }
    else if (count == 4)
    {

      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);
      analogWrite(9, 204);

      //if the counter is at 5
    }
    else if (count == 5)
    {


      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      analogWrite(9, 255);

      //if the counter is at 0 or the unit has just been turned on default to
    }
    else
    {

      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      analogWrite(9, 0);
    }
  }
}

Phil.

Like so?

void loop()

{
  // read the Down pushbutton input pin:
  UpState = digitalRead(2);

  // compare the buttonState to its previous state
  if (UpState != UpButtonState) 
  {
    // if the current state is HIGH then the button went from off to on
    if (UpState == HIGH) {

Well, there's every and there's EVERY. Every does not mean some.

The blank line between loop() and { should go. Lots of other blank lines should go, too.

You get some serial output. You don't show any. Is there some pattern to the value in count? Nothing obvious is wrong with the code, except that there is far more of it than it needs to be.

This doesn't look right:

  else if  // read the Down pushbutton input pin:
  (DownState = digitalRead(3));

My suggestion for rewriting with loops, do it, you should.

Here's a rejigged version of you code, simplified and with all the redundant stuff removed.

// Variables That will change:
int count = 0;  // counter for the number of button presses
int currentUpState = 0;         // current state of the up button
int lastUpButtonState = 0;     // previous state of the up button
int currentDownState = 0;        // current state of the down button
int lastDownButtonState = 0;  // previous state of the down button

int aVals [] = {51,102,153,204,255};

void setup() {
  // initialize input/output:
  pinMode(2, INPUT);
  pinMode(3, INPUT);

  for (int i = 4; i <= 9; i++) {
    pinMode(i, OUTPUT);
  }
  Serial.begin(9600);
}

void loop() {
  int countVal = 0;
  currentUpState = digitalRead(2); // read the Up pushbutton input pin
  if (currentUpState != lastUpButtonState) {
    countVal = 1;
    lastUpButtonState = currentUpState;
  }
  currentDownState = digitalRead(3); // read the Down pushbutton input pin
  if (currentDownState != lastDownButtonState) {
    countVal = -1;
    lastDownButtonState = currentDownState;
  }
  if (countVal) {
    count += countVal;
    count = constrain(count,0,5);
    Serial.print("Count:  ");
    Serial.println(count);
    for (int i = 4; i < 9; i++) {
      digitalWrite(i, i < count ? HIGH : LOW);
      analogWrite(9, aVals[i-4]);
    }
  }
  delay (30);
}

I hope it didn't lose something in the translation but at least it will be easier to debug.

EDIT: BTW there's no debouncing in that code so you could easily get multiple counts per press. I've added a poor man's debounce in the form of a delay().


Rob

The above code sisnt work , it is almost like once i have pushed teh buttons too many times like the memory if full or something, very odd. I have de-bounced the switch with a resistor and a capacitor externaly which is why you didnt see any de-bouncing in my code.

Thanks,

Phil.