-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heather Simmons Project 1 Code
123 lines (102 loc) · 6.15 KB
/
Heather Simmons Project 1 Code
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//source for twinkle twinkle code is Arduino cookbook, exercise 9.2
//source for fade code is Instructables website, user maewert http://www.instructables.com/id/RGB-LED-transition-Arduino/
int red = 9; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 11; //this sets the blue led pin
int currentRed;//starting color level (0-255) for red color, whole number, so int
int currentBlue;//starting color level (0-255) for blue color
int currentGreen;//starting color level (0-255) for green color
int newRed;//new color level for red, this is the color the currentRed variable fades or brightens to
int newBlue;//same as immediately above, except for blue;
int newGreen;//same as immediately above, except for green;
int lightLevel;//holds reading of light levels from light dependent resistor ("LDR"), whole number from 0 to 1024, so int
float forceSensor;//holds reading of pressure exerted on force sensor; may have a decimal, so float.
const int speakerPin = 6; // connect speaker to pin 6
char noteNames[] = {'C','D','E','F','G','a','b'};//array or "group" of note names in twinkle twinkle
unsigned int frequencies[] = {262,294,330,349,392,440,494};//array of frequencies associated with each note, "unsigned" means positive values only
const int noteCount = sizeof(noteNames); // sizeof operator returns number of notes (7 here) in array "noteNames"
//notes, a space represents a rest
char score[] = "CCGGaaGFFEEDDC GGFFEEDGGFFEED CCGGaaGFFEEDDC "; //musical score for twinkle twinkle, "char" because it is letters, not numbers
const int scoreLen = sizeof(score); // sizeof operator returns the number of notes in the array "score
void setup()//one time set-up
{ //this sets the input and output pins
pinMode(red, OUTPUT);//sets red pin as an output
pinMode(green, OUTPUT);//sets green pin as an output
pinMode(blue, OUTPUT);//sets blue pin as an output
pinMode(lightLevel, INPUT);//sets LDR as an input
pinMode(forceSensor, INPUT);//sets force sensor as an input
currentRed = random(255);//sets red color saturation to a random number between 0 and 255
currentBlue = random(255);//sets blue color saturation to a random number between 0 and 255
currentGreen = random(255);//sets green color saturation to a random number between 0 and 255
newRed = currentRed;//sets new red value to current red in order to get started
newBlue = currentBlue;//same as above, for blue
newGreen = currentGreen;//same as above, for green
Serial.begin(9600);//initialize serial monitor, for debugging
randomSeed(analogRead(0));//initialize pseudo-random number generator by reading LDR value (range of possible numbers between 0 and 1024, so analog)
}
void playNote(char note, int duration)//function, requires two parameters, note and duration
{
// play the tone corresponding to the note name
for (int i = 0; i < noteCount; i++)
{
// try and find a match for the noteName to get the index to the note
if (noteNames[i] == note) // find a matching note name in the array
tone(speakerPin, frequencies[i], duration); // play the note
}
// if there is no match then the note is a rest, so just do the delay
delay(duration);
}
#define fade(x,y) if (x>y) x--; else if (x<y) x++;//function which takes two parameters, x and y (current color and new color), and either fades or brightens current color to new color
void loop()//continuous loop
{
forceSensor = analogRead(A2);//read pressure on force sensor, range of values, so analog
lightLevel=(analogRead(0));//read light level at LDR, range of values, so analog
//use serial monitor to read LDR and force sensor levels, and output RGB values - for debugging purposes
Serial.print("LDR reading = ");
Serial.println(lightLevel);
Serial.print("RGB values are ");
Serial.print(currentRed);
Serial.print(",");
Serial.print(currentGreen);
Serial.print(",");
Serial.println(currentBlue);
Serial.print("Force sensor reading = ");
Serial.println(forceSensor);
delay(1000);
if (lightLevel<500)//if the room is dark, turn on the nightlight
{
if(forceSensor>20)//if it is dark in the room AND the cat's nose HAS been pressed (i.e., this "if" is nested in the LDR "if") ...
{
for (int i = 0; i < scoreLen; i++)//loop through as many times as there are notes in the score - i.e., scorelen
{
int duration = 333; // each note lasts for a third of a second
playNote(score[i], duration); // play the note
}//end of for statement
}//end of nested if statement
analogWrite(blue, currentBlue);//set the blue output pin to the analog value randomly generated for it in setup (generated by Arduino's random function)
analogWrite(red, currentRed);//same as above, but for red
analogWrite(green, currentGreen);//same as above, but for green;
newRed = random(255);//pick a new random value between 0 and 255 for the red pin
newBlue = random(255);//same as above, for the blue pin
newGreen = random(255);//same as above, for the green pin
// fade or brighten to new colors
while ((currentRed != newRed) ||//as long as current red does not equal new red, or current blue does not equal new blue, or current green does not equal new green...
(currentBlue != newBlue) ||
(currentGreen != newGreen))
{
fade(currentRed,newRed);//fade or brighten current red color to new red color
fade(currentBlue,newBlue);//same as above, but for blue
fade(currentGreen,newGreen);//same as above, but for green
analogWrite(red, currentRed);//make the new red color the "current" red color so we can loop through the program again and fade or brighten to a new random color
analogWrite(blue, currentBlue);//same as above, but for blue
analogWrite(green, currentGreen);//same as above, but for green
delay(20);//appropriate delay to produce "slow fade" effect
}//end of while statement
}//end of if statement
else//If the room light is bright, turn the nightlight off. The cat's nose also will not play the tune while the room light is bright.
{
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 0);
}//end of else statement
}//end of void loop