11/*
2- SparkFun Inventor’s Kit
3- Circuit 1D-RGB Nightlight
2+ SparkFun Inventor’s Kit
3+ Circuit 1D-RGB Nightlight
44
5- Turns an RGB LED on or off based on the light level read by a photoresistor.
6- Change colors by turning the potentiometer.
5+ Turns an RGB LED on or off based on the light level read by a photoresistor.
6+ Change colors by turning the potentiometer.
77
8- This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
9- This code is completely free for any use.
8+ This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
9+ This code is completely free for any use.
1010
11- View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
12- Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
13- */
11+ View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
12+ Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
13+ */
1414
1515int photoresistor = A0; // variable for storing the photoresistor value
1616int potentiometer = A1; // this variable will hold a value based on the position of the knob
@@ -25,103 +25,103 @@ void setup() {
2525 Serial.begin (9600 ); // start a serial connection with the computer
2626
2727 // set the LED pins to output
28- pinMode (RedPin,OUTPUT);
29- pinMode (GreenPin,OUTPUT);
30- pinMode (BluePin,OUTPUT);
28+ pinMode (RedPin, OUTPUT);
29+ pinMode (GreenPin, OUTPUT);
30+ pinMode (BluePin, OUTPUT);
3131}
3232
3333void loop () {
34-
34+
3535 photoresistor = analogRead (A0); // read the value of the photoresistor
3636 potentiometer = analogRead (A1);
3737
3838 Serial.print (" Photoresistor value:" );
3939 Serial.print (photoresistor); // print the photoresistor value to the serial monitor
4040 Serial.print (" Potentiometer value:" );
4141 Serial.println (potentiometer); // print the potentiometer value to the serial monitor
42-
43- if (photoresistor < threshold){ // if it's dark (the photoresistor value is below the threshold) turn the LED on
44- // These nested if statements check for a variety of ranges and
42+
43+ if (photoresistor < threshold) { // if it's dark (the photoresistor value is below the threshold) turn the LED on
44+ // These nested if statements check for a variety of ranges and
4545 // call different functions based on the current potentiometer value.
46- // Those functions are found at the bottom of the sketch.
47- if (potentiometer > 0 && potentiometer <= 150 )
46+ // Those functions are found at the bottom of the sketch.
47+ if (potentiometer > 0 && potentiometer <= 150 )
4848 red ();
49- if (potentiometer > 150 && potentiometer <= 300 )
49+ if (potentiometer > 150 && potentiometer <= 300 )
5050 orange ();
51- if (potentiometer > 300 && potentiometer <= 450 )
52- yellow ();
53- if (potentiometer > 450 && potentiometer <= 600 )
51+ if (potentiometer > 300 && potentiometer <= 450 )
52+ yellow ();
53+ if (potentiometer > 450 && potentiometer <= 600 )
5454 green ();
55- if (potentiometer > 600 && potentiometer <= 750 )
55+ if (potentiometer > 600 && potentiometer <= 750 )
5656 cyan ();
57- if (potentiometer > 750 && potentiometer <= 900 )
58- blue ();
59- if (potentiometer > 900 )
60- magenta ();
61- }
57+ if (potentiometer > 750 && potentiometer <= 900 )
58+ blue ();
59+ if (potentiometer > 900 )
60+ magenta ();
61+ }
6262 else { // if it isn't dark turn the LED off
63-
63+
6464 turnOff (); // call the turn off function
65-
66- }
67-
65+
66+ }
67+
6868 delay (100 ); // short delay so that the printout is easier to read
6969}
7070
71- void red (){
72-
73- // set the LED pins to values that make red
74- analogWrite (RedPin, 100 );
75- analogWrite (GreenPin, 0 );
76- analogWrite (BluePin, 0 );
71+ void red () {
72+
73+ // set the LED pins to values that make red
74+ analogWrite (RedPin, 100 );
75+ analogWrite (GreenPin, 0 );
76+ analogWrite (BluePin, 0 );
7777}
78- void orange (){
79-
80- // set the LED pins to values that make orange
81- analogWrite (RedPin, 100 );
82- analogWrite (GreenPin, 50 );
83- analogWrite (BluePin, 0 );
78+ void orange () {
79+
80+ // set the LED pins to values that make orange
81+ analogWrite (RedPin, 100 );
82+ analogWrite (GreenPin, 50 );
83+ analogWrite (BluePin, 0 );
8484}
85- void yellow (){
86-
87- // set the LED pins to values that make yellow
88- analogWrite (RedPin, 100 );
89- analogWrite (GreenPin, 100 );
90- analogWrite (BluePin, 0 );
85+ void yellow () {
86+
87+ // set the LED pins to values that make yellow
88+ analogWrite (RedPin, 100 );
89+ analogWrite (GreenPin, 100 );
90+ analogWrite (BluePin, 0 );
9191}
92- void green (){
93-
94- // set the LED pins to values that make green
95- analogWrite (RedPin, 0 );
96- analogWrite (GreenPin, 100 );
97- analogWrite (BluePin, 0 );
92+ void green () {
93+
94+ // set the LED pins to values that make green
95+ analogWrite (RedPin, 0 );
96+ analogWrite (GreenPin, 100 );
97+ analogWrite (BluePin, 0 );
9898}
99- void cyan (){
100-
101- // set the LED pins to values that make cyan
102- analogWrite (RedPin, 0 );
103- analogWrite (GreenPin, 100 );
104- analogWrite (BluePin, 100 );
99+ void cyan () {
100+
101+ // set the LED pins to values that make cyan
102+ analogWrite (RedPin, 0 );
103+ analogWrite (GreenPin, 100 );
104+ analogWrite (BluePin, 100 );
105105}
106- void blue (){
107-
108- // set the LED pins to values that make blue
109- analogWrite (RedPin, 0 );
110- analogWrite (GreenPin, 0 );
111- analogWrite (BluePin, 100 );
106+ void blue () {
107+
108+ // set the LED pins to values that make blue
109+ analogWrite (RedPin, 0 );
110+ analogWrite (GreenPin, 0 );
111+ analogWrite (BluePin, 100 );
112112}
113- void magenta (){
114-
115- // set the LED pins to values that make magenta
116- analogWrite (RedPin, 100 );
117- analogWrite (GreenPin, 0 );
118- analogWrite (BluePin, 100 );
113+ void magenta () {
114+
115+ // set the LED pins to values that make magenta
116+ analogWrite (RedPin, 100 );
117+ analogWrite (GreenPin, 0 );
118+ analogWrite (BluePin, 100 );
119119}
120- void turnOff (){
121-
122- // set all three LED pins to 0 or OFF
123- analogWrite (RedPin, 0 );
124- analogWrite (GreenPin, 0 );
125- analogWrite (BluePin, 0 );
120+ void turnOff () {
121+
122+ // set all three LED pins to 0 or OFF
123+ analogWrite (RedPin, 0 );
124+ analogWrite (GreenPin, 0 );
125+ analogWrite (BluePin, 0 );
126126}
127127
0 commit comments