A little help with ADC and swordish

Status
Not open for further replies.

be80be

Well-Known Member
What I'm trying to do is save two voltage values the first being the highest
and the second the lower seeing ADC is bigger then 8 bits I get a error saying out of bounds when I try it like this
Code:
Dim highvolts As Word
Dim lowvolts As Word
Dim Counter As Word
Dim hold(2) As Word
Counter = 0
For Counter = 0 To 1                   // loop 2 times
While Get_ADC_Sample > 0          // get sample
Wend
hold(Counter) = Get_ADC_Sample     // put in hold(1)
DelayMS(20)                 // small delay
Next
highvolts = hold(1)  [COLOR="Red"]//I get a Constant expression [/COLOR]
lowvolts = hold(2) [COLOR="Red"]//violates subrange bounds here [/COLOR]

3v0

Coop Build Coordinator
Forum Supporter
The loop is saving the read values in
hold(0) and hold(1) and that is good;

But the array starts at zero so you should have

highvolts = hold(0)
lowvolts = hold(1)

be80be

Well-Known Member
That's it I keep forgetting that with swordfish and it's the same in C too Lol it worked.
I set the counter right just not calling it back right thanks 3v0

be80be

Well-Known Member
I still have a problem it's putting the same value in both
highvolts = hold(0)
lowvolts = hold(1)

be80be

Well-Known Member
Here the whole code
Code:
Device = 18F1220
Clock = 8 // tells the compiler the FOSC speed
Config OSC = INTIO2, WDT = OFF, LVP = OFF, MCLRE = OFF
Include "Utils.bas"
Include "INTOSC8.bas"
Dim right1 As PORTA.0
Dim left1 As PORTA.1
Dim highvolts As Word
Dim lowvolts As Word
Dim Counter As Word
Dim hold(2) As byte

//result = result * 2               // Scale the answer up by a factor of 3

End Function
hold(0) = 0
hold(1) = 0
Counter = 0
For Counter = 0 To 1                    // loop 2 times
While Get_ADC_Sample > 0          // get sample
Wend
hold(Counter) = Get_ADC_Sample     // put in hold()
DelayMS(20)                 // small delay
Next

SetAllDigital
TRISB =%00000000
PORTB =%00000000
Input(PORTA.0)

While True
highvolts = hold(0)
lowvolts = hold(1)

If highvolts > lowvolts  Then     // checks to see if highvols is higher
PORTB = %11111111                 // lights up portB
DelayMS(20)
Else
PORTB =%00000000                  // if not higher turns portB off
DelayMS(20)
EndIf

Wend

Last edited:

3v0

Coop Build Coordinator
Forum Supporter
You need to have someone who programs in SF to look at this. There are things I have doubts about that may be clear the them.

This is what I see as you main code. You read the ADC just the twice and go into a loop using the same values for hold(0) and hold (1) over and over.

Code:
hold(0) = 0
hold(1) = 0
Counter = 0
For Counter = 0 To 1          // loop 2 times
While Get_ADC_Sample > 0    // get sampl
Wend
hold(Counter) = Get_ADC_Sample // put in hold()
DelayMS(20)                 // small delay
Next

SetAllDigital
TRISB =%00000000
PORTB =%00000000
Input(PORTA.0)

While True
highvolts = hold(0)
lowvolts = hold(1)

If highvolts > lowvolts  Then     // checks to see if highvols is higher
PORTB = %11111111                 // lights up portB
DelayMS(20)
Else
PORTB =%00000000                  // if not higher turns portB off
DelayMS(20)
EndIf

Wend

be80be

Well-Known Member
This make the portb flash when the adc value changes but not what i'm trying for.
Code:
Device = 18F1220
Clock = 8 // tells the compiler the FOSC speed
Config OSC = INTIO2, WDT = OFF, LVP = OFF, MCLRE = OFF
Include "Utils.bas"
Include "INTOSC8.bas"
Dim right1 As PORTA.0
Dim left1 As PORTA.1
Dim highvolts As Word
Dim lowvolts As Word
Dim Counter As Word
Dim hold(2) As word

//result = result * 2               // Scale the answer up by a factor of 3

End Function

SetAllDigital
TRISB =%00000000
PORTB =%00000000
Input(PORTA.0)

While True
Counter = 0
For Counter = 0 To 1                    // loop 2 times
While Get_ADC_Sample = 0          // get sample
Wend
hold(Counter) = Get_ADC_Sample     // put in hold()
DelayMS(20)                 // small delay
Next
highvolts = hold(0)
lowvolts = hold(1)

