Back to Parent

#include <neopixel.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 24
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

const int sampleWindow = 50;
int electretPin = A5;
int RelayPin = D4;
long startTime = 0;
long timeOut = 10*1000;

void setup() {
Serial.begin (9600);
strip.begin();
strip.show();
pinMode(RelayPin, OUTPUT);
startTime = millis(); }

void loop() {
double v = getPeakToPeakValue();
int vpercent = (int)(v * 100 / 3.3);
int b = map( vpercent, 0, 100, -100, 255);
if (b < 0) { b = 0; }
uint16_t i;
uint32_t c = strip.Color(b, 0, b);	
for (i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, c );
strip.show();
delay( 20 ); }
long tNow = millis();
if (tNow > startTime + timeOut) {
startTime = tNow;
digitalWrite(RelayPin, HIGH);
delay(100);
digitalWrite(RelayPin, LOW); }
Serial.println (b); }
double getPeakToPeakValue() {
unsigned int sample;
unsigned long startMillis= millis(); 
unsigned int peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 4096;
while (millis() - startMillis < sampleWindow){
sample = analogRead( electretPin );
if (sample < 4096) {
if (sample > signalMax) {
signalMax = sample; }
else if (sample < signalMin) {
signalMin = sample; }}}
peakToPeak = signalMax - signalMin; 
double volts = (peakToPeak * 3.3) / 4096;
Serial.println(volts);
return volts; }
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0