Back to Parent

#include <Servo.h>

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 = A2;              //mic input pin
int ledPin = 6;
int angle = 0;                      //initial position of servo
int threshold= 40;           
Servo myservo;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);     
  pinMode(ledPin, OUTPUT);   
  pinMode(A2, INPUT);           
  myservo.attach(9); 
  myservo.write(0) ;

  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
}

void loop() {
 
  total= total - readings[index];         
  readings[index] = analogRead(A2); 
  total= total + readings[index];        
  index = index + 1;                    

  if (index >= numReadings){           

    index = 0;}                           
    
  average = total / numReadings;         
  
  int newAvg = map(average, 0 , 1023, 0, 255);
  
    
  Serial.println(newAvg);   
  delay(15);  
  
if (newAvg>threshold){
    myservo.write(180);
    delay(10000);
    myservo.write(0);
    delay(100000);
    newAvg = 0;
  }
    

  analogWrite(ledPin, newAvg);
  
            
}
Click to Expand

Content Rating

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

0