So I am working on a project for school and I'm making an alarm clock device that detects noise and performs a function. I have an electret microphone that detects the noise but the problem I'm having right now is that I can't use two PWM pins. I can have the electret microphone make a servo move and I can also make the microphone make an led fade from dim to bright, but sadly I can't do them together. Once again I'm new to all this so any help will be greatly appreciated.
int knockSensor = 0;
int sensorReading = 0;
int threshold = 400;
int ledPin = 9;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
pinMode(ledPin, OUTPUT);
myservo.attach(10); // attaches the servo on pin 9 to the servo object
}
void loop()
{
sensorReading = analogRead(knockSensor);
if (sensorReading >=threshold) {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(100);
}
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else (sensorReading < threshold); {
digitalWrite(ledPin, LOW);
pos = 0;
}
delay(200);
}