Breathing LED controlled by temperature sensor (NEED HELP!!)

PaulRB:
Hmmm... I think RP may have spotted the problem. We always focus on the complex parts of the problem, but yet again, it may be a simple error we didn't even think to question!

I changed it to A0, same flashing result

I changed it to A0, same flashing result

Rats!

Ok, have we seen the values of tempReading and x yet?

kovil:
I changed it to A0, same flashing result

So you do have the "temperature sensor" connected to Analog In pin A0 (not Digital pin 0) -- right ?

And when I posted that I got some wonky results, that was only when it changed val, the transitions from one to the next weren't "graceful".

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

Thanks for quoting my last post to you, but you did not answer my question.

It doesn't actually matter, If it's pin 0, and I use 'analogRead', the default is A0. The problem is the output, not input.

And that's what I've been saying the whole time: ' the transitions from one to the next weren't "graceful" '
It does work, but not well.

kovil:

[quote author=Runaway Pancake link=topic=232878.msg1679571#msg1679571 date=1397481446]

kovil:
I changed it to A0, same flashing result

So you do have the "temperature sensor" connected to Analog In pin A0 (not Digital pin 0) -- right ?

And when I posted that I got some wonky results, that was only when it changed val, the transitions from one to the next weren't "graceful".

And that's what I've been saying the whole time: ' the transitions from one to the next weren't "graceful" '
It does work, but not well.
[/quote]

Horsefeathers!
You posted that you were getting "random" flashing, "not working", "doesn't work" and that like.
Maybe I'm having an aneurysm. Does anybody else smell burning toast?
So your beef all along has been the transitions?

My "guess" would be to structure the equation so that any new value of x is incorporated when the output is:
a) at max
OR
b) at min.

Horsefeathers!
You posted that you were getting "random" flashing, "not working", "doesn't work" and that like.
Maybe I'm having an aneurysm. Does anybody else smell burning toast?
So your beef all along has been the transitions?

My "guess" would be to structure the equation so that any new value of x is incorporated when the output is:
a) at max
OR
b) at min.

[/quote]

Sorry for not being clear, what I meat by ' not working' is the result is not good enough for my at installation, and random flash means the val output not following the curve. I wrote a lot of these program, including case switch and if statement, but none of them seem to work flawlessly. Can u think of a way to control the breath light using temperature? Maybe use a new function or pwm, I've already tried everything I know. Sorry for the rudeness, I was really angry, since the last one I wrote should work perfectly in theory, but didn't acutely work out. Thanks for helping me.

I'm not clear what the problem is now.... start over, as though you are explaining it to someone new.

There are 5 speeds (as derived from the map scheme), or levels, or whatever you want to call them.
So, when it changes a level, from 3 to 4 or 3 to 5 (for example), it can/will, by the looks of it here, cut short instead of winding down, no matter where it is in the curve - bang, it starts anew.

The transition to a new "speed" shouldn't be allowed till the output is at "0".

I understand you, the wave should only change if the output is zero, but how do I write the code? Thanks

Right, so what I think is that the temp band controls the wavelength of the sine wave. In the instant when that wavelength changes, we want to remain at the same point on the sine curve, so there is no sudden change in led brightness. I'll have a think.

PaulRB:
Right, so what I think is that the temp band controls the wavelength of the sine wave. In the instant when that wavelength changes, we want to remain at the same point on the sine curve, so there is no sudden change in led brightness. I'll have a think.

Yes, or only changes the wavelength when val reaches zero

Yes, I know how to do it. Declare a variable which keeps track of the current "angle"/position in the sine wave. The "breathing" formula uses that angle, rather than calculating it from millis (). Each time round the loop, add a small amount to the angle. That small amount depends on the temp band. Lowest temp band = smallest additional angle, highest temp band = largest additional angle. Don't have to wait until zero before changing the wavelength, because there will be no sudden change to the angle, whatever angle that is at the time.

So its just a matter of calculating the additional angle for each temp band.

Paul

PaulRB:
Yes, I know how to do it. Declare a variable which keeps track of the current "angle"/position in the sine wave. The "breathing" formula uses that angle, rather than calculating it from millis (). Each time round the loop, add a small amount to the angle. That small amount depends on the temp band. Lowest temp band = smallest additional angle, highest temp band = largest additional angle. Don't have to wait until zero before changing the wavelength, because there will be no sudden change to the angle, whatever angle that is at the time.

So its just a matter of calculating the additional angle for each temp band.

Paul

Thanks so much, can you help me write an example? If you have free time.

Can you post your latest code again? Or maybe not the latest, I think we may have gone up more than one blind alley and the code may have become overcomplicated. Post the best/simplest starting point.

I based this on the version you gave in the very first post:

#include <math.h>

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
float angle = 0;

void setup()
{
  pinMode(9, OUTPUT); 
  Serial.begin(9600); 
}

void loop()
{
  tempReading=analogRead(tempPin);//temperature reading
  x=map(tempReading,150,170,1,5);//map the reading to change the frequency
  Serial.println(x);
  angle += 20.0/1500.0*x*PI;
  float val = (exp(sin(angle)) - 0.36787944)*108.0;//the function of breathing led
  Serial.println(val);
  analogWrite(transistorPin, val);
  delay(20);

}

PaulRB:
I based this on the version you gave in the very first post:

#include <math.h>

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
float angle = 0;

void setup()
{
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  tempReading=analogRead(tempPin);//temperature reading
  x=map(tempReading,150,170,1,5);//map the reading to change the frequency
  Serial.println(x);
  angle += 20.0/1500.0xPI;
  float val = (exp(sin(angle)) - 0.36787944)*108.0;//the function of breathing led
  Serial.println(val);
  analogWrite(transistorPin, val);
  delay(20);

}

Thank you so much!!!!! IT WORKS!!
I add a smooth code, It works flawlessly now!!!
Here is the code.

const int numReadings = 50;

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;

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
float angle = 0;

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
    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
  Serial.println(average);   
  delay(1);  
  x=map(average,155,175,1,10);//map the reading to change the frequency
  Serial.println(average);
  angle += 20.0/2000.0*x*PI;
  float val = (exp(sin(angle)) - 0.36787944)*108.0;//the function of breathing led
  
  analogWrite(transistorPin, val);
  delay(20);

}