-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson-7.ino
90 lines (64 loc) · 2.49 KB
/
Lesson-7.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**************************************
Lesson 7: Using Conditionals: Momentary Modes
Additional materials
- 1 10K ohm resistors
- 1 momentary button (https://www.adafruit.com/product/1119)
**************************************/
int ledPin = LED_BUILTIN; // same as pin 13
int knobPin = A0; // analog pin because range of numbers
int knobValue = 0; // variable for reading the knob
int switchPin = 7; // digital pin because binary (on/off)
int switchState = 0; // variable for reading the switch
int buttonPin = 2; // digital pin because binary (on/off)
int buttonState = 0; // variable for reading the button
void setup() {
// put your setup code here, which runs once
// set digital pin as output
pinMode(ledPin, OUTPUT);
// set digital pins as inputs:
pinMode(switchPin, INPUT);
pinMode(buttonPin, INPUT);
// start serial communications at 9600 baud
Serial.begin(9600);
}
void loop() {
// main program goes here
buttonState = digitalRead(buttonPin); // check if button is pressed
Serial.print("Button: ");
Serial.println(buttonState);
if (buttonState == HIGH) {
// button pressed
digitalWrite(ledPin, HIGH); // turn the ledPin on
} else {
// button not pressed
// so check if the switch is switched on
switchState = digitalRead(switchPin);
Serial.print("Switch: ");
Serial.println(switchState);
if (switchState == 1) {
// switch is on so read the value of the knob
knobValue = analogRead(knobPin);
if (knobValue < 300) {
// switch is on and knobValue is less than 300
knobValue = analogRead(knobPin);
digitalWrite(ledPin, HIGH); // turn the ledPin on
Serial.print("On, Knob=");
Serial.println(knobValue);
delay(knobValue); // stop the program for <knobValue> ms
knobValue = analogRead(knobPin);
digitalWrite(ledPin, LOW); // turn the ledPin off
Serial.print("Off, Knob=");
Serial.println(knobValue);
delay(knobValue); // stop the program for <knobValue> ms
} else {
// switch is on and knobValue is 300 or greater
digitalWrite(ledPin, HIGH); // turn the ledPin on
Serial.print("On, Knob=");
Serial.println(knobValue);
} // end of knobValue if/else
} else {
// switch is off
digitalWrite(ledPin, LOW); // turn the ledPin off
} // end of switchState if/else
} // end of buttonState logic
}