How to control led brightness with one keyboard button

So the idea is to use only one button to control the brightness of led. I was trying to use 'a' to control the brightness from 0 to 200. For example if I use pin 5 and 10, by pressing 'a' once, the brightness of pin5 would be 5, pin10 still 0. And then if i press it twice pin5 would get brighter and pin10 will start from brightness 5. Which eventually, I could make use the whole keyboard to control the brightness of several leds gradually growing from 0 to max but not simultaneously. I'm totally new using this device so please if you have any idea that could help feel free to comment. I'm using uno R3 wtich mac

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
pinMode(5,OUTPUT);
pinMode(10,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
if(Serial .available()){
  char a = Serial.read();

  switch(a){
    case'a' :
    analogWrite(10, 0);
    analogWrite(5, 0);
    break; 

    case'a' :
    analogWrite(10, 50);
    analogWrite(5, 50);
    break; 

   case'a' :
    analogWrite(10, 100);
    analogWrite(5, 100);
    break; 

   
}
}
}

Hi, @junghun0505
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

What model Arduino are you using?
Make sure the pins you use are capable of PWM.

Have you run your code?
What does it do?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The switch - case structure checks for the first match.
If you read an "a" from the keyboard it will always find

you are building a "state machine" - specifically a "sequential state machine"
I found a simple example here that shows one way to do this -

and I've copied the code here: the loop uses a variable "state" 
to keep track of which is the current state; 
and each time a match is found the "state" variable is incremented.
/*
* Traffic lights example
*
* Red light to pin 7
* Yellow light to pin 6
* Green light to pin 5
*
* Arduino IDE 1.6.12
*/
// Pins
int red = 7, yellow = 6, green = 5;

// System variables
byte state = 0; // initial state

void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}

void loop() {

switch(state){
case 0:
digitalWrite(red, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
delay(10000);
state = 1;
break;
case 1:
digitalWrite(red, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
delay(3000);
state = 2;
break;
case 2:
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(8000);
state = 3;
break;

case 3:
digitalWrite(red, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
delay(2000);
state = 0;
break;

default:
break;
}  //state
}  // loop

You should check out PWM (pulse width modulation) for controlling the brightness of the LED. Have look at https://www.arduino.cc/en/Tutorial/Foundations/PWM
This also shows where to find examples within the Arduino IDE.

Also.. your case statement won't do what you want. The first 'a' check will be true.. and exit the switch statement (break). Next time through the same 'a' check will be true... etc.

Hi, @junghun0505
Try this edit of your code.
Your original would not compile because of case a entered twice.
Do you have the UNO and LEDs and resistor to try it.
This code only increases and decreases the brightness of the Pin5 LED but you should be able to see the basic requirements and variables.

byte LED1Pin = 5;
byte LED2Pin = 10;
int LED1Level = 0;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED1Pin, OUTPUT);
  pinMode(LED2Pin, OUTPUT);
  Serial.println(" Press a <return> to increase brightness, Press b <return> to decrease brightness.");
}

void loop()
{
  // put your main code here, to run repeatedly:
  if (Serial .available())
  {
    char a = Serial.read();
    switch (a)
    {
      case'a' :
        LED1Level = LED1Level + 5;  //step up brightness in steps of 5
        if (LED1Level > 255)      // checks if brightness is > 255
        {
          LED1Level = 255;
        }
        analogWrite(LED1Pin, LED1Level);
        Serial.print(" LED1 Level = ");
        Serial.println(LED1Level);
        break;
      case'b' :
        LED1Level = LED1Level - 5;  //step down brightness in steps of 5
        if (LED1Level < 0)     // checks if brightness is < 0
        {
          LED1Level = 0;
        }
        analogWrite(LED1Pin, LED1Level);
        Serial.print(" LED1 Level = ");
        Serial.println(LED1Level);
        break;
      default:
        // statements if key is neither
        Serial.println(" Press a <return> to increase brightness, Press b <return> to decrease brightness.");
        break;
    }
  }
}

Tom... :smiley: :+1: :coffee: :australia:

jesus thank you all you guys!!! I'll try each one of them and see what suits!! thank you again everyone :innocent:

thank you I'll try this one out and see if i could use it
Thank you!!! :+1:

Thx!! I'll check it out and see how this thing works. Seems arduino aint that easy as I've heard :rofl:

I've just tried it out and I think I could use this code!! Thank you so much you guys saved my life :innocent:

Hi,
If you understand the structure, adding another LED will be straight forward.

It is pretty basic code, there are shorter more efficient ways, but if you are new to Arduino, starting with this code should get you going.

As well as using Serial.print to give some feedback form your code.

Tom... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.