Pages: [1]   Go Down
Author Topic: Problem with multiple button brightness control  (Read 820 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Hello World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,
I'm working on a light system that has 6 button that regulate the brightess of two leds,

two sets of buttons to increase/decrease brightess of led1, another setup similar to the one before which controls led2 and a third set of buttons that increases and decreases both leds together.
I was successfull are making both buttons independent but when I try to control them together the third set of button take over the loop,
can anyone help me figure out what is blocking the loop,

thanks smiley


Code:
//Variables won't change
const int lamp1 = 11;      //led #1
const int lamp2 = 10;       //led #2
//
const int upPin = 2;      //button for brightness up
const int downPin = 3;    //button for brightness down
//
const int upPin2 = 4;      //button for brightness up
const int downPin2 = 5;    //button for brightness down
//
const int upPin3 = 6;      //ALL LIGHT UP AND DOWN
const int downPin3 = 7;    //button for brightness down

// BLUE BUTTON
int lastCounter = 5;
int counter;              //the basic counter
int up;                   //brightnes up button reading
int down;                 //brightnes down button reading

// RED BUTTON
int lastCounter2 = 5;
int counter2;              //the basic counter
int up2;                   //brightnes up button reading
int down2;                 //brightnes down button reading

// RED BUTTON
int lastCounter3 = 5;
int counter3;              //the basic counter
int up3;                   //brightnes up button reading
int down3;                 //brightnes down button reading


int volt;                 //PWM value sent to the leds
int volt2;                 //PWM value sent to the leds
int volt3;                 //PWM value sent to the leds

void setup(){
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(up2, INPUT);
  pinMode(down2, INPUT);
  pinMode(up3, INPUT);
  pinMode(down3, INPUT);
 pinMode(up3, INPUT);
 pinMode(down3, INPUT);
  Serial.begin(9600);

 
}

void loop(){
  counter = lastCounter;      //counter recyrculation
  counter2 = lastCounter2;      //counter recyrculation
  counter3 = lastCounter3;      //counter recyrculation

  up = digitalRead(upPin);        //button pins reading
  down = digitalRead(downPin);
  up2 = digitalRead(upPin2);        //button two pins reading
  down2 = digitalRead(downPin2);
 up3 = digitalRead(upPin3);        //button two pins reading
  down3 = digitalRead(downPin3);
 
 
// BUTTON BLUE
   if (up == HIGH) {                    //if the up button is pressed
      counter = counter++;              //counter increases by one
      Serial.println("Counter");
      Serial.println(counter);

    }
   
   if (down == HIGH) {            //if down button is pressed
        counter = counter--;       //counter decreases by one
        Serial.println("Counter");
        Serial.println(counter);
      }
     
// BUTTON RED
     
   if (up2 == HIGH) {                    //if the up button is pressed
      counter2 = counter2++;              //counter increases by one
      Serial.println("Counter2");
      Serial.println(counter2);

    }
   
   if (down2 == HIGH) {            //if down button is pressed
        counter2 = counter2--;       //counter decreases by one
        Serial.println("Counter2");
        Serial.println(counter2);
      }   
   
// EVERYONE
     
  if (up3 == HIGH) {                    //if the up button is pressed
      counter3 = counter3++;              //counter increases by one
      Serial.println("Counter3");
      Serial.println(counter3);

    }
   
   if (down3 == HIGH) {            //if down button is pressed
        counter3 = counter3--;       //counter decreases by one
        Serial.println("Counter3");
        Serial.println(counter3);
      }     
   
  if (counter > 6){      //check that counter won't get further than 5
    counter = 6;
  }
 
  if (counter < 2){    //check that counter won't fall bellow 1
    counter = 1;
  }
 
  switch (counter){    //depending on counter value the propper PWM value is sent to the leds
 
   
  case 1:
  volt = 0;
  break;
 
  case 2:
  volt = 50;
  break;
 
  case 3:
  volt = 100;
  break;
 
  case 4:
  volt = 150;
  break;
 
  case 5:
  volt = 200;
  break;
 
  case 6:
  volt = 250;
  break;

 
  }

if (counter2 > 6){      //check that counter won't get further than 5
    counter2 = 6;
  }
 
  if (counter2 < 2){    //check that counter won't fall bellow 1
    counter2 = 1;
  }
 
  switch (counter2){    //depending on counter value the propper PWM value is sent to the leds
 
   
  case 1:
  volt2 = 0;
  break;
 
  case 2:
  volt2 = 50;
  break;
 
  case 3:
  volt2 = 100;
  break;
 
  case 4:
  volt2 = 150;
  break;
 
  case 5:
  volt2 = 200;
  break;
 
  case 6:
  volt2 = 250;
  break;

 
  }
 
 if (counter3 > 6){      //check that counter won't get further than 5
    counter3 = 6;
  }
 
  if (counter3 < 2){    //check that counter won't fall bellow 1
    counter3 = 1;
  }
 
  switch (counter3){    //depending on counter value the propper PWM value is sent to the leds
 
   
  case 1:
  volt3 = 0;
  break;
 
  case 2:
  volt3 = 50;
  break;
 
  case 3:
  volt3 = 100;
  break;
 
  case 4:
  volt3 = 150;
  break;
 
  case 5:
  volt3 = 200;
  break;
 
  case 6:
  volt3 = 250;
  break;

 
  }
 
  analogWrite(lamp1, volt);    //leds turn on with the desirable PWM
  analogWrite(lamp2, volt2);
  analogWrite(lamp1, volt3);
  analogWrite(lamp2, volt3);

  lastCounter = counter;      //counter recylculation
  lastCounter2 = counter2;      //counter recylculation
  lastCounter3 = counter3;      //counter recylculation

 
  delay(500);             
 
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

counter2 and counter3 have values on every pass through loop. So, on every pass through loop, you are acting on the value in counter2 and on the value in counter3.

You want to restructure your code so that you do something, on any given pass through loop() only if one of the switches is pressed, resulting in a change to counter, counter2 or counter3.

PS. It's not a good idea to number 2 out of three related variables.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
Hello World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply PaulS,

So I may solve this problem by braking the code in three parts,
I'm not a a great coder, but should I create 3 loops like "void btnRedlights () {}", etc?

can you also give me a small explanation of what this means, I do not get it,
Quote
PS. It's not a good idea to number 2 out of three related variables.

thanks a lot man,

Z_
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7305
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This program does NOT do "two sets of buttons to increase/decrease brightess of led1, another setup similar to the one before which controls led2 and a third set of buttons that increases and decreases both leds together.".

What it does instead, as you observed, is to control both LEDs with button set 3.

Read your code again and see why button set 3 always takes over, at this portion:

Code:
  analogWrite(lamp1, volt);    //leds turn on with the desirable PWM
  analogWrite(lamp2, volt2);
  analogWrite(lamp1, volt3);
  analogWrite(lamp2, volt3);

To solve your problem without changing much of the code, you should increment counter 1 and 2 when button 3 up is pressed, not a new counter 3.

Also, don't use counter=counter++. That is not how you increment counter. Do counter++ or counter=counter+1.
Logged


East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4400
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
PS. It's not a good idea to number 2 out of three related variables.
It will not cause a problem in the program because you can give variables names of your choice but counter, counter2 and counter3 would be better named counter1, counter2 and counter3 to help understanding the code.  The same for the other variables with similar names.

Once you have a set of variables with a similar function such as v1, v2, v3 then your mind out to start thinking about making the variable an array and using a for loop to iterate through them.  I suggest that you don't try this until you have got the repetitive code working which you can then improve.

On another subject, you could eliminate the switch/case constructs in your program by putting the voltage values in an array and using the value of the counter as an index to the array to get the matching voltage.  The sections of code that use switch/case would also be a good candidate to make into a function called with the counter value as a parameter.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
Hello World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks liudr and UKHeliBob,
I'll try to implement your tips,
cheers smiley
 
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could also look into arrays and indexes.

counter1
counter2
counter3

are more easily handled as

Code:
byte counter[3];
for ( byte i = 0; i < 3; i++ )
{
  counter[ i ] = i + 1; // by controlling the index you control which array member is assigned
}
// now counter[0] == 1, counter[1] == 2, counter[2] == 3

Just seeing that first sketch hurt.
The code below compiles and should show some lessons, hopefully a boost on your way.
One thing though, learn about inputs with pullups and you can save on resistors.

BUT -- this needs a fix -- can you guess what?

Code:
// indents kept clean using IDE Tools->Auto Formet

const byte buttons = 6;
const byte buttonPin[ buttons ] = {
  2, 3, 4, 5, 6, 7 }; // up1, dn1, up2, dn2, up3, dn3
byte buttonTest = 0;
byte pressed; // made global just to save allocating on the stack every time loop() executes

const byte leds = 2;
const byte ledPin[ leds ] = {
  11, 10 };
byte ledPower[ leds ] = {
  5, 5 };
const byte  minPower = 1, maxPower = 5;

void pinBoundsCheck( byte led ) // showing passed value use
{
  ledPower[ led ] = min( max( ledPower[ led ], minPower ), maxPower );
}

void setup()
{
  for ( byte i = 0; i < buttons; i++ ) // i only exists in this loop
  {  
    pinMode( buttonPin[ i ], INPUT );
  }  
}

void loop()
{
  pressed =  digitalRead( buttonPin[ buttonTest ] ); // it will be either LOW or HIGH

  if ( pressed ) // LOW is FALSE, HIGH is TRUE
  {
    switch ( buttonTest )
    {
    case 0 : // led 1 up
      ledPower[ 0 ]++;
      break;
    case 1 : // led 1 down
      ledPower[ 0 ]--;
      break;
    case 2 : // led 2 up
      ledPower[ 1 ]++;
      break;
    case 3 : // led 2 down
      ledPower[ 1 ]--;
      break;
    case 4 : // both leds up
      ledPower[ 0 ]++;
      ledPower[ 1 ]++;
      break;
    case 5 : // both leds down
      ledPower[ 0 ]--;
      ledPower[ 1 ]--;
      break;
    }
    
    buttonTest++; // test next button next tme through loop()
    if ( buttonTest >= buttons )
    {
      buttonTest = 0;
    }

    pinBoundsCheck( 0 );
    pinBoundsCheck( 1 );

    analogWrite( ledPin[ 0 ], ledPower[ 0 ] * 50 );
    analogWrite( ledPin[ 1 ], ledPower[ 1 ] * 50 );
  }
}




« Last Edit: July 02, 2013, 11:35:18 am by GoForSmoke » Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
Hello World!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My last post got lost,
anyways, nice reply GoForSmoke,
I'm really bad at coding and often end up with riddles instead of tidy codes,
I solved the problem I had before, now I'll try to modify my code with your method,
thanks

here's the working code,


Code:
//Variables won't change
const int lamp1 = 11;      //led #1
const int lamp2 = 10;       //led #2
//
const int upPin = 2;      //button for brightness up
const int downPin = 3;    //button for brightness down
//
const int upPin2 = 4;      //button for brightness up
const int downPin2 = 5;    //button for brightness down
//
const int upPin3 = 6;      //ALL LIGHT UP AND DOWN
const int downPin3 = 7;    //button for brightness down

// BLUE BUTTON
int lastCounter = 5;
int counter;              //the basic counter
int up;                   //brightnes up button reading
int down;                 //brightnes down button reading

// RED BUTTON
int lastCounter2 = 5;
int counter2;              //the basic counter
int up2;                   //brightnes up button reading
int down2;                 //brightnes down button reading

// EVERYTHING BUTTON
int lastCounter3 = 5;
int counter3;              //the basic counter
int up3;                   //brightnes up button reading
int down3;                 //brightnes down button reading


int volt;                 //PWM value sent to the leds
int volt2;                 //PWM value sent to the leds
int volt3;                 //PWM value sent to the leds

void setup(){
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(up2, INPUT);
  pinMode(down2, INPUT);
  pinMode(up3, INPUT);
  pinMode(down3, INPUT);
  Serial.begin(9600);

 
}

void loop(){
  counter = lastCounter;      //counter recyrculation
  counter2 = lastCounter2;      //counter recyrculation

  up = digitalRead(upPin);        //button pins reading
  down = digitalRead(downPin);
  up2 = digitalRead(upPin2);        //button two pins reading
  down2 = digitalRead(downPin2);
  up3 = digitalRead(upPin3);        //button two pins reading
  down3 = digitalRead(downPin3);
 
 
// BUTTON BLUE
   if (up == HIGH) {                    //if the up button is pressed
      counter = counter+1;              //counter increases by one
      Serial.println("Counter");
      Serial.println(counter);

    }
   
   if (down == HIGH) {            //if down button is pressed
        counter = counter-1;       //counter decreases by one
        Serial.println("Counter");
        Serial.println(counter);
      }
     
// BUTTON RED
     
   if (up2 == HIGH) {                    //if the up button is pressed
      counter2 = counter2+1;              //counter increases by one
      Serial.println("Counter2");
      Serial.println(counter2);

    }
   
   if (down2 == HIGH) {            //if down button is pressed
        counter2 = counter2-1;       //counter decreases by one
        Serial.println("Counter2");
        Serial.println(counter2);
      }   
   
// EVERYONE
     
   if (up3 == HIGH) {                    //if the up button is pressed
      counter = counter+1 == counter2+1;
      //counter2 = counter2+1;
//counter increases by one
      Serial.println("Counter3");
      Serial.println(counter3);

    }
   
   if (down3 == HIGH) {            //if down button is pressed
        counter = counter-1;       //counter decreases by one
        //counter2 = counter2-1;
        Serial.println("Counter3");
        Serial.println(counter3);
      }   
     
   
  if (counter > 6){      //check that counter won't get further than 5
    counter = 6;
  }
 
  if (counter < 2){    //check that counter won't fall bellow 1
    counter = 1;
  }
 
  switch (counter){    //depending on counter value the propper PWM value is sent to the leds
 
   
  case 1:
  volt = 0;
  break;
 
  case 2:
  volt = 50;
  break;
 
  case 3:
  volt = 100;
  break;
 
  case 4:
  volt = 150;
  break;
 
  case 5:
  volt = 200;
  break;
 
  case 6:
  volt = 250;
  break;

 
  }

if (counter2 > 6){      //check that counter won't get further than 5
    counter2 = 6;
  }
 
  if (counter2 < 2){    //check that counter won't fall bellow 1
    counter2 = 1;
  }
 
  switch (counter2){    //depending on counter value the propper PWM value is sent to the leds
 
   
  case 1:
  volt2 = 0;
  break;
 
  case 2:
  volt2 = 50;
  break;
 
  case 3:
  volt2 = 100;
  break;
 
  case 4:
  volt2 = 150;
  break;
 
  case 5:
  volt2 = 200;
  break;
 
  case 6:
  volt2 = 250;
  break;

 
  }
 
 
 
  analogWrite(lamp1, volt);    //leds turn on with the desirable PWM
  analogWrite(lamp2, volt2);

  lastCounter = counter;      //counter recylculation
  lastCounter2 = counter2;      //counter recylculation
  lastCounter3 = counter3;      //counter recylculation

 
  delay(500);             
 
}

 
Logged

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm really bad at coding and often end up with riddles instead of tidy codes,
I solved the problem I had before, now I'll try to modify my code with your method,
thanks

here's the working code,

Code:
//Variables won't change
const int lamp1 = 11;      //led #1
const int lamp2 = 10;       //led #2
const int upPin = 2;      //button for brightness up
const int downPin = 3;    //button for brightness down
const int upPin2 = 4;      //button for brightness up
const int downPin2 = 5;    //button for brightness down
const int upPin3 = 6;      //ALL LIGHT UP AND DOWN
const int downPin3 = 7;    //button for brightness down

// BLUE BUTTON
int lastCounter = 5;
int counter;              //the basic counter
int up;                   //brightnes up button reading
int down;                 //brightnes down button reading

// RED BUTTON
int lastCounter2 = 5;
int counter2;              //the basic counter
int up2;                   //brightnes up button reading
int down2;                 //brightnes down button reading

// EVERYTHING BUTTON
int lastCounter3 = 5;
int counter3;              //the basic counter
int up3;                   //brightnes up button reading
int down3;                 //brightnes down button reading
int volt;                 //PWM value sent to the leds
int volt2;                 //PWM value sent to the leds
int volt3;                 //PWM value sent to the leds

void setup(){
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(up2, INPUT);
  pinMode(down2, INPUT);
  pinMode(up3, INPUT);
  pinMode(down3, INPUT);
  Serial.begin(9600);

}

void loop(){
  counter = lastCounter;      //counter recyrculation
  counter2 = lastCounter2;      //counter recyrculation

  up = digitalRead(upPin);        //button pins reading
  down = digitalRead(downPin);
  up2 = digitalRead(upPin2);        //button two pins reading
  down2 = digitalRead(downPin2);
  up3 = digitalRead(upPin3);        //button two pins reading
  down3 = digitalRead(downPin3);
 
 
// BUTTON BLUE
   if (up == HIGH) {                    //if the up button is pressed
      counter = counter+1;              //counter increases by one
      Serial.println("Counter");
      Serial.println(counter);

    }
   
   if (down == HIGH) {            //if down button is pressed
        counter = counter-1;       //counter decreases by one
        Serial.println("Counter");
        Serial.println(counter);
      }
     
// BUTTON RED
     
   if (up2 == HIGH) {                    //if the up button is pressed
      counter2 = counter2+1;              //counter increases by one
      Serial.println("Counter2");
      Serial.println(counter2);

    }
   
   if (down2 == HIGH) {            //if down button is pressed
        counter2 = counter2-1;       //counter decreases by one
        Serial.println("Counter2");
        Serial.println(counter2);
      }   
   
// EVERYONE
     
   if (up3 == HIGH) {                    //if the up button is pressed
      counter = counter+1 == counter2+1;
      //counter2 = counter2+1;
//counter increases by one
      Serial.println("Counter3");
      Serial.println(counter3);

    }
   
   if (down3 == HIGH) {            //if down button is pressed
        counter = counter-1;       //counter decreases by one
        //counter2 = counter2-1;
        Serial.println("Counter3");
        Serial.println(counter3);
      }   
        
  if (counter > 6){      //check that counter won't get further than 5
    counter = 6;
  }
 
  if (counter < 2){    //check that counter won't fall bellow 1
    counter = 1;
  }
 
  switch (counter){    //depending on counter value the propper PWM value is sent to the leds
     
  case 1:
  volt = 0;
  break;
 
  case 2:
  volt = 50;
  break;
 
  case 3:
  volt = 100;
  break;
 
  case 4:
  volt = 150;
  break;
 
  case 5:
  volt = 200;
  break;
 
  case 6:
  volt = 250;
  break;
 
  }

if (counter2 > 6){      //check that counter won't get further than 5
    counter2 = 6;
  }
 
  if (counter2 < 2){    //check that counter won't fall bellow 1
    counter2 = 1;
  }
 
  switch (counter2){    //depending on counter value the propper PWM value is sent to the leds
    
  case 1:
  volt2 = 0;
  break;
 
  case 2:
  volt2 = 50;
  break;
 
  case 3:
  volt2 = 100;
  break;
 
  case 4:
  volt2 = 150;
  break;
 
  case 5:
  volt2 = 200;
  break;
 
  case 6:
  volt2 = 250;
  break;
 
  }
   
  analogWrite(lamp1, volt);    //leds turn on with the desirable PWM
  analogWrite(lamp2, volt2);

  lastCounter = counter;      //counter recylculation
  lastCounter2 = counter2;      //counter recylculation
  lastCounter3 = counter3;      //counter recylculation
 
 delay(500);             
}
Why all those counters and variables?
Pseudo code:
if (digitalRead(upPin) ==HIGH);     // up button pressed   
volts1 = volts1 + 50
if volts1>250 then volts1 =250 //keep in range.
serial.Print("LED 1   ")
serial.Println (1 + (volts1 / 50))
analogWrite(lamp1, volts1)

if (digitalRead(downPin) == HIGH)  // down button pressed     
volts1 = volts1 - 50
if volts1 < 0 then volts1 = 0 //keep in range.
serial.Print("LED 1   ")
serial.Println (1 + (volts1 / 50))
analogWrite(lamp1, volts1)

etc.

For up and down button3 you'll need to increment or decrement both volts1 and volts2 and check they're both within range before writing the PWMs.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And the bug that needs fixing is code to keep track of if each button is pressed so that if it was pressed last time through loop() it doesn't check again until it hasn't been pressed for 10 millis() or more (debounce).
Otherwise you touch a button and in less than a millisecond it has been read HIGH enough times to take the light to the limit, 1 or 5 depending on if the up or down button has been hit.

Wheeee.

Zaphod, notice that I have it only read one button per pass through loop(). That simplifies the logic, shortens the code and gets through loop() in a small fraction of a millisecond even when a button is pressed, ie many 1000's of times per second. When the checked button is not pressed the loop() should run close to 1,000,000 times a second.

You don't have to or need to do everything every time through loop().

Learn to walk through example code line by line like algebra only with branching and loop logic. You will see techniques that if you can work them out will save you a lot of time and improve your approach to any project in the future. It's how you get good quicker.

Ignore the feeling that you're not up to understanding code and start hacking references (ask questions if need be but look it up first so you can ask what will help you) wherever there's a part you never saw or don't understand. Learn how to use the resources you have. Learn how to learn and you won't have to wait for everything. Then you can pick what you do wait for even as you learn other things yourself.

The tutorial examples and scraps like I posted are not rocket science or differential equations. Kids can learn this, at least the sharp ones who have patience with the subject and themselves. Maybe not expecting so much quickly gives them an advantage. Being interested is usually enough, one friend long ago told me that maintaining interest is a sign of IQ.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And the bug that needs fixing is code to keep track of if each button is pressed so that if it was pressed last time through loop() it doesn't check again until it hasn't been pressed for 10 millis() or more (debounce).
Otherwise you touch a button and in less than a millisecond it has been read HIGH enough times to take the light to the limit, 1 or 5 depending on if the up or down button has been hit.

Wheeee.
Agreed, but the last line of his original code, delay(500);, should take care of debouncing.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And the bug that needs fixing is code to keep track of if each button is pressed so that if it was pressed last time through loop() it doesn't check again until it hasn't been pressed for 10 millis() or more (debounce).
Otherwise you touch a button and in less than a millisecond it has been read HIGH enough times to take the light to the limit, 1 or 5 depending on if the up or down button has been hit.

Wheeee.
Agreed, but the last line of his original code, delay(500);, should take care of debouncing.

I was referring to the bug I left in the example I posted before.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And the bug that needs fixing is code to keep track of if each button is pressed so that if it was pressed last time through loop() it doesn't check again until it hasn't been pressed for 10 millis() or more (debounce).
Otherwise you touch a button and in less than a millisecond it has been read HIGH enough times to take the light to the limit, 1 or 5 depending on if the up or down button has been hit.

Wheeee.
Agreed, but the last line of his original code, delay(500);, should take care of debouncing.

I was referring to the bug I left in the example I posted before.


Sorry, I thought you were referring to my lack of debounce code.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No sir!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: