#include <Servo.h>
// servos
Servo flwr1;
// servo positions
int startleCount = 0;
int blueCount = 0;
// RGB LEDs on PWN pins
const int flwrLED1R = 6;
const int flwrLED1G = 5;
const int flwrLED1B = 3;
const int flwrLED2R = 11;
const int flwrLED2G = 10;
const int flwrLED2B = 9;
// microphone
const int micPin = A0;
// smoothing
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int mapAvg = 0;
void setup() {
// attach servos to pins
flwr1.attach(2);
flwr1.write(0);
// initialize LEDs to white
pinMode(flwrLED1R, OUTPUT);
analogWrite(flwrLED1R, 0);
pinMode(flwrLED1G, OUTPUT);
analogWrite(flwrLED1G, 0);
pinMode(flwrLED1B, OUTPUT);
analogWrite(flwrLED1B, 0);
pinMode(flwrLED2R, OUTPUT);
analogWrite(flwrLED2R, 0);
pinMode(flwrLED2G, OUTPUT);
analogWrite(flwrLED2G, 0);
pinMode(flwrLED2B, OUTPUT);
analogWrite(flwrLED2B, 0);
// initialize microphone
pinMode(micPin, INPUT);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(micPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings)
// ...wrap around to the beginning:
readIndex = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
// map to values
mapAvg = map(average, 0, 1023, 0, 255);
// react to door opening or door slam
if (average <= 200) {
// flwr1
flwr1Color(255,0,0);
// flwr2
flwr2Color(0,255,0);
// remove echo
delay(175);
startleCount = 0;
} else {
// flwr1
flwr1Color(0,0,255);
//flwr2
flwr2Color(0,255,255);
if (startleCount != 1) {
for (int i=0; i<=180; i+=1) {
flwr1.write(i);
delay(1);
}
delay(1000);
flwr1.write(170);
delay(150);
flwr1.write(180);
delay(125);
flwr1.write(170);
delay(125);
flwr1.write(180);
delay(125);
flwr1.write(170);
delay(125);
flwr1.write(180);
delay(125);
for (int i=180; i>=0; i-=1) {
flwr1.write(i);
delay(20);
}
// reset
startleCount = 1;
}
}
}
// control LED color
void flwr1Color (int r, int g, int b) {
analogWrite(flwrLED1R, r);
analogWrite(flwrLED1G, g);
analogWrite(flwrLED1B, b);
}
void flwr2Color (int r, int g, int b) {
analogWrite(flwrLED2R, r);
analogWrite(flwrLED2G, g);
analogWrite(flwrLED2B, b);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .