* Since, I did not have a group, I worked by myself *
Next process that I started to work on was to work on the Arduino coding that controls the output behaviours, using values from input sensor (sound sensor).
Here is my code for the project ********
/*
WhistLED by Jonggen Yoo (Andy) - 301025115
For IAT320 - Sketch two
Inputs used::
1 x Analog input for electret microphone as sound sensor
3 x Analog ouputs for LEDs
How the coding works::
Reads the sensor value from analog input and converts(maps) the values to
range from 0 to 255. And then, using if statement, the code executes.
There are four conditions, depending on mapped outputValue
65 to 80 - delay 25
81 to 135 - delay 15
136 to 204 - delay 8
205 to 255 - delay 3
And the code prints out read value from sensor and output value after mapping
ont the serial monitor.
*/
// Analog input, connected to the microphone
int analogPin = 0;
// initializing sensorValue (read from the microphone) and
// outputValue (after mapping sensorValue).
int sensorValue = 0;
int outputValue = 0;
// declaring PWM output channels for LEDs
int pinLED3 = 3;
int pinLED5 = 5;
int pinLED6 = 6;
//------------------------------------------------------------------ void setup :: begins
void setup() {
// initializing serial communication at 9600 bps.
Serial.begin(9600);
}
//------------------------------------------------------------------ void setup :: ends
//------------------------------------------------------------------ void loop :: begins
void loop() {
// reading values from analogPin (analog input) and assign value to sensorValue.
sensorValue = analogRead(analogPin);
// mapping value of sensorValue, range from 0 to 255.
outputValue = map(sensorValue, 0, 1023, 0, 255);
//----------------------------------------------------------- Four if statements :: begins
/*
Fade in-out effect on LEDs
Each statement has different delay rate between each channel output.
To create more relevant movement of light that resembles the movement of the sound
*/
//---------------------------------------------------------------- value between 75 - 80
if (outputValue >= 75 && outputValue <= 80){
// For value between 75 - 80 :: LED3
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED3, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED3, fadeValue);
// wait for 25 milliseconds to see the dimming effect
delay(25);
}
// For value between 75 - 80 :: LED5
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED5, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED5, fadeValue);
// wait for 25 milliseconds to see the dimming effect
delay(25);
}
// For value between 75 - 80 :: LED6
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED6, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED6, fadeValue);
// wait for 25 milliseconds to see the dimming effect
delay(25);
}
}
//---------------------------------------------------------------- value between 50 - 70
//---------------------------------------------------------------- value between 81 - 135
else if (outputValue >= 81 && outputValue <= 135){
// For value between 81 - 135 :: LED3
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED3, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED3, fadeValue);
// wait for 15 milliseconds to see the dimming effect
delay(15);
}
// For value between 81 - 135 :: LED5
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED5, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED5, fadeValue);
// wait for 15 milliseconds to see the dimming effect
delay(15);
}
// For value between 81 - 135 :: LED6
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED6, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED6, fadeValue);
// wait for 15 milliseconds to see the dimming effect
delay(15);
}
}
//---------------------------------------------------------------- value between 71 - 135
//---------------------------------------------------------------- value between 136 - 204
else if (outputValue >= 136 && outputValue <= 204){
// For value between 136 - 204 :: LED3
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED3, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED3, fadeValue);
// wait for 8 milliseconds to see the dimming effect
delay(8);
}
// For value between 136 - 204 :: LED5
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED5, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED5, fadeValue);
// wait for 8 milliseconds to see the dimming effect
delay(8);
}
// For value between 136 - 204 :: LED6
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED6, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED6, fadeValue);
// wait for 8 milliseconds to see the dimming effect
delay(8);
}
}
//---------------------------------------------------------------- value between 136 - 204
//---------------------------------------------------------------- value between 205 - 255
else if (outputValue >= 205 && outputValue <= 255){
// For value between 205 - 255 :: LED3
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED3, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED3, fadeValue);
// wait for 2 milliseconds to see the dimming effect
delay(2);
}
// For value between 205 - 255 :: LED5
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED5, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED5, fadeValue);
// wait for 2 milliseconds to see the dimming effect
delay(2);
}
// For value between 205 - 255 :: LED6
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(pinLED6, fadeValue);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(pinLED6, fadeValue);
// wait for 2 milliseconds to see the dimming effect
delay(2);
}
}
//---------------------------------------------------------------- value between 205 - 255
//----------------------------------------------------------- Four if statements :: ends
// prints out result in serial monitor (sensorValue + outputValue).
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(100);
}
//------------------------
Using four if statements, participants can experience four different behaviours, depending on the value sensor senses.
Also, most of the code involved examples from Arudino such as Fade in/out, mapping, and AnalogInOutSerial.
** On Nov. 17th - 27th 2010 **
댓글 없음:
댓글 쓰기