//***********************
//Audio+Smoothing
//***********************
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int micPin = A0;
int micVal;
int micMin = 33;
int micMax = 500;
//**************
//NeoPixel
//**************
#include <Adafruit_NeoPixel.h> //include the neopixel library
//declarations
#define PIXEL_PIN 6
#define NUMPIXELS 12
Adafruit_NeoPixel ring = Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800); //initialize neopixel object
//colors
uint32_t ringColor;
//Yellow(255,11,78)
//pink(255,133,152)
int yelr=255;int yelg=11;int yelb=78;
int pinkr=255;int pinkg=133;int pinkb=152;
//deepBlue(16,91,99)
//purple(51,37,50)
int deepr=16;int deepg=91;int deepb=91;
int purpr=51;int purpg=37;int purpb=50;
//******************
//Servo Motor
//******************
#include <Servo.h>
Servo myServo;
int servoPin = 10;
int servoPos = 0;
int minPos = 0;
int maxPos = 60;
//******************
//State Logic
//******************
int micThres = 200;//800
boolean closed = false;
boolean servoOn = false;
float counter = 0;
float countdown = 500;
void setup() {
// put your setup code here, to run once:
//Initialize Servo
myServo.attach(servoPin);
//Initialize NeoPixel
ring.begin();
ring.setBrightness(80);
ring.show();
Serial.begin(9600);
myServo.write(maxPos);
}
void loop() {
//smoothing
micVal = smoothing();
Serial.print("Mic: ");
Serial.println(micVal);
delay(1); // delay in between reads for stability
updateState();
updatePos();
updatePixels();
}
//Servo Motion
void updatePos() {
servoPos = map(counter, 0, countdown, maxPos, minPos);
servoPos = constrain(servoPos, minPos, maxPos);
myServo.write(servoPos);
Serial.print("Pos: ");
Serial.println(servoPos);
delay(15);
}
//neoPixel Update
void updatePixels() {
int r = 0;int g = 0;int b = 0;
int minr = 0;int ming = 0;int minb = 0;
int maxr = 0;int maxg = 0;int maxb = 0;
if(closed){
minr=purpr;ming=purpg;minb=purpb;
maxr=deepr;maxg=deepg;maxb=deepb;
}
else{
minr=yelr;ming=yelg;minb=yelb;
maxr=pinkr;maxg=pinkg;maxb=pinkb;
}
r = map(micVal, micMin, micMax, minr, maxr);
g = map(micVal, micMin, micMax, ming, maxg);
b = map(micVal, micMin, micMax, minb, maxb);
ringColor = ring.Color(r, g, b);
for (int i = 0; i < NUMPIXELS; i++)
{
ring.setPixelColor(i, ringColor);
}
ring.show();
}
void updateState(){
if(!servoOn){
if(micVal>micThres){closed = true;counter=countdown;}
else{closed = false;counter=0;}
}
if(counter>0){counter--;servoOn=true;}
else{servoOn = false;counter=0;}
// Serial.print("close:");
// Serial.print(closed);
// Serial.print("; time:");
// Serial.println(counter);
}
//sensorReading
int smoothing() {
// subtract the last reading:
total = total - readings[index];
// read from the sensor:
readings[index] = analogRead(micPin);
// add the reading to the total:
total = total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
return average;
}
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. .