DS18B20 code problem

ewandingwall

New Member
main:
readtemp C.2,b1 ; read value into b1
if b1 > 50 then ; Temperature rises 50 degrees celsius
high B.1 ; Turn on blue LED
endif
if b1 > 70 then ; Temperature rises above 70 degrees celsius
high B.2 ; Turn on red LED
endif
goto main

im using a hair drier to simulate the temperatures. im getting the blue led constantly turned on but the red doesnt turn on even when i put the hair drier really close next to the temperature sensor.
any ideas as to where im going wrong? many thanks
 

eggdweather

Senior Member
If the programme was just:
high B.2
Does the LED come on?

If so then your not getting the 18b20 hot enough, try 55 instead of 70 then test, gradually moving the value up, I suspect the hairdryer is not that hot. Got a soldering iron, place that near it.
 

ewandingwall

New Member
Code:
main:
readtemp C.2,b1 ; read value into b1

if b1 > 30 then ; Temperature rises above 30 degrees celsius
high B.1        ; Turn on blue LED
pause 1000      ; Wait 1 second
low B.1         ; Turn off blue LED 
else            ; or else
low B.1         ; Turn off blue LED
endif

if b1 > 40 then ; Temperature rises above 40 degrees celsius
high B.2        ; Turn on red LED
pause 1000      ; wait 1 second
low B.2;        ; Turn off red LED
else            ; or else
low B.2         ; Turn off red LED
endif

if b1 > 50 then ; Temperature rises above 50 degrees celsius 
high B.3        ; Turn on green LED
pause 1000      ; Wait 1 second 
low B.3         ; Turn off green LED
else            ; or else
low B.2         ; Turn off green LED 
endif

goto main
thanks very much jarubell, that done the trick!
what about if i want the LED to stop flashing as soon as the new temperature was reached? for example when the temperature reaches 40, the LED for the 30 degrees temperature stops flashing when 40 degrees is reached?
many thanks.
 
Last edited by a moderator:

ewandingwall

New Member
thanks again Jarubell, that done the trick! im going to try and add a buzzer now so that it buzzes when with the flashing LED. After that my final step is to add an OLED screen which should be fun. A lot of people seem to be having trouble with then judging by the forums. Many thanks again.
 

ewandingwall

New Member
Code:
main:
readtemp C.2,b1 ; read value into b1

if b1 > 30 and b1 < 40 then ; Temperature rises above 30 degrees celsius
high B.1        ; Turn on blue LED
low B.4         ; Turn off buzzer
pause 1000      ; Wait 1 second
low B.1         ; Turn off blue LED   
high B.4        ; Turn on buzzer 
pause 1000;     ; Wait 1 second  
else            ; or else
low B.1         ; Turn off blue LED
endif

if b1 > 40 and b1 < 50 then ; Temperature rises above 40 degrees celsius
high B.2        ; Turn on red LED
pause 1000      ; wait 1 second
low B.2         ; Turn off red LED
pause 1000;     ; Turn off red LED
else            ; or else
low B.2         ; Turn off red LED
endif

if b1 > 50 then ; Temperature rises above 50 degrees celsius 
high B.3        ; Turn on green LED
low B.4         ; Turn off buzzer 
pause 1000      ; Wait 1 second 
low B.3         ; Turn off green LED 
high B.4        ; Turn on buzzer 
pause 1000      ; Wait 1 second 
else            ; or else
low B.2         ; Turn off green LED 
endif

goto main
I have added a buzzer to the circuit but between 40 and 50 degrees, the buzzer makes a continious beeping noise even though i havent added any high or low inputs to the code i.e b.4 which is used as the input to the buzzer. Any ideas as to why this is happening? many thanks
 
Last edited by a moderator:

tmfkam

Senior Member
After leaving your 30 degree and 50 degree 'If - EndIf' commands, the buzzer is turned on. It is only turned off again assuming the temperature is either greater than 50 or less than 40.

What happens if the temperature is exactly 40? Or exactly 50?
 

hippy

Technical Support
Staff member
Between 31C and 39C you turn on the buzzer. If the buzzer is on and then the temperature rises to 40C or above the buzzer will remain on.

Just as you are turning the blue LED off when between 31C and 39C you should also turn the buzzer off.

Also note your conditions; "b1 > 30 and b1 < 40" means that code part will only execute between 31C and 39C. Likewise "b1 > 40 and b1 < 50" only between 41C and 49C, and "> 50" only at 51C and above.

You might be able to handle things better with a SELECT CASE ...

Code:
Select Case b1
  Case < 40
    ......... do things when below 40C
  Case < 50
    ......... do things when below 50C
  Else
    ......... do things when 50C or above
End Select
 

ewandingwall

New Member
so i would only need to use the select case on the code relevent to temperatures 41 - 49 because that is the only problem where im getting the continuous beeping sound from the buzzer? could you give me an example in my code where I could implement the select case please? many thanks.
 

hippy

Technical Support
Staff member
so i would only need to use the select case on the code relevent to temperatures 41 - 49 because that is the only problem where im getting the continuous beeping sound from the buzzer? could you give me an example in my code where I could implement the select case please? many thanks.
The problem is not in the 41-49 section per se; it's a design flaw that the buzzer is left on in the 31-39 and above 50 routines. You can turn the buzzer off in any section of code.

The suggestion of using SELECT-CASE is that it's potentially easier to use and less error prone and less difficult to get the conditions right to cover all cases. You should use IF-ELSE-THEN or SELECT-CASE rather than mixing the two.
 

ewandingwall

New Member
okay ill try that, the buzzer is also continually beeping when the temp is bellow 30 which i have just noticed as well as in the 41-49 section. many thanks for the reply.
 

ewandingwall

New Member
Code:
main:
readtemp C.2,b1 ; read value into b1

if b1 >= 30 and b1 <= 40 then ; Temperature rises above 30 degrees celsius
high B.1        ; Turn on blue LED
high B.4         ; Turn on buzzer
pause 250      ; Wait 0.25 seconds
low B.1         ; Turn off blue LED   
low B.4        ; Turn on buzzer 
pause 250;     ; Wait 0.25 seconds  
else            ; or else
low B.1         ; Turn off blue LED
low B.4         ; Turn off buzzer
endif

if b1 > 40 and b1 <= 50 then ; Temperature rises above 40 degrees celsius
high B.2        ; Turn on red LED
endif

if b1 > 50 then ; Temperature rises above 50 degrees celsius 
high B.3        ; Turn on green LED
high B.4         ; Turn on buzzer 
pause 250      ; Wait 0.25 seconds 
low B.3         ; Turn off green LED 
low B.4        ; Turn off buzzer 
pause 250      ; Wait 0.25 seconds 
else            ; or else
low B.2         ; Turn off green LED 
endif

goto main
hello again guys, thanks for all your help. My code is almost perfect, ive fixed my buzzer problem by adding low B.4 to the program which done the trick! now the only issue i have is my red LED is flashing at > 40 an <= 50 degrees even though i only have it set to high B.2. Can any help with the problem. many thanks again for all your help! kind regards.
 
Last edited by a moderator:

bfgstew

Senior Member
Third from bottom line, you have B.2 instead of B.3 this is causing the red LED to flash.

Saying that, this is the only low B.2 in your code, how are you turning the red LED off?
 
Last edited:

ewandingwall

New Member
Code:
screen:
serout b.7,N2400, (254,1)
pause 500
serout b.7, N2400, (254,128)
serout b.7,n2400, ("temp sensor")
pause 2000
goto main
main:
readtemp C.2,b1 ; read value into b12

