-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pushbutton_dimmable_LEDs_29.cpp
64 lines (51 loc) · 1.58 KB
/
Pushbutton_dimmable_LEDs_29.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
int buttonPin1=12;
int buttonPin2=11;
int LEDPin=3;
int buzzPin=2;
int buttonVal1;
int buttonVal2;
int LEDbright=0;
int dt=500;
void setup(){
pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(LEDPin,OUTPUT);
pinMode(buzzPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonVal1 = digitalRead(buttonPin1);
buttonVal2 = digitalRead(buttonPin2);
Serial.print("Button 1 = ");
Serial.print(buttonVal1);
Serial.print(", ");
Serial.print("Button 2 = ");
Serial.println(buttonVal2);
delay(dt);
if (buttonVal1 == 0) { // if the button is down, it will increase the brightness.
LEDbright=LEDbright+5; // it seems he can just hold it or repeatedly click it to brighten it.
}
if (buttonVal2 == 0) {
LEDbright=LEDbright-5;
}
Serial.println(LEDbright);
if (LEDbright>255) { // so you don't exceed 255
LEDbright=255;
digitalWrite(buzzPin, HIGH); // if you turn it on ...
delay(dt);
digitalWrite(buzzPin, LOW); //... you'll need to turn it off.
Serial.println("Buzz HIGH"); // to test if you're in the if statements
}
if (LEDbright < 0){ // so you don't exceed 0
LEDbright = 0;
digitalWrite(buzzPin, HIGH);
delay(dt);
digitalWrite(buzzPin, LOW);
Serial.println("Buzz LOW"); // to test if you're in the if statements
}
analogWrite(LEDPin, LEDbright); // not sure what this is doing here ...
// *might* be just to turn the LED on in analog (smoothly).
}
// important for the last lesson: the "off" position automatically produces a
// read-out of "0" and "1" for "on.""
// This is the huge missing piece from 1/17 lesson.