Arduino UNO - Help me fast!

Arduino UNO
Led on pin 13 - basic led.
two buttons - first increases brightness (ex. +5 unit) second reduces brightness (ex. -5 unit)
one press = one time (and not in the loop)
I need a code that allows increases or reduces brightness, pleas help me!


Connect D111 to D13.

Set D13 to input, D11 to output.

if button press 1, if PWMvalue <=250, then PWMvalue =  PWMValue +5, analogWrite PWMvalue to D11.

if button press 2, if PWMvalue >=5, then PWMvalue = PWMvalue -5, analogWrite PWMvalue D11.

I have to use:
One arduino uno - on it is 13 led, two buttons - separate buttons, cables and contact plate.
I have to change it for the brightness to be changed using two buttons.

On the basis of this I should do: (but if isnt possible to do i need just any working code.)
(Moderator edit - CTRL-T formatting, code tags, and missing ; added to the int button = 0 line)

void setup() {

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, INPUT_PULLUP);

int T = 20; // time 20ms f=50Hz
int St = 1; // initial value for 50% fill
int button = 0; // it signals pressing 0 - pressed 1 - no

void loop() {

  digitalWrite(13, HIGH); // LeD on (high is the voltage level)
  delay(T - St);
  digitalWrite(13, LOW); // turn the LED off by making the voltage LOW

  if (digitalRead(11) == LOW) {
    if (button == 1) {    //if not pressed
      button = 0; // reset
  else {
    if (button == 0) { //if were pressed
      button = 1; // shows the button is pressed

So add some code to read the 2 buttons, and increase/decrease ST accordingly.

  1. In my school the teacher does not teach but requires.
  2. When I wanted to borrow arduino-uno home to be able to practice - my teacher said no.

Summarizing I do not know how to do it, I've tried everything, it is not easy for me, I will be very grateful if you help me.

When is the assignment due?

In about six hours.


  1. In my school the teacher does not teach but requires.

Quite honestly I don't believe you.
That might be your perception but it will not be true. In my experance this is said by kids who don't pay attention and leave any assignment to the night before it is due in.

Sure he might not have taught you how to do that specific problem but he would have taught you enough that you could figure it out, if you were paying attention and were smart enough to warrant a pass.

In about six hours.

Contender for “post of the week”. :grinning:

This is probably too late but whatever...

With what, exactly, are you having trouble? What topics have been covered in your course?

Using intuition, how might you go about tackling a task like this?

In a nutshell, you know:

  • you have one LED the brightness of which you need to vary
  • you have to read two buttons
  • if one is depressed, increase the brightness of an LED
  • if the other is depressed, decrease the brightness of the LED

That's it. So look at each requirement in isolation:

LED Brightness
How would you vary the brightness of an LED? Usually you do this by varying the current through it. In this case, you can do this by varying the voltage at the pin the LED is connected to. Now what would you do to a pin to vary the voltage at the pin?

Hint: Study pulse width modulation (PWM) and the use of the analogWrite() function in the Arduino library.

Read buttons
You already know about digitalRead() so that's good. But because the processor can loop so fast, you don't want to read and process the buttons every pass through the loop. Instead you might want to wait until, say, 20mS has passed between checks of the buttons. That way the LEDs increase and decrease in brightness smoothly.

If you start from zero and count to 255 in steps of 5, that's 51 steps. If you update the buttons every 20mS, you'd go from off to full brightness in 51x21mS or ~1-second. You can change your read interval from 20mS to make it as long or short as you want.

So how would you time 20mS? There's a number of ways but perhaps the easiest is using the millis() function. How might you use this?

When you read a button as being pressed, what would you do? How do you know how bright the LED is now and how do you know how to adjust it? Consider the variable you'd use and how you'd manipulate it when you sense a button press...

Do you know what pseudo-code is? You might try creating a simple "text" version of your code to help in creating the real thing.

For example:

time_then = millis()    //get time now
PWM = 128 //mid brightness
if( millis() - time_then >= 20mS ){
if( up button pressed )
    bump PWM up (limit to 255)
else if (down button pressed)
    bump PWM down (limit to 0)
write( analog PWM to pin)
time_then = millis();

You just need to fill in the blanks.

This is probably too late but whatever...

I seem to remember that there is a program on-line that allows you to run your code and watch what would happen.
if you define LED's it will turn them on and off on a virtual led on-screen.

I had teachers that taught us to learn not just their subject but learning in general and they would give us assignments where we were to figure out what to do and get on with it but none of it was beyond our reach.

OTOH those who go to fraud schools like Trump University get educated when they sue the school.

The thing is, I don't know how it is in other places.

You just need to fill in the blanks.

...but keep in mind that analogWrite won't work as you expect on pin 13, and you'll have to do the PWM in software.

I suspect that with the passing of the deadline the OP has lost all interest in the Arduino, until maybe the next assignment when he will be even further behind.

…but keep in mind that analogWrite won’t work as you expect on pin 13, and you’ll have to do the PWM in software.

Ah right. Complicates things a bit.

Ah right. Complicates things a bit.

I think that's the point of the exercise.

I think that's the point of the exercise.

I would cheat and use a Leonardo 8)

I would cheat and use a Leonardo 8)

You’d fail, for not keeping to the spec (see thread title) :wink:

You'd fail, for not keeping to the spec (see thread title) :wink:

That is why it was cheating :smiley: