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;
}
}
}
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;
}
}
}