Temperature to flash a light part2

gengis

New Member
My code is supposed to flash a light in sync with the temperature. It does and works pretty well (aside from the aforementioned problem flashing Zero times in part one)

The thing is simply a DS18B20 one wire temperature sensor using 12 bit accuracy and converting to degrees F, then breaking out the tens, units and hundreds to flash a common anode tricolor LED. 80 and above is red, 60-80 green, below 60 is blue.

It works but I want to also want to break up the colors further so that between75-80 flashes the tens in green and units in red - and I need a different way to do it than my current code because it uses too much program memory (and I still need to add a photocell so I can shut it down during daylight - that's easy but takes another couple of lines of code)

So, to make it clearer: 72 F should flash green 7 times pause, flash green 2 times -long pause and repeat. 75 F should flash green 7 times pause, flash red 5 times - long pause and repeat. (also to the same at 65, so 65 would flash blue 6, green 5)

My code so far:
Code:
'Main2 takes a 12 bit reading from the ds18 and converts celcius (times 16) to Fahrenheit
'then breaks out the digits into units,tens,hundreds for flashing led display



symbol C_raw = w0
symbol DegreesF = b4
symbol Celcius = b2
symbol tens = b10
symbol units = b11
symbol hundreds = b12
symbol red = 0
symbol blue = 1
symbol green = 2
    high red
main2:

    readtemp12 4,C_raw        '12 bit reading from sensor
    DegreesF = w0*9/80+32        'conversion to fahrenheit degrees (with divide by 16 included)
    Celcius = w0/16            'just a place for degrees C for debugging purposes
    units = DegreesF//10 'Decimal points    'breakout units
    tens = DegreesF/10 'First digits        'tens        
    hundreds = DegreesF/100                'hundreds
    'debug                        '(remarked)for troubleshooting purposes 
    'goto main2    
    pause 2000
    if DegreesF>= 80 then redone
    if DegreesF>= 60 then  greenone    
    if DegreesF>= 0 then  blueone    



redone:    
    if degreesF>=100 then low red
    pause 200
    high red
    pause 200
    endif
    pause 1000
    for b5 = 1 to tens low red
    pause 200
    high red
    pause 200
    next
    pause 1000
     for b5 = 1 to units low red
    pause 200
    high red
    pause 200
    next
    pause 1000
    goto main2


greenone:
    for b5 = 1 to tens low green
    pause 200
    high green
    pause 200
    next
    pause 1000
     for b5 = 1 to units low green
    pause 200
    high green
    pause 200
    next
    pause 1000
    goto main2


blueone:
    for b5 = 1 to tens low blue
    pause 200
    high blue
    pause 200
    next
    pause 1000
     for b5 = 1 to units low blue
    pause 200
    high blue
    pause 200
    next
    pause 1000
    goto main2
 

Jamster

Senior Member
Can you tell us what ic you are using so i can test it and while away the next hour getting it to work :)
 

gengis

New Member
I'm using a garden variety 08M on a breadboard with batteries to get it going. Nothing fancy.

Is that what you were asking Jamster?
 

Jeremy Leach

Senior Member
Without looking too deeply, I think you need to just have one subroutine for red/green/blue and pass it parameters as variables. So much of the code in your existing 3 routines is repeated so you'll save a lot of space.

You basically need a section of code to select the colours based on the current temperature. This could be a set of IF statements or maybe SELECT...CASE.

You could code the colours according to the output pins, and then make use of the 'pins' variable.

I'm deliberately leaving it vague otherwise I'll be writing it :)
 

Jamster

Senior Member
Try this
Code:
'Main2 takes a 12 bit reading from the ds18 and converts celcius (times 16) to Fahrenheit
'then breaks out the digits into units,tens,hundreds for flashing led display



symbol C_raw = w0
symbol DegreesF = b4
symbol Celcius = b2
symbol tens = b10
symbol units = b11
symbol hundreds = b12
symbol red = 0
symbol blue = 1
symbol green = 2
    high red
main2:

    readtemp12 4,C_raw        '12 bit reading from sensor
    DegreesF = w0*9/80+32        'conversion to fahrenheit degrees (with divide by 16 included)
    Celcius = w0/16            'just a place for degrees C for debugging purposes
    units = DegreesF//10 'Decimal points    'breakout units
    tens = DegreesF/10 'First digits        'tens        
    hundreds = DegreesF/100                'hundreds
    'debug                        '(remarked)for troubleshooting purposes 
    'goto main2    
    pause 2000
    if DegreesF>= 80 then redone
    if DegreesF>= 60 then  greenone    
    if DegreesF>= 0 then  blueone    



