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 brightness = 0;
int fadeAmount = 5;
int delayDuration = 30;
int flag=0;
int inputPin = A0;
void setup()
{
pinMode(3, OUTPUT);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// 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
Serial.println(average);
delay(1);
if(average<341) {
analogWrite(3, brightness);
if(brightness==0) {
delayDuration=60;
}
if(brightness==255) {
delayDuration=30;
}
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount ;
if(brightness==0) flag=1;
}
delay(delayDuration);
}
else if(average<682){
analogWrite(3, brightness);
delayDuration=30;
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount ;
if(brightness==0) flag=1;
}
delay(delayDuration);
}
else if(average<1024) {
analogWrite(3, brightness);
if(brightness==0) {
delayDuration=30;
}
if(brightness==255) {
delayDuration=60;
}
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount ;
if(brightness==0) flag=1;
}
delay(delayDuration);
}
}
===================================================================
沒有留言:
張貼留言
文章有誤或有問題麻煩您留言告知! 謝謝您~~