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...
![]()