redone:    
    do until b13=tens
    	high red
    	pause 200
    	low red
    	pause 200
    	inc b13
    loop
    pause 2000
    let b13=b13-b13
    do until b13=tens
    	high red
    	pause 200
    	low red
    	pause 200
    	inc b13
    loop
    goto main2


greenone:
    do until b13=tens
    	high green
    	pause 200
    	low green
    	pause 200
    	inc b13
    loop
    pause 2000
    let b13=b13-b13
    do until b13=tens
    	high green
    	pause 200
    	low green
    	pause 200
    	inc b13
    loop
    goto main2



blueone:
    do until b13=tens
    	high blue
    	pause 200
    	low blue
    	pause 200
    	inc b13
    loop
    pause 2000
    let b13=b13-b13
    do until b13=tens
    	high blue
    	pause 200
    	low blue
    	pause 200
    	inc b13
    loop
    goto main2
Probably wrong
 

Jamster

Senior Member
What aabout this, i think it does what you want
Code:
'Main2 takes a 12 bit reading from the ds18 and converts celcius (times 16) to Fahrenheit
'then breaks out the digits into units,tens,hundreds for flashing led display



symbol C_raw = w0
symbol DegreesF = b4
symbol Celcius = b2
symbol tens = b10
symbol units = b11
symbol hundreds = b12
symbol red = 0
symbol blue = 1
symbol green = 2
    high red
main2:

    readtemp12 4,C_raw        '12 bit reading from sensor
    DegreesF = w0*9/80+32        'conversion to fahrenheit degrees (with divide by 16 included)
    Celcius = w0/16            'just a place for degrees C for debugging purposes
    tens = DegreesF/10 
    let b13=tens*10
    units = DegreesF-b13                   
    let b13=0
    'debug                        '(remarked)for troubleshooting purposes 
    'goto main2    
    pause 2000
    if tens>=7 then
    	let b5= green
    endif
    if tens=6 then 
    	let b5= blue    
    endif
    if tens<=5 then
    	let b5= red
    endif
    
    if units>=5 and b5=green then
    	let b6= red
    endif
    if units>=5 and b5=blue then
    	let b6= green    
    endif
    if units>=5 and b5=red then
    	let b6= blue
    endif
    if units>=0 and units<5 then
    	let b6 =5
    endif
    
    do until b13=tens
    	high b5
    	pause 200
    	low b5
    	pause 200
    	inc b13
    loop
    let b13=0
    pause 2000
    do until b13=units
    	if b6=5 then
    		let b6=b5
    	endif
    	high b6
    	pause 200
    	low b6
    	pause 200
    	inc b13
    loop
    let b13=0
    goto main2
Jamster
 

gengis

New Member
Thanks Jamster

It isn't working... the ambient is ~70 and it flashes 7 then stays on, then off for a flash of one (so it may be 71 - can't run debug and LEDs at the same time or at least the red one), but then I warm it with my hand and it flashes 74 (leaving the green on between flashes) - Leds are on when low (common anode - anode connected to + power)

When it gets to red, that's it red comes on stays on and it doesn't flash or do anything even when the temperature drops - reset the power and it starts semi working again.

I do think you have a better concept of how to do it. My alternative was going to be shift to a 14M and control the anode with a PNP transistor which should simplify the program,(separate the flashing from the color) but logic tells me that there aught to be a way to do it in code and I just don't see it.
 

Jamster

Senior Member
Feel freee to play about with the code if you want, it should work after a few tampers.

No problem

Jamster
 

william47316

New Member
read up on pulsout, and set pins :)
pulsout will remove the need for the low commands and the set pins will set pins on or off all at once
eg pulsout blue, 5000 will pulse your blue pin for 50mS
and set pins = 00010111 will set all the output pins on an 08M "high" (4, 2,1 and 0)
 

marks

Senior Member
RGB Temperature Fahrenheit

HI Flooby,
I cant wait for part 3 lol !!
I was wondering how you would remember all those colours but after thinking about it
quite a clever idea it does make sense lol.i have only gota 20x2 and 1 led so couldnt
completely test ,leave it for you to R& D (p,s hope you get syntex lol)
Code:
        SYMBOL Ds18b20       = 4
        SYMBOL Red           = 0
        SYMBOL Green         = 2
        SYMBOL Blue          = 1
      
        SYMBOL Colourten     = B2
        SYMBOL Colourone     = B3
        SYMBOL pulse         = B4
        SYMBOL hundreds      = B5  
        SYMBOL units         = B6  
        SYMBOL tens          = B7  		  
        SYMBOL DegreesF      = W0 
	
