SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By Limus
#30574
Hi all,

I'm contacting you because I have a doubt related to Humidity and temperature sensor. I have tested the code in my project and it worked well. As I told you before I'm using the atmega16, lcd 16X2, and 2 actuators ( a cooler and a Thermostate resistor to warm the air). I would like to do the following:

When Humidity is >=75% the cooler starts working, otherwise remains stoped.

The Thermostate resitor is always "on", but when temperature is >= 30º it stops.

I'm using portc.1 for cooler and portc.2 for thermostate resistor.



The instruction to set up cooler and thermostate resistor is Reset portc.1/2 because the actuator circuit (with a relay) works as a not.

I'm working on this for two weeks but I can't put this working. Anyone could help me with this please?
This is for a final year school project and I'm starting to be worried.



Humidity and temperature sensor code:

$crystal = 4000000
$regfile = "m16def.dat"
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portd.3 , Db5 = Portd.2 , Db6 = Portd.1 , Db7 = Portd.0 , E = Portd.6 , Rs = Portd.7

Dim Command As Byte , Msb As Byte , Lsb As Byte , Humi As Word
Dim I As Byte , Temp As Word , Celi As Word , Deseti As Word , Big_celi As Word

Config Portc.0 = Input 'Portc Sensor Chuva
Config Portc.1 = Output
'Config Portc.2 = Input
'Config Portc.3 = Output
'Config Portc.4 = Output

Config Portb.7 = Output
'Config Portb.2 = Output

Deflcdchar 0 , 6 , 9 , 9 , 6 , 32 , 32 , 32 , 32 'Creating "°" character


Sck Alias Portd.0 'Serial CLOCK
Sdata Alias Pind.1 'Serial DATA for INPUT
Sdata_out Alias Portd.1 'Serial DATA for OUTPUT

Ddrd.0 = 1 'Data direction for SCK

Config Watchdog = 2048 'Watchdog time setting
Start Watchdog 'watchdog enable
Cursor Off
Cls 'LCD must move back to DATA mode after creating chr
Waitms 20 'Must wait to stabilize LCD

If Pinc.0 = 0 Then

Goto Rotina_chuva

Else

Goto Start1

End If

Start1: 'Main program

Reset Portb.7
Ddrd.2 = 1
Ddrd.0 = 1
Set Portb.2 'Switch ON LED (measuring start)

Command = &B00011110 'SOFTWARE RESET command
Gosub Trans_start
Gosub Send_cmd

Gosub Write_status
Gosub Conn_reset
Gosub Read_humidity

Gosub Conn_reset
Gosub Read_temp

'Cursor Off
Reset Portb.2 'Switch OFF LED (measuring finish)
'Cls ' Clear display
'Waitms 10

Locate 1 , 1
Lcd "HR:"

Locate 1 , 8
Lcd Humi ; " %"

Locate 2 , 1
Lcd "Temp:"

If Celi < 10 Then
Locate 1 , 9
Lcd Celi
Else

Locate 2 , 8
End If
Lcd Celi
Locate 2 , 10
Lcd "."
Locate 2 , 11
Lcd Deseti

Locate 2 , 13
Lcd Chr(0)
Locate 2 , 14
Lcd "C "

' Reset Watchdog
'L11:
'Goto L11
'Powerdown

'***************'
' HUMIDITY READ '
'***************'

Read_humidity:
Command = &B00000101 'Command "READ HUMIDITY"
Gosub Send_cmd
Waitms 20 'Wait to finish measuring
Gosub Sensor_readout

Humi = 0 'Start of humidity calculation (see App. note)
Humi = Humi + Msb
Shift Humi , Left , 8
Humi = Humi + Lsb

If Humi <= 107 Then
Humi = 143 * Humi
If Humi < 512 Then Humi = 512
Humi = Humi - 512
Else
Humi = 111 * Humi
Humi = Humi + 2893
If Humi > 25600 Then Humi = 25600

End If
Shift Humi , Right , 8

Return
'******************'
' TEMPERATURE READ '
'******************'

Read_temp:
Command = &B00000011 'Command "READ TEMPERATURE"
Gosub Send_cmd
Waitms 60
Gosub Sensor_readout
Temp = 0 'Calculation (see App. note)
Temp = Temp + Msb
Shift Temp , Left , 8
Temp = Temp + Lsb
Temp = Temp * 10
Temp = Temp / 25
Temp = Temp - 400


Celi = Temp / 10
Big_celi = Celi * 10
Deseti = Temp - Big_celi

Return



'******************'
' TRANSMISION START'
'******************'


Trans_start:
Config Sdata = Output
Set Sdata_out
Set Sck
Reset Sdata_out
Reset Sck
Waitus 5
Set Sck
Set Sdata_out
Reset Sck
Return


'******************'
' CONNECTION RESET '
'******************'



Conn_reset:

Config Sdata = Output
Reset Sck
Set Sdata_out
For I = 1 To 12
Set Sck
Reset Sck
Next I
Goto Trans_start
Return


'**************'
' COMMAND SEND '
'**************'


Send_cmd:
Config Sdata = Output
Shiftout Sdata_out , Sck , Command , 1 ', 8 , 1 'data change when sck goes low, 8bit out, 1uS delay
Ddrd.1 = 0
Reset Sck
Set Sck
Bitwait Sdata , Reset 'Wait ACK
Reset Sck
Return




Sensor_readout:
Config Sdata = Input
Shiftin Sdata , Sck , Msb , 1 , 8 , 1
Config Sdata = Output
Reset Sdata_out
Set Sck
Reset Sck
Config Sdata = Input
Shiftin Sdata , Sck , Lsb , 1 , 8 , 1
Config Sdata = Output

Set Sdata_out 'pull-up ACK (CRC not use)
Set Sck
Reset Sck


Return


Write_status:
Gosub Trans_start
Command = &B00000110
Gosub Send_cmd
Command = &B00000001 'Switch to 8/12 bit resolution
Gosub Send_cmd
Return

Rotina_chuva:

Home

Cls

Set Portb.7

Lcd "*Esta a Chover*"

Waitms 500

Goto Start1

Return

End







Actuators code (I tested and worked well)


$regfile = "m16def.dat"
$crystal = 4000000

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portd.3 , Db5 = Portd.2 , Db6 = Portd.1 , Db7 = Portd.0 , E = Portd.6 , Rs = Portd.7

Config Portc.1 = Output
Config Portc.2 = Output
Config Portc.3 = Output
Config Portc.4 = Output

Cls

Do

Home

Lcd "All turned off"

Set Portc.1 'Cooler off
Set Portc.2 'Resistor off
Reset Portc.3 'Led off
Reset Portc.4 'Led off

Wait 5

Cls
Home

Lcd "Resistor On"

Set Portc.4 'Led on
Reset Portc.2 'Resistor on
Reset Portc.3 'Led off
Set Portc.1 'Cooler off

Wait 5

Cls
Home

Lcd "Cooler On"

Set Portc.3 'Led on
Reset Portc.4 'Led off
Set Portc.2 'resistor off
Reset Portc.1 'cooler on

Loop



My problem now is how to interface this two codes, to do what I have said previously. When Humidity is >=75% the cooler starts working, the resistor is allways on but when temperature is >30º it stops.

Please help me with this..


If you want the two codes in attachment please ask me, I put this way because you might don't have Bascom installed on your Pc.





Many thanks,

Gonçalo
By n1ist
#73147
Right now, your main program just takes one measurement, displays it, and stops (goes into an endless loop). You need to change it to make one big loop that takes the measurement, sets the outputs based on the measurements, waits a bit, and then does it all over again. That's the first step. It will work, but will likely oscillate. Once you get to that point, look into P-I or P-I-D controllers to tame it.
/mike