if b1 >= 30 and b1 <= 40 then
serout b.7, n2400, (254,1)
pause 500
serout b.7, n2400, (254,128)
serout b.7, N2400,("temperature")
serout b.7, n2400, (254,192)
serout b.7, n2400, (#b1,"°C")
pause 2000
serout b.7,n2400, (254,1)
pause 500
serout b.7,n2400,(254,128)
serout b.7,n2400,("low temp")
serout b.7,n2400,(254,192)
serout b.7,n2400, ("alarm")
pause 2000


high B.1        ; Turn on blue LED
high B.4         ; Turn on buzzer
pause 250      ; Wait 0.25 seconds
low B.1         ; Turn off blue LED   
low B.4        ; Turn on buzzer 
pause 250;     ; Wait 0.25 seconds  
else            ; or else
low B.1         ; Turn off blue LED
low B.4         ; Turn off buzzer
endif

if b1 > 40 and b1 <= 50 then ; Temperature rises above 40 degrees celsius
high B.2
else
low B.2
endif

if b1 > 50 then ; Temperature rises above 50 degrees celsius 
high B.3        ; Turn on green LED
high B.4         ; Turn on buzzer 
pause 250      ; Wait 0.25 seconds 
low B.3         ; Turn off green LED 
low B.4        ; Turn off buzzer 
pause 250      ; Wait 0.25 seconds 
else            ; or else
low B.3         ; Turn off green LED 
endif

goto main

ive tried to get temperatures on an LCD screen but my degrees sign is comming up as a minus sign on my screen. plus my LEDS and buzzers are going off and on at a much slower rates. can anyone see any errors in my code for the LCD? many thanks and kind regards.
 
Last edited by a moderator:

ewandingwall

New Member
LCD screen problem with DS18B20 temp sensor.

This is a duplicate of post 20 a merged here along with following 1 posted response to keep all together:

screen:
serout b.7,N2400, (254,1)
pause 500
serout b.7, N2400, (254,128)
serout b.7,n2400, ("temp sensor")
pause 2000
goto main
main:
readtemp C.2,b1 ; read value into b12

if b1 >= 30 and b1 <= 40 then
serout b.7, n2400, (254,1)
pause 500
serout b.7, n2400, (254,128)
serout b.7, N2400,("temperature")
serout b.7, n2400, (254,192)
serout b.7, n2400, (#b1,"°C")
pause 2000
serout b.7,n2400, (254,1)
pause 500
serout b.7,n2400,(254,128)
serout b.7,n2400,("low temp")
serout b.7,n2400,(254,192)
serout b.7,n2400, ("alarm")
pause 2000


high B.1 ; Turn on blue LED
high B.4 ; Turn on buzzer
pause 250 ; Wait 0.25 seconds
low B.1 ; Turn off blue LED
low B.4 ; Turn on buzzer
pause 250; ; Wait 0.25 seconds
else ; or else
low B.1 ; Turn off blue LED
low B.4 ; Turn off buzzer
endif

if b1 > 40 and b1 <= 50 then ; Temperature rises above 40 degrees celsius
high B.2
else
low B.2
endif

if b1 > 50 then ; Temperature rises above 50 degrees celsius
high B.3 ; Turn on green LED
high B.4 ; Turn on buzzer
pause 250 ; Wait 0.25 seconds
low B.3 ; Turn off green LED
low B.4 ; Turn off buzzer
pause 250 ; Wait 0.25 seconds
else ; or else
low B.3 ; Turn off green LED
endif

goto main


ive tried to get temperatures on an LCD screen but my degrees sign is comming up as a minus sign on my screen. plus my LEDS and buzzers are going off and on at a much slower rates. can anyone see any errors in my code for the LCD? many thanks and kind regards.
 
Last edited by a moderator:

neiltechspec

Senior Member
Use this for displaying degree symbol.

serout b.7, n2400, (#b1,$df,"C")

$df is the ascii code (in HEX) for the degree symbol on most LCD's

"°C" is not valid

a "readtemp" takes approx 750ms to complete, that would probably confuse your expectations

Neil.
 
Last edited:

westaust55

Moderator
You have around 5 seconds with of delays through the various PAUSE commands associated with the LCD display part so the Main: program loop is only executed once every 5 or so seconds.
Hence the updates are slower.

As a further comment, when you have more than a few lines of program code, the actual code part should be placed within [code] and [/code] tags - then it appears in a sub window and formatting and indenting are preserved.

Edit:
I have merged your two threads on the same general topic into this one thread.
It is not nice to have the same post in two different threads and folks answer in different places so the information becomes out of synch.
 
Last edited:

Hemi345

Senior Member
Just a suggestion, but it would help you tremendously (and us) if you used symbols to label your variables, pins, and such at the beginning of your program and then use these symbols. Like:
Code:
symbol redLED = B.2
symbol buzzr = B.4
symbol myPause = 250
...

high redLED
high buzzr
pause myPause
low redLED
low buzzr
...
 

ewandingwall

New Member
thank you very much everyone who replied over the past few days, after a bit more fiddling about with my code, my LCD worked without any major issue. I now have a fully functional temperature sensor!
many thanks to everyone again for your input and good luck with your future projects.
 
Top