Skip to main content
changed code
Source Link

I am using an Arduino UNO and I am trying to vary the brightness by steps by 0.5 volts till it gets to a max of 5.0 V using buttonA and decrease the brightness of the LED by 0.5 volts with buttonB I believe that I am close the only. The Problem I have is that when I push buttonA the LED steps it's voltage by 0.5V all the way to 5.0V and with buttonB its steps down to 0. I would like to have the LED step each time I press the button not all at once. Here is my code that I have so far can someone help me with telling the Arduino to Step each time a button is pressed. CODE IS DIFFERENT from another post I did

int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;A2; //buttonA to analog 2
int buttonBpin = 11;A1; // buttonB to analog 1
int maxBright = 255; // between 0 and 255  
int ms = 300;1500;    // delay in milliseconds
int buttonPushCounterA = 0; //counter for the number of button presses
int counterbuttonStateA = 0; // current state of the button
int lastButtonStateA = 0; // previous state of the button
voidint setup()buttonPushCounterB = 0; //counter for the number of button presses
{int buttonStateB = 0; // current state of the button
int lastButtonStateB = 0; // previous state of the button

void setup() 
{
  pinMode(ledPin, OUTPUT);  
    pinMode(buttonApin, INPUT_PULLUP);  
    pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
    if (digitalRead(buttonApin) == LOW)  
 //if buttonA is pushed
 {  {
       buttonStateA if= digitalRead(counter >= 10buttonApin)  ;

     if (buttonStateA != {lastButtonStateA)
  
    {
        if(buttonStateA counter== =LOW)
 10;  
   {
      }  buttonPushCounterA++;
     }
    }

// fade from min to max in increments of 25.5 points: basically (0.5 volts)  
        for (float n = 0; n <= maxBright; n +=25.5)   
        {  
            // sets the value (range from 0 to 255):  
 analogWrite(ledPin,n);
 delay(ms);
}
  }

  if (digitalRead(buttonBpin) == LOW) //if buttonB is analogWrite(ledPin,n);pushed
  {
     buttonStateB = digitalRead(buttonBpin);

       delayif (ms);buttonStateB != lastButtonStateB)
        }  {
    }  

       if(digitalRead(buttonBpin)buttonStateB == LOW)  
    {  
         if (counter{
 <= 0)  
         {  
   buttonPushCounterB--;
          counter = 0;  }
        }  }
    

     // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
        for (float n = maxBright; n >= 0; n -= 25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin, n);  
            delay(ms);  
      }
  }   

  lastButtonStateA = }buttonStateA;
  
}lastButtonStateB = buttonStateB;
 }

I am using an Arduino UNO and I am trying to vary the brightness by steps by 0.5 volts till it gets to a max of 5.0 V using buttonA and decrease the brightness of the LED by 0.5 volts with buttonB I believe that I am close the only Problem I have is that when I push buttonA the LED steps it's voltage all the way to 5.0V and with buttonB its steps down to 0. I would like to have the LED step each time I press the button not all at once. Here is my code that I have so far can someone help me with telling the Arduino to Step each time a button is pressed.

int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;  
int buttonBpin = 11;  
int maxBright = 255; // between 0 and 255  
int ms = 300;    // delay in milliseconds  
int counter = 0;  
  
void setup()   
{  
    pinMode(ledPin, OUTPUT);  
    pinMode(buttonApin, INPUT_PULLUP);  
    pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
    if (digitalRead(buttonApin) == LOW)  
     {  
        if (counter >= 10)  
        {  
             counter = 10;  
         }  
        // fade from min to max in increments of 25.5 points: basically (0.5 volts)  
        for (float n = 0; n <= maxBright; n +=25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin,n);  
            delay(ms);  
        }  
    }  

     if(digitalRead(buttonBpin) == LOW)  
    {  
         if (counter <= 0)  
         {  
             counter = 0;  
        }  
        // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
        for (float n = maxBright; n >= 0; n -= 25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin, n);  
            delay(ms);  
        }  
    }  
}  

I am using an Arduino UNO and I am trying to vary the brightness by steps by 0.5 volts till it gets to a max of 5.0 V using buttonA and decrease the brightness of the LED by 0.5 volts with buttonB. The Problem I have is that when I push buttonA the LED steps it's voltage by 0.5V all the way to 5.0V and with buttonB its steps down to 0. I would like to have the LED step each time I press the button not all at once. Here is my code that I have so far can someone help me with telling the Arduino to Step each time a button is pressed. CODE IS DIFFERENT from another post I did

int ledPin = 9; // must be a PWM digital pin
int buttonApin = A2; //buttonA to analog 2
int buttonBpin = A1; // buttonB to analog 1
int maxBright = 255; // between 0 and 255
int ms = 1500;    // delay in milliseconds
int buttonPushCounterA = 0; //counter for the number of button presses
int buttonStateA = 0; // current state of the button
int lastButtonStateA = 0; // previous state of the button
int buttonPushCounterB = 0; //counter for the number of button presses
int buttonStateB = 0; // current state of the button
int lastButtonStateB = 0; // previous state of the button

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
}

void loop() 
{
  if (digitalRead(buttonApin) == LOW) //if buttonA is pushed
   {
      buttonStateA = digitalRead(buttonApin);

     if (buttonStateA != lastButtonStateA)
     {
        if(buttonStateA == LOW)
     {
        buttonPushCounterA++;
     }
    }

// fade from min to max in increments of 25.5 points: basically (0.5 volts)
for (float n = 0; n <= maxBright; n +=25.5) 
{
 // sets the value (range from 0 to 255): 
 analogWrite(ledPin,n);
 delay(ms);
}
  }

  if (digitalRead(buttonBpin) == LOW) //if buttonB is pushed
  {
     buttonStateB = digitalRead(buttonBpin);

       if (buttonStateB != lastButtonStateB)
         {
            if(buttonStateB == LOW)
              {
                buttonPushCounterB--;
              }
         }
 

     // fade from max to min in increments of 25.5 points: basically (0.5 volts)
     for (float n = maxBright; n >= 0; n -= 25.5) 
    {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, n);
     delay(ms);
    }
  }  

  lastButtonStateA = buttonStateA;
  lastButtonStateB = buttonStateB;
 }
added 182 characters in body
Source Link
jfpoilpret
  • 9.1k
  • 7
  • 37
  • 54
int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;  
int buttonBpin = 11;  
int maxBright = 255; // between 0 and 255  
int ms = 300;    // delay in milliseconds  
int counter = 0;  
  
void setup()   
{  
    pinMode(ledPin, OUTPUT);  
    pinMode(buttonApin, INPUT_PULLUP);  
    pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
    if (digitalRead(buttonApin) == LOW)  
    {  
        if (counter >= 10)  
        {  
            counter = 10;  
        }  
        // fade from min to max in increments of 25.5 points: basically (0.5 volts)  
        for (float n = 0; n <= maxBright; n +=25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin,n);  
            delay(ms);  
        }  
    }  

    if(digitalRead(buttonBpin) == LOW)  
    {  
        if (counter <= 0)  
        {  
            counter = 0;  
        }  
        // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
        for (float n = maxBright; n >= 0; n -= 25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin, n);  
            delay(ms);  
        }  
    }  
}  
int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;  
int buttonBpin = 11;  
int maxBright = 255; // between 0 and 255  
int ms = 300;    // delay in milliseconds  
int counter = 0;  
  
void setup()   
{  
pinMode(ledPin, OUTPUT);  
pinMode(buttonApin, INPUT_PULLUP);  
pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
  if (digitalRead(buttonApin) == LOW)  
  {  
  if (counter >= 10)  
  {  
   counter = 10;  
  }  
 // fade from min to max in increments of 25.5 points: basically (0.5 volts)  
 for (float n = 0; n <= maxBright; n +=25.5)   
 {  
  // sets the value (range from 0 to 255):  
  analogWrite(ledPin,n);  
  delay(ms);  
 }  
}  

 if(digitalRead(buttonBpin) == LOW)  
 {  
   if (counter <= 0)  
   {  
   counter = 0;  
   }  
   // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
   for (float n = maxBright; n >= 0; n -= 25.5)   
   {  
    // sets the value (range from 0 to 255):  
    analogWrite(ledPin, n);  
    delay(ms);  
   }  
 }  
}  
int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;  
int buttonBpin = 11;  
int maxBright = 255; // between 0 and 255  
int ms = 300;    // delay in milliseconds  
int counter = 0;  
  
void setup()   
{  
    pinMode(ledPin, OUTPUT);  
    pinMode(buttonApin, INPUT_PULLUP);  
    pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
    if (digitalRead(buttonApin) == LOW)  
    {  
        if (counter >= 10)  
        {  
            counter = 10;  
        }  
        // fade from min to max in increments of 25.5 points: basically (0.5 volts)  
        for (float n = 0; n <= maxBright; n +=25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin,n);  
            delay(ms);  
        }  
    }  

    if(digitalRead(buttonBpin) == LOW)  
    {  
        if (counter <= 0)  
        {  
            counter = 0;  
        }  
        // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
        for (float n = maxBright; n >= 0; n -= 25.5)   
        {  
            // sets the value (range from 0 to 255):  
            analogWrite(ledPin, n);  
            delay(ms);  
        }  
    }  
}  
Source Link

Arduino UNO button LED brightness vary

I am using an Arduino UNO and I am trying to vary the brightness by steps by 0.5 volts till it gets to a max of 5.0 V using buttonA and decrease the brightness of the LED by 0.5 volts with buttonB I believe that I am close the only Problem I have is that when I push buttonA the LED steps it's voltage all the way to 5.0V and with buttonB its steps down to 0. I would like to have the LED step each time I press the button not all at once. Here is my code that I have so far can someone help me with telling the Arduino to Step each time a button is pressed.

HERE is my code:

int ledPin = 9; // must be a PWM digital pin  
int buttonApin = 10;  
int buttonBpin = 11;  
int maxBright = 255; // between 0 and 255  
int ms = 300;    // delay in milliseconds  
int counter = 0;  
  
void setup()   
{  
pinMode(ledPin, OUTPUT);  
pinMode(buttonApin, INPUT_PULLUP);  
pinMode(buttonBpin, INPUT_PULLUP);  
}  

void loop()   
{  
  if (digitalRead(buttonApin) == LOW)  
  {  
  if (counter >= 10)  
  {  
   counter = 10;  
  }  
 // fade from min to max in increments of 25.5 points: basically (0.5 volts)  
 for (float n = 0; n <= maxBright; n +=25.5)   
 {  
  // sets the value (range from 0 to 255):  
  analogWrite(ledPin,n);  
  delay(ms);  
 }  
}  

 if(digitalRead(buttonBpin) == LOW)  
 {  
   if (counter <= 0)  
   {  
   counter = 0;  
   }  
   // fade from max to min in increments of 25.5 points: basically (0.5 volts)  
   for (float n = maxBright; n >= 0; n -= 25.5)   
   {  
    // sets the value (range from 0 to 255):  
    analogWrite(ledPin, n);  
    delay(ms);  
   }  
 }  
}