-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt_led_dimmer.ino
48 lines (41 loc) · 986 Bytes
/
bt_led_dimmer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const led = 11;
int brightness;
String userInput;
void onclick_on() // LED fade-in for "on" button
{
for(int up=0; up<255; up++){
analogWrite(led, up);
delay(30);
}
userInput = "255"; // fix the value to avoid looping
}
void onclick_off() // LED fade-out for "off" button
{
for(int down=255; down>=0; down--){
analogWrite(led, down);
delay(30);
}
userInput = "0";
}
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);
delay(10);
Serial.println("Connected.");
}
void loop() {
if(Serial.available())
{
userInput = Serial.readStringUntil('\n');
Serial.println("In: " + userInput);
brightness = userInput.toInt();
Serial.print("Out: ");
Serial.println(brightness);
if(userInput=="on") onclick_on();
else if(userInput=="off") onclick_off();
else{
analogWrite(led, brightness);
}
}
}