Go Down

Topic: Breathing LED controlled by temperature sensor (NEED HELP!!) (Read 2337 times) previous topic - next topic

Runaway Pancake




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.


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.




"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

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.


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.







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.

PaulRB

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

Runaway Pancake

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".
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

kovil


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

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.

kovil


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

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

kovil


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.

PaulRB

#39
Apr 15, 2014, 06:01 pm Last Edit: Apr 15, 2014, 06:10 pm by PaulRB Reason: 1
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.

PaulRB

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

Code: [Select]
#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);

}

kovil


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

Code: [Select]
#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);

}



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

Code: [Select]
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);

}

Go Up