#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define ledPIN 6
//Which pin is input for the switch
const int pinOFswitch = 8;
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 1
//define the Mic/sound sensor on which analog pin
#define MIC A0
int a; //varibale to store analog read of sound sensor level
int ledBrightnessStrip = 0; //declare and set to initial value of 0
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals.
Adafruit_NeoPixel pixels(NUMPIXELS, ledPIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 1000 // Time (in milliseconds) to pause between color changes when sound sensor not used
float col[3];
float hue =0;
float fract(float x) { return x - int(x); }
float mix(float a, float b, float t) { return a + (b - a) * t; }
float* hsv2rgb(float h, float s, float b, float* rgb)
{
rgb[0] = b * mix(1.0, constrain(abs(fract(h + 1.0) * 6.0 - 3.0) - 1.0, 0.0, 1.0), s);
rgb[1] = b * mix(1.0, constrain(abs(fract(h + 0.6666666) * 6.0 - 3.0) - 1.0, 0.0, 1.0), s);
rgb[2] = b * mix(1.0, constrain(abs(fract(h + 0.3333333) * 6.0 - 3.0) - 1.0, 0.0, 1.0), s);
return rgb;
}
//set color names
//pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
uint32_t blue = pixels.Color(0, 0, 255);
uint32_t red = pixels.Color(255, 0, 0);
uint32_t purple = pixels.Color(255, 0, 255);
uint32_t yellow = pixels.Color(255, 255, 0);
uint32_t green = pixels.Color(0, 150, 0);
uint32_t cyan = pixels.Color(0, 200, 220);
//SETUP <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
void setup()
{
pixels.begin(); // INITIALIZE NeoPixel object (REQUIRED)
//Set pin mode of the switch to Pull Up
pinMode(pinOFswitch, INPUT_PULLUP);
//setup the sound sensor
Serial.begin(9600);
delay(1000);
//set pinmode of the sound sensor to 'input'
pinMode(MIC, INPUT);
// pixels.setPixelColor(0, 255, 0, 0);
// pixels.setBrightness(100); //turn the brightness down so it can brighten with sound volume
// pixels.show();
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
void loop() {
pixels.clear(); // Set all pixel colors to 'off'
int ValueOFswitch;
ValueOFswitch = digitalRead(pinOFswitch);
if (ValueOFswitch == LOW)
{
pixels.setBrightness(200); //if not using the sound sensor, make the LED brighter
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
// Red:
pixels.setPixelColor(i, red);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL);
// Blue:
pixels.setPixelColor(i, blue);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL);
// Purple:
pixels.setPixelColor(i, purple);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL);
// Green:
pixels.setPixelColor(i, green);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL);
// Yellow:
pixels.setPixelColor(i, yellow);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL);
// Cyan:
pixels.setPixelColor(i, cyan);
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL);
// Template to add another color
// Color:
// pixels.setPixelColor(i, cyan);
// pixels.show(); // Send the updated pixel colors to the hardware.
// delay(DELAYVAL);
}}
else //do this if the switch reads HIGH
{
//pixels.clear(); // Set all pixel colors to 'off'
//pixels.show(); // Send the updated pixel colors to the hardware.
//test mic input and writing out to logger
// int sample = analogRead(A0);
// Serial.println(sample);
// digitalWrite(13, abs(sample - 512) > 50);
//---This section is for the sound sensor------------------------------
pixels.setPixelColor(0, 255, 0, 0);
pixels.setBrightness(100); //turn the brightness down so it can brighten with sound volume
pixels.show();
delay(10);
a = analogRead(MIC);
Serial.println(a);
if (a > 500) {
ledBrightnessStrip=255;
}
ledBrightnessStrip=ledBrightnessStrip*0.95;
if (ledBrightnessStrip<0) {
ledBrightnessStrip=0;
}
hue += 0.0005;
if (hue >= 1.0) hue = 0.0;
hsv2rgb(hue, 1.0, 1.0, col);
pixels.setBrightness(ledBrightnessStrip);
pixels.setPixelColor(0, (int)(col[0]*255), (int)(col[1]*255), (int)(col[2]*255));
pixels.show();
// ---------------------------------------------------------------------
}
}