-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson-6.ino
More file actions
74 lines (51 loc) · 2.02 KB
/
Copy pathLesson-6.ino
File metadata and controls
74 lines (51 loc) · 2.02 KB
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
/**************************************
Lesson 6: Using Conditionals: Switch between modes
Additional materials
- 1 10K ohm resistors
- 1 switch (https://www.adafruit.com/product/805)
**************************************/
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
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);
// start serial communications at 9600 baud
Serial.begin(9600);
}
void loop() {
// main program goes here
//switchState = digitalRead(switchPin); // check if switch is on
Serial.print("Switch: ");
Serial.println(switchState);
if (digitalRead(switchPin) == 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
}