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

I'm trying to control a pulsing LED using temperature sensor, but it doesn't work out. The LED pulses randomly. =(

I'm doing this for an art project, any help is appreciated.

#include <math.h>

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
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);
  float val = (exp(sin(millis()/1500.0*x*PI)) - 0.36787944)*108.0;//the function of breathing led
  Serial.println(val);
  analogWrite(transistorPin, val);


}

Is it the val values that you are hitting analogWrite with?
What if you Serial.print those? What's happening there?
Shouldn't you take a pause after sampling and analogWriting at the end of the loop? To let that dwell for a bit?
I guess it's supposed to rise and fall, and how does that work out?

Hi Runaway Pancake ,
Below I attached the result I get from serial print val.
How do I take a pause after sampling and analogWriting at the end of the loop?
I'm really new to Arduino.
Thank you so much for replying :slight_smile:

Untitled.tiff (63.3 KB)

I tried to delay(30) at the end of the loop and after the equation, the light still flashes randomly.
(to be specific, it looks like there is transition of frequency, but once the frequency change, the light flashes and dim randomly between differentfrequency)

I tried to use floating point too, and it didn't work either.

#include <math.h>

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

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

void loop()
{
  tempReading=analogRead(tempPin);//temperature reading
  x=map(tempReading,150,170,15000,20000);//map the reading to change the frequency
float z= (float)x/10.0;

  float val = (exp(sin(millis()/z*PI)) - 0.36787944)*108.0;//the function of breathing led
  Serial.println(z);
  delay(30);
  analogWrite(transistorPin, val);



}

kovil:
Below I attached the result I get from serial print val.

Hi, I can't view that attachment. Can you just post the output as text please (just cut & paste).

I don't think a delay is important and your formulas look ok. Your breathing formula should give a value between 0 and 255.

Can we have a schematic please? How are the temp sensor and led attached? What temp sensor is used and what transistor?

Paul

PaulRB:
and what transistor?

Transistor?

kovil:
How do I take a pause after sampling and analogWriting at the end of the loop?

#include <math.h>

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
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);
  float val = (exp(sin(millis()/1500.0*x*PI)) - 0.36787944)*108.0;//the function of breathing led
  Serial.println(val);
  analogWrite(transistorPin, val);
  delay(500);  //  <<<< a pause
}

Stll, though, what is the "breathing" effect here?

The pin that the pwm is used on in the sketch is called "transistorPin". I inferred from that there could be a transistor bewteen the Arduino output and the led and that could be the cause of the random flashing.

The "breathing" effect is generated by the sin () function. Its period is governed by the millis () function combined with the value derived from the temperature reading. Adding delays won't change that period, just update it less often. The exp () function compensates for the non-linear response of the eye.

So, if I used that sketch with an ordinary LED, but made tempReading a const, it'd sit there fading in/out?
Can you suggest a good value to plug in?

OK, cool

WIth this modified version of the sketch

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
void setup()
{
  pinMode(transistorPin, OUTPUT); 
}

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

there's a pulsating effect
BUT
only for values of x < 15 or so.

PaulRB:

kovil:
Below I attached the result I get from serial print val.

Hi, I can't view that attachment. Can you just post the output as text please (just cut & paste).

I don't think a delay is important and your formulas look ok. Your breathing formula should give a value between 0 and 255.

Can we have a schematic please? How are the temp sensor and led attached? What temp sensor is used and what transistor?

Paul

Hi, thanks for the reply. The transistor is BC547, the temp is TMP36.
The transistor control the on and off of a led light with 9v input (not from arduino), The temp snesor is connected to the 5v, ground, and a0.
The function works perfectly if there is no temperature variable.( float val = (exp(sin(millis()/1500.0*PI)) - 0.36787944)*108.0)
And the temperature reading works fine.

PaulRB:

kovil:
Below I attached the result I get from serial print val.

Hi, I can't view that attachment. Can you just post the output as text please (just cut & paste).

I don't think a delay is important and your formulas look ok. Your breathing formula should give a value between 0 and 255.

Can we have a schematic please? How are the temp sensor and led attached? What temp sensor is used and what transistor?