Main:High red,green,blue

Readtemperature:
Readtemp12 ds18b20, DegreesF
DegreesF = DegreesF+880*9/80-67 'degrees fahrenheit = 0 to 257

SELECT CASE DegreesF
  CASE 0  to 64 : Colourten = blue  : Colourone = blue
  CASE 65 to 69 : Colourten = blue  : Colourone = green
  CASE 70 to 74 : Colourten = green : Colourone = green
  CASE 75 to 80 : Colourten = green : Colourone = red
  CASE     > 80 : Colourten = red   : Colourone = red
END SELECT

hundreds = DegreesF/100
  for pulse = 1 to hundreds                                                                                          
    high  Red  : pause 400
    IF hundreds = 0 THEN
                goto tensd      
    else
    low   Red  : pause 800         'low output for pulse                             
 endif
  next pulse
     high Red  : pause 2000

Tensd:  tens = DegreesF//100/10
  for pulse = 1 to tens                                                                                          
    high  Colourten  : pause 400
    IF tens = 0 THEN
                 pause 400
    else
    low   Colourten  : pause 400    'low output for pulse                             
 endif
  next pulse
     high Colourten  : pause 2000
 
units =DegreesF//100//10
  for pulse = 1 to units                                                                                      
     high  Colourone  : pause 400
     IF units = 0 THEN
                  pause 100
     else
     low   Colourone  : pause 100    'low output for pulse                                   
     endif
  next pulse 
     high  Colourone  : pause 2000 
                             
   goto Readtemperature
 

marks

Senior Member
Dam you can always improve a little bit better post this before Westy lol
Code:
     SYMBOL Ds18b20       = 4
      SYMBOL Red           = 0
      SYMBOL Green         = 2
      SYMBOL Blue          = 1
      
      SYMBOL Colourten     = B2
      SYMBOL Colourone     = B3
	SYMBOL pulse         = B4
	SYMBOL hundreds      = B5  
	SYMBOL units         = B6  
	SYMBOL tens          = B7  		  
	SYMBOL DegreesF      = W0 
	
Main:High red,green,blue

Readtemperature:
Readtemp12 ds18b20, DegreesF
DegreesF = DegreesF+880*9/80-67 'degrees fahrenheit = 2 to 257

SELECT CASE DegreesF
  CASE       32 : Colourten = red   : Colourone = blue          'possible data loss
  CASE 2  to 64 : Colourten = blue  : Colourone = blue        ' 10 blue
  CASE 65 to 69 : Colourten = blue  : Colourone = green     ' 66 blue/green
  CASE 70 to 74 : Colourten = green : Colourone = green    ' 70 green
  CASE 75 to 80 : Colourten = green : Colourone = red       ' 77 green/red
  CASE     > 80 : Colourten = red   : Colourone = red          ' 100 red
END SELECT

hundreds = DegreesF/100
  for pulse = 1 to hundreds                                                                                          
    high  Red  : pause 400
    IF hundreds = 0 THEN
                goto tensd      
    else
    low   Red  : pause 800      'low output for pulse slow                        
 endif
  next pulse
     high Red  : pause 2000

Tensd:  tens = DegreesF//100/10
  for pulse = 1 to tens                                                                                          
    high  Colourten  : pause 400
    IF tens <> 0 THEN
        low   Colourten         
    endif
                  pause 400    'low output for pulse fast                            
  next pulse
     high Colourten  : pause 2000
 
units =DegreesF//100//10
  for pulse = 1 to units                                                                                      
     high  Colourone  : pause 400
     IF units <> 0 THEN
            low   Colourten         
    endif
                  pause 100    'low output for pulse  quick                           
  next pulse
     high Colourten  : pause 2000
                             
   goto Readtemperature
 
Last edited:

gengis

New Member
Well I have a lot of stuff to play with here.

Thank you all.

Unfortunately I have to leave it alone whilst trucking up to see my wife for the "Thanksgiving" weekend. She wouldn't understand if I pulled out handfuls of stuff and just marched to her computer to play.I only see her twice a month... Tempting as it is to take the breadboard along, it would be sending the wrong message.

But I will check the forum.
 

Dippy

Moderator
I think if it gets to Part7 then Flooby should invite us all up for a Thanksgiving meal to pay for the work :)

Marks won't eat much though lol as he is too busy lolling. lol ;)
 
Top