If highvolts > lowvolts  Then     // checks to see if highvols is higher
PORTB = %11111111                 // lights up portB
DelayMS(20)
Else
PORTB =%00000000                  // if not higher turns portB off
DelayMS(20)
EndIf

Wend
I didn't read your post till after mine but that's what's happening
This is what I see as you main code. You read the ADC just the twice and go into a loop using the same values for hold(0) and hold (1) over and over.
but I moved
the for next down in to the while wend loop. So now it's just flashes the leds as it changes.

Last edited:

be80be

Well-Known Member
The thing is it's happening so fast if i didn't have the delay you couldn't see it. It's reading
the light bulb flicker in the room. No it's the power supply it's reading I changed it to this. I'm getting two values. I need to figure out how to set a high range and a low range
Code:
While True
Counter = 0
For Counter = 0 To 1                    // loop 2 times
While Get_ADC_Sample = 0          // get sample
Wend
hold(Counter) = Get_ADC_Sample     // put in hold()
DelayMS(100)                 // small delay
Next
highvolts = hold(0)
lowvolts = hold(1)

If highvolts > lowvolts  Then     // checks to see if highvols is higher
PORTB = hold(0)                 // lights up portB
DelayMS(20)
Else
PORTB =hold(1)                 // if not higher turns portB off
DelayMS(20)
EndIf

Wend

Last edited:

Pommie

Well-Known Member
I'm not sure what you are trying to do but a few things stand out as possible problems.

You have SetAllDigital when you need analogue pins.

If you can describe what the program is supposed to do then it is much easier to write it.

Mike.

Pommie

Well-Known Member
Burt,

Here's a little program that lights the Junebug LEDs as you move VR1.

To use it set all DIP switches to ON.
Code:
Device = 18F1320
Clock = 4 // 4MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF

Include "18F1320.bas"

GO=1
While(GO=1)
Wend
End Function

ADCON0 = %00000001      //A2D on and select AN0
ADCON2 = %10110101      //Right justify - Fosc/16
End Sub

//main code starts here
Dim Result As Word
Upper=0
Lower=$ffff while TRUE Result=ReadADC(1) //read VR1 if Result > Upper then //is it a new high Upper=Result //yes so update Upper endif If Result < Lower then //is it a new low Lower=Result //yes so update lower endif if(Upper-Result)>(Result-Lower) then //code for when result is nearer Lower else //code for when result is nearer Upper Endif Wend Mike. be80be Well-Known Member Mike that works for me Thanks what it is doing you set Upper to 0 and Lower to 1111111111111111 Then you put the value of ReadADC(1) into Result and make it = to Upper But what keeps Upper from going lower. Because I have tried it like this and the values would change till they had the same is it this part that keeps it from happen. Code: Upper=0 Lower=$ffff
that's the only thing i haven't put in my code.
So if ADC is high it is close to 0
and if it is Low it's closer to 65535 of $ffff I think that's right thanks agin. Mike 3v0 Coop Build Coordinator Forum Supporter Upper starts at zero. You only change Upper it "Result>Upper" so it can only go higher. Code:  Result=ReadADC(1) //read VR1 if Result > Upper then //is it a new high Upper=Result //yes so update Upper endif Pommie Well-Known Member Most Helpful Member To add to what 3v0 posted, think how the variables will get changed first time through the loop. Say Result is 500, the line if Result > Upper Then will result in Upper being set to 500 as 500 is greater than 0. if Result < Lower Then will result in Lower being set to 500 as 500 is less than$ffff.

And then second time through the loop.
Say the next reading is 400 then
if Result > Upper Then
will result in no change as 400 is not greater than 500.
if Result < Lower Then
will result in Lower being set to 400 as 400 is less than 500.

And so, Lower will contain the lowest value read (400) and Upper the highest (500).

Mike.

be80be

Well-Known Member
Thanks Mike I was close I just never set a value in Lower but 0 and that's why they ended up the same. You have to think in 3D here to make it work lol. It works like a charm
now for the next part. This is what I'm making Interactive LED Panels just one. I was play with ADC and a LDR and out putting the value to portb with 8 leds it looks just like one
row on one of the panels.

Last edited:
Status
Not open for further replies.

Replies
12
Views
4K
Replies
4
Views
1K
Replies
3
Views
721
Replies
1
Views
965
Replies
10
Views
8K