3
\$\begingroup\$

I'm trying to switch between 4 different brightness of an LED using an Arduino Uno. I've used analogWrite and not digitalWrite with PWM. The problem i'm facing is that the brightness does not switch at all when the pushbutton is pressed. Here's my code:

#define led 11 //assign pin 11 to LED
#define BUTTON 7 //assign pin 7 to pushbutton
int val=0; //val used to check current status of pushbutton later in the program
int old_val=0; //old_val to check previous status
int state=0; //state to check button presses

void setup()
{
  pinMode(led,OUTPUT);
  pinMode(BUTTON,INPUT);
}

void loop()
{
  val=digitalRead(BUTTON); //check status of pushbutton
  if((val==HIGH)&&(old_val==LOW)) //button pressed
  {
    state++; //increment state
    delay(10); //debounce consideration
    if(state>4) //want only 4 brightness options
    {
      state=1;
      delay(10);
    }
  }
  old_val=val;
  if(state==1)
  {
    analogWrite(led,0); //0 brightness
  }
  else if(state==2)
  {
    analogWrite(led,75);
  }
  else if(state==3)
  {
    analogWrite(led,150);
  }
  else if(state==4)
  {
    analogWrite(led,255);
  }
}

How can I resolve this problem?

\$\endgroup\$
  • \$\begingroup\$ Does your push button automatically switch back after releasing it? \$\endgroup\$ – Keelan Apr 22 '13 at 15:14
  • \$\begingroup\$ Yes it does. Instead of switching to the next brightness, it goes back to the same brightness. \$\endgroup\$ – user22934 Apr 22 '13 at 15:42
  • \$\begingroup\$ #define led 11 //assign pin 11 to LED this is wrong as long as you use analog , this can be from 0-5 . \$\endgroup\$ – yahya tawil Apr 22 '13 at 16:03
  • 1
    \$\begingroup\$ I thought pins 1 to 5 serve only as analog INPUTS. Are you sure about it? \$\endgroup\$ – user22934 Apr 22 '13 at 16:25
  • 3
    \$\begingroup\$ You can use pin 11 for PWM! In fact, you can use any pin marked with a "~" for PWM on the Arduino Uno. The reference page for AnalogWrite (arduino.cc/de/Reference/AnalogWrite) confirms that too. \$\endgroup\$ – Philipp Horn Apr 23 '13 at 13:15
4
\$\begingroup\$

Although your code has some weaknesses it shouldn't generate the behavior you describe. You haven't provided any shematic of the setup so my answer is really just a guess but a missing pull-down resistor would probably cause exactly what you're telling : the digital reading of the button pin sporadically switches between HIGH and LOW, and the led appears to flicker randomly instead of just changing brightness as expected.

\$\endgroup\$
  • \$\begingroup\$ Thanks for pointing it out. I just realized there's no pull down resistor in my circuit. Will revert back when I'm able to add one. \$\endgroup\$ – user22934 Apr 22 '13 at 17:50
  • 3
    \$\begingroup\$ The AVR that the arduino is based on has internal pull up resistors. It is much easier to use the switch to connect to ground and have the internal pullup resistors enabled. \$\endgroup\$ – Kurt E. Clothier Apr 22 '13 at 19:22
  • 1
    \$\begingroup\$ To enable the internal pullup set the pin mode as INPUT_PULLUP instead of INPUT \$\endgroup\$ – Craig May 1 '13 at 16:50
0
\$\begingroup\$
#define led 11 //assign pin 11 to LED
#define BUTTON 7 //assign pin 7 to pushbutton
#define SAMPLING 250 //250milliSecond per press, used for debouncing

static unsigned long TIMER = 0; //record current millis()
byte state = 0; //val used to check current status of pushbutton later in the program
byte brightness[] = {0, 75, 150, 255}; //comment this out if not picky  

void setup()
{
  pinMode(led,OUTPUT);
  pinMode(BUTTON,INPUT);
  digitalWrite(BUTTON, HIGH); //use internal pull up resistor, no need for external resistor   
}

void loop()
{
  if(millis() - TIMER >= SAMPLING) //sampling ~= 250mS
  {  
    TIMER = millis();
    if(!digitalRead(BUTTON)) //button pressed, pulled low
    {
      state++; //increment state
      if(state > 3) //want only 4 brightness options, 0, 1, 2, 3
        state = 0;
      analogWrite(led, brightness[state]); //if you aren't picky, use analogWrite(led, state*85); 
                                           //no byte array;
                                           //0*85 = 0
                                           //1*85 = 85
                                           //2*85 = 170
                                           //3*85 = 255
    }
  }
}  
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.