Back to Parent

//Early Exploration of LED Patterns
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 inputPin = A0;

//neopixel
#include <Adafruit_NeoPixel.h> //include the neopixel library
#define PIXEL_PIN 6
#define NUMPIXELS 12

Adafruit_NeoPixel ring = Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800); //initialize neopixel object
uint32_t ringColor;
uint32_t blank = ring.Color(0,0,0);
int r=25;
int g=0;
int b=25;
int maxLEDS=0;

int lightState[]={1,0,0,0,0,0,0,0,0,0,0,0};
int minPos=0;
int LEDSpeed = 20;
int timer=0;

void setup(){
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
    //Initialize Ring
    ring.begin();
    ring.setBrightness(50);
    ring.show();
    Serial.begin(9600);
}
void loop(){
  //Get input signal
  int val = cleanSignal();
  timer++;
  Serial.print("raw: ");
  Serial.print(analogRead(inputPin));
  Serial.print("; avg: ");
  Serial.print(val);
  Serial.print("; timer: ");
  Serial.println(timer);
  delay(1);        // delay in between reads for stability  
  ringPattern(val);
}


void ringPattern(int micVal){
  g = map(micVal,33,1023,0,255);
  maxLEDS = map(micVal,33,1023,0,NUMPIXELS);
  LEDSpeed = map(micVal,33,1023,40,100);
  ringColor = ring.Color(r,g,b);
  
  //updatePixels
  minPos++;
  minPos = minPos%NUMPIXELS;
  for(int i=0;i<NUMPIXELS;i++)
  {
    if(i>=minPos && i<minPos+maxLEDS){lightState[i]=1;}
    else{lightState[i]=0;}
  }
  //drawPixels
  for(int i=0;i<NUMPIXELS;i++)
  {
    ring.setPixelColor(i,blank);
    if(lightState[i]>0){
      ring.setPixelColor(i,ringColor);
    }
  }
  ring.show();
  delay(LEDSpeed);
}

int cleanSignal(){
    // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // 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!

0