Noob - help needed

Hi,

I am using an Arduino for the first time and am trying to write code for a circuit which will increase the brightness of an led with one button and completely turn off with the other. I have written some code but am unsure why its not working

Thanks in advance!

#define led 5
#define buttonA 9
#define buttonB 8

void setup()
{
pinMode(led, OUTPUT);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
}

int brightness;

void loop()
{
brightness = 1;
if (digitalRead(buttonA) == LOW)
{
for (int i=0; i<255; i+=1)
brightness +=1;
digitalWrite(led, brightness);
delay(10);
}

if (digitalRead(buttonB) == LOW)
{
digitalWrite(led, LOW);
}
}

Wow!

So many things wrong!

You set "brightness" to 1 on every pass of the loop.

digitalWrite sets the pin to HIGH on any argument of 1 or more, so the LED will always be on if buttonA is pressed. Were you thinking of analogWrite?

If the LED is on and buttonB is pressed, the LED will go off.

So I have made a couple of changes but am still getting an error as “brightness” dosent have a name. What I am unsure about is where do I define the “brightness” = to 0?

Thanks in advance

#define led 5
#define buttonA 9
#define buttonB 8

void setup()
{
pinMode(led, OUTPUT);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
}

int brightness;
brightness = 0;

void loop()
{
if (digitalRead(buttonA) == LOW)
{
for (int i=0; i<255; i+=1)
brightness +=1;
analogWrite(led, brightness);
delay(100);
}

if (digitalRead(buttonB) == LOW)
{
digitalWrite(led, LOW);
}
}

  brightness = 0;

You are trying to execute code outside of a function.

int brightness = 0;

Does the same thing, but no error.

OK so there may be a few errors. I’m no programming expert by any means, but I’ll have a go…
Please someone correct any errors in this. I’m sure there will be a few. I’ve been a beginner for years.
a few notes for starters. These are just my opinions, they may be “right” they may be “wrong” it’s just what I do and the way I do it.

const int LED = 5;//pin 5 is a PWM pin on most arduinos
const int BUTTONA = 9;
const int BUTTONB = 8;
int brightness;

void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTONB, INPUT_PULLUP);
pinMode(BUTTONA, INPUT_PULLUP);
}

void loop()
{
if (digitalRead(BUTTONA) == LOW) { // I prefer opening curlies at the end of the line

brightness=0;// set brightness off as soon as button A is pressed

while (digitalRead(BUTTONA) == LOW){// while the buttonA is held down
analogWrite(LED, brightness);// use analogWrite to send PWM output to pin LED (5)
if (brightness <= 255) brightness ++;// increment brigntness to a maximum of 255
delay(10); // it will take approx 2.5 seconds for brightness to reach full
}
delay(20); // wait for the mechanical switch contacts to stop bouncing. may heed longer.
}

if (digitalRead(BUTTONB) == LOW) {
analogWrite(LED, 0);
}// don’t need to worry about debouncing here. no issues even if the line is repeated a few times.
}

When posting code please use the code button </>

so your code 
looks like this

and is easy to copy to a text editor See How to use the forum

…R

const int  LED = 5;//pin 5 is a PWM pin on most arduinos
const int BUTTONA = 9;
const int BUTTONB =  8;
 int brightness;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTONB, INPUT_PULLUP);
  pinMode(BUTTONA, INPUT_PULLUP);
}

void loop()
{
    if  (digitalRead(BUTTONA) == LOW) { // I prefer opening curlies at the end of the line
       
        brightness=0;// set brightness off as soon as button A is pressed
       
        while (digitalRead(BUTTONA) == LOW){// while the buttonA is held down
           analogWrite(LED, brightness);// use analogWrite to send PWM output  to pin LED (5)
           if (brightness <= 255) brightness ++;// increment brigntness to a maximum of 255
           delay(10); // it will take approx 2.5 seconds for brightness to reach full
        }
        delay(20);  // wait for the mechanical switch contacts to stop bouncing. may heed longer.
   }
 
     if (digitalRead(BUTTONB) == LOW) {
         analogWrite(LED, 0);
    }// don't need to worry about debouncing here. no issues even if the line is repeated a few times.
}

sorry, too many Pimms!

Thanks for using the code button.

Does the program do what you want?

If not, what does it actually do and what do you want it to do that is different?

...R