I changed my code
#include <math.h>
int z ;
int c=300;
const int transistorPin = 9;
int tempPin = 0;
int tempReading;
float x;
int y;
const int numReadings = 100;
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
void setup()
{
pinMode(9, OUTPUT);
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop()
{
tempReading=analogRead(tempPin);//temperature reading
//map the reading to change the frequency
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(tempReading);
// 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
delay(10);
z=y ; // delay
if (average>=140&&average<170){if(y<=100){ y=100;}else if(y>100){y--;};}
if (average>=170&&average<200){if(y>=400){y=400;}else if(y<400){y++;};}
delay(10);
float val = (exp(sin(millis()/150000.0*y*PI)) - 0.36787944)*108.0;//the function of breathing led
analogWrite(transistorPin, val);
Serial.println(y);
}
It's wierd that the flashing result still exist,bacause
#include <math.h>
int z ;
int c=300;
const int transistorPin = 9;
int tempPin = 0;
int tempReading;
float x;
int y=400;
const int numReadings = 100;
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
void setup()
{
pinMode(9, OUTPUT);
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop()
{
tempReading=analogRead(tempPin);//temperature reading
//map the reading to change the frequency
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(tempReading);
// 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
delay(1); // delay
y=y--;
float val = (exp(sin(millis()/150000.0*y*PI)) - 0.36787944)*108.0;//the function of breathing led
analogWrite(transistorPin, val);
delay(10);
Serial.println(y);
}
works perfectly