Paul

.79
2.24
1.76
1.27
0.91
0.62
0.35
0.18
0.06
0.01
0.01
0.08
0.20
0.38
0.62
0.91
1.33
1.83
2.33
2.89
245.06
4.45
90.53
6.38
7.33
8.36
9.66
10.86
12.37
14.00
16.02
17.92
252.36
251.22
249.76
247.50
245.35
242.90
8.42
41.98
45.52
49.81
53.78
57.96
62.98
217.57
212.64
208.09
203.40
198.58
98.76
189.25
113.61
177.68
172.45
167.20
161.92
155.99
150.72
168.51
177.36
185.11
128.74
201.01
208.10
113.91
109.14
103.89
233.94
238.72
89.99
246.05
249.35
251.54
253.01
253.75
253.75
252.71
251.06
248.69
245.63
241.91
237.57
232.00
226.47
219.69
213.20
206.35
0.97
192.71
185.11
177.36
169.50
159.61
151.71
143.85
136.07
128.42
120.00
112.71
105.62
97.92
92.14
86.56
80.42
6.69
71.02
66.25
61.70
57.35
52.06
48.17
2.57
41.49
38.15
35.01
31.63
28.86
26.25
0.43
21.51
19.37
17.09
15.24
13.52
11.93
0.07
9.28
8.01
6.85
0.56
5.10
4.33
3.62
1.44
2.33
1.83
1.40
2.83
0.71
0.42
0.20
0.08
0.01
0.00
0.05

kovil:

PaulRB:

kovil:
Below I attached the result I get from serial print val.

Hi, I can't view that attachment. Can you just post the output as text please (just cut & paste).

I don't think a delay is important and your formulas look ok. Your breathing formula should give a value between 0 and 255.

Can we have a schematic please? How are the temp sensor and led attached? What temp sensor is used and what transistor?

Paul

I tried normal led without transistor too, still has random output

Does running my simulation example [above] result what you expect ?
If not, could you sketch out your transistor circuit?
(Skip the verbal, do the schematic or make a diagram.)

const int transistorPin = 9;
int tempPin = 0;
int tempReading;
int x;
void setup()
{
  pinMode (transistorPin, OUTPUT); 
  Serial.begin(9600); 
}

void loop()
{
  tempReading=analogRead(tempPin);
  x=map(tempReading,150,170,1,5);
  x = x / 17  // *NEW* IF preceding line results 0 < x < 255 then, here, 0 < x < 15
  float val = (exp(sin(millis()/1500.0*x*PI)) - 0.36787944)*108.0;
  analogWrite(transistorPin, val);
}

Sorry, but why x/17?

It doesn't work

x only control the frequency, the amplitude of the wave is controlled to under 255.
The random error is not caused by the exceeding of 255.

This is the Wave I use

kovil:
Sorry, but why x/17?
It doesn't work

Because from my experience of it, only values of x less than 15 give a good result, higher values are too fast.
So the range, I believe, should be limited to 0 to 15 not 0 to 255.
255 / 17 = 15
If you can reconfigure the map expression for that -- then by all means.
It was easier for me to divide that by 17, cheap and easy.

Does my demo sketch in Reply #9 effect a fade in/out somewhat as you expect?
It won't change, it just stays at that one "speed", but it verifies the fading aspect anyway.
Doesn't even that much "work"? [I think it does here - I can *youtube* evidence of that if necessary.]

And I don't know from your "wave", let's see your "transistor circuit", please.

I guess I am being unreasonable.

Yeah, I know the fade works if there is no x, I wrote the function, and x is mapped to 1, 5. Whats the point of /17?
The problem is the change of the x resulted in random data in the output val.
BTW, I switch the transistor to a single led connected to 5v output, the result is the same.

kovil:
Whats the point of /17?

x=map(tempReading,150,170,1,5);
OK, map is resulting a range of 1 to 5 already, provided that 150 < tempReading < 170.
Is that the situation, those are your results?
tempReading=analogRead(tempPin);
Serial.println(tempReading,DEC); // this is always a number 150-170 ??

kovil:
Yeah, I know the fade works if there is no x...

No, x is needed either way.