PDA

View Full Version : If ... Then Problem



Roddy Wayne
- 5th July 2012, 03:23
Hi All,

I'm using an Ultrasonic Sensor to determine the level of fuel in a storage tank.
This Sensor puts out a 0 to 5 Volt analog signal depending on the level in the tank.
This problem appears when the analog voltage is 1 Volt or less.
I'm using If ... Then's to determine the current fuel level. Every level seems to
be fine except for Screen21 and Screen19 just above the "End" statement. For
some unknown reason, Screen21 & Screen19 alternate when this code is running.
(When the analog voltage is .5 Volt, Screen21 should be called up. When the
voltage is 1 Volt, Screen19 should be on). I've put the following code line in my
program in order to troubleshoot this problem without success:

Debug 12, "Value: ", Bin8 A2D_V 'Display the Binary value

Any Help at all, will be greatly appreciated!!!

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 5/2/2012 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
CLEAR ;always start with clear
DEFINE OSC 4 ;define oscillator speed
DEFINE ADC_BITS 8 ; Sets number of bits in result
DEFINE ADC_CLOCK 3 ; Sets clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ; Sets sampling time in uS
define DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
define DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0
DEFINE DEBUG_PACING 1000
define BUTTON_PAUSE 50
Low PORTE.2 ;makes low for write only
TRISA = %00001111 ;sets 0, 1, 2, and 3 as porta inputs (A to D's & ref's)
TRISB = %00111111 ;sets bits 6 & 7 portb pins as outputs, 0 thru 5 as inputs
TRISC = %10111111 ;sets port c.6 for serial output
TRISD = %00000011 ;sets PORTD 0,1, lines to inputs, rest to outputs
TRISE = %00000000 ;sets all PORTE lines to outputs

A2D_V var byte ;created to store Tank's Fuel Level

Start:
pause 100
if portc.0 = 0 then start 'If portc.0 = 1, take a measurement

Pause 2000
ADCON1=%00000010 'Setup to read analog inputs
ADCIN 0, A2D_V 'Read channel 0 to A2D_Value
Debug 12, "Value: ", Bin8 A2D_V 'Display the Binary value
pause 2000
If (A2D_V <=%00011001) then Screen21
' 0 25
If (A2D_V >=%00011010)and(A2D_V <=%00110010) then Screen19
' 26 50
If (A2D_V >=%00110011)and(A2D_V <=%01001011) then Screen17
' 51 75
If (A2D_V >=%01001100)and(A2D_V <=%01100100) then Screen15
' 76 100
If (A2D_V >=%01100101)and(A2D_V <=%01111101) then Screen13
' 101 125
If (A2D_V >=%01111110)and(A2D_V <=%10010110) then Screen11
' 126 150
If (A2D_V >=%10010111)and(A2D_V <=%10101111) then Screen9
' 151 175
If (A2D_V >=%10110000)and(A2D_V <=%11001000) then Screen7
' 176 200
If (A2D_V >=%11001001)and(A2D_V <=%11100001) then Screen5
' 201 225
If (A2D_V >=%11100010)and(A2D_V <=%11111010) then Screen3
' 226 250
If (A2D_V >=%11111011) then Screen1
' > or = to 251



Pause 2000
Goto Start

Screen1:
pause 2000
Debug 12, 27, 69, "1", 32 '100% Fuel Remaining Screen
Goto Start

Screen3:
Pause 2000
Debug 12, 27, 69, "3", 32 '90% Fuel Remaining Screen
Goto Start

Screen5:
Pause 2000
Debug 12, 27, 69, "5", 32 '80% Fuel Remaining Screen
goto Start

Screen7:
pause 2000
Debug 12, 27, 69, "7", 32 '70% Fuel Remaining Screen
goto Start

Screen9:
Pause 2000
Debug 12, 27, 69, "9", 32 '60% Fuel Remaining Screen
goto Start

Screen11:
Pause 2000
Debug 12, 27, 69, "11", 32 '50% Fuel Remaining Screen
goto Start

Screen13:
Pause 2000
Debug 12, 27, 69, "13", 32 '40% Fuel Remaining Screen
goto Start

Screen15:
Pause 2000
Debug 12, 27, 69, "15", 32 '30% Fuel Remaining Screen
goto Start

Screen17:
pause 2000
Debug 12, 27, 69, "17", 32 '20% Fuel Remaining Screen
goto Start

Screen19:
Pause 2000
Debug 12, 27, 69, "19", 32 '10% Fuel Remaining Screen
Pause 2000

Screen21:
Pause 2000
Debug 12, 27, 69, "21", 32 'Out of Fuel Screen
Pause 2000
goto Start

End

HenrikOlsson
- 5th July 2012, 06:06
A missing Goto Start at the end of the Screen19 routine perhaps?

/Henrik.

Roddy Wayne
- 5th July 2012, 07:16
I must be tired to have missed something this obvious!
The problem has been solved.
Thank You Henrik!