Congratulations on GC Basic, what great tool you've created.
Only a newbie so forgive me if I am overlooking the obvious.
16f88 at 4mhz external osc
I'm writing some code that will read a high and low reading at the (ANx) analogue input. The end user will push a button to set the current (ANx) reading and store it in the EEprom memory (1 button sets high and the other sets low). That piece of code seems fine.
Problem is I want to divide the difference beween the 2 numbers into 20 equal amounts.
(e.g) low is 25 and high is 201. The difference is 176. Then I want to divide it into 20 equal parts. so the answer would be 8.8 in this particular example. You would end up with an array with numbers like
25, 33.8, 42.6, 51.4 .....etc..... 201. don't mind rounding the numbers to make it evenly divisable by 20, just don't know how to go about it
I was trying to use a for - next loop to store the numbers in an array, using variables of course.
How does GCBasic handle floating points, or does it handle them at all? If I do a divide that results in an answer with a point something on the end do I have to define it as a word? or do I need to process the numbers first so they are evenly divisable by 20. I don't know the values before hand so I don't think I can use a lookup table.
To make it harder the code has to handle the possiblity that the low and high values might be the other way around. Instead of low being say 10 and high being 190 it could be that low is 210 and high is 50. You will see my primitive attempt to deal with this.
Am I able to use the 'step' part as a variable in a For - Next loop
The whole idea is to make a device that can have a user use what ever range resistance they have (within reason) and set the low and high values. When running, the main loop the program will compare the current (ANx) to the values in the array and as a result will light a LED to corrosponding brighness (or drive a meter/gauge) by using the PWM output.
Another part that seems unstable is having 20 IF...Then's one after the other seems to make the program loop at the start and not want to proceed thru any further. If I 'rem out the last 10 lines then it seems more responsive.
I'll post some relevant code below. The whole program is a bit lengthy but it compiles without any error. Just doesn't seem to run. I have ommited sections that seem to work fine like the parts where the values get stored in memory.
Just a final note. OptionA and OptionB are meant to do the same thing but I tried doing them differently to see what GCbasic was happiest with. It seems neither
Thanks for any advice, if more code is needed just let me know. Just trying to save some space
Best Regards
Paul
' FuelHigh and FuelLow have been previously set and recovered from eeprom in other parts of the program
IF FuelHigh > FuelLow then
FuelDiff = FuelHigh - FuelLow
FuelStep = FuelDiff / 20
Counter = 1
For FuelArray = FuelLow to FuelHigh Step FuelStep
FuelSender(Counter) = FuelArray
Counter = Counter + 1
Next FuelArray
Counter = 1
END IF
'If FuelLow is greater than FuelHigh this IF will run
IF FuelHigh < FuelLow then
FuelDiff = FuelLow - FuelHigh
FuelStep = FuelDiff / 20
Counter = 21
For FuelArray = FuelHigh to FuelLow step Fuelstep
FuelSender(Counter) = FuelArray
Counter = Counter - 1
Next FuelArray
Counter = 1
END IF
'FuelGauge Array Loading
Dim FuelGauge(21)
'If GaugeHigh is greater than GaugeLow then this IF will run
If GaugeHigh > GaugeLow then
GaugeDiff = GaugeHigh - GaugeLow
GaugeStep = Gaugediff / 20
Counter = 1
For GaugeArray = GaugeLow to GaugeHigh step GaugeStep
FuelGauge(Counter) = GaugeArray
Counter = Counter + 1
next GaugeArray
Counter = 1
END IF
'If GaugeLow is greater than GaugeHigh then this IF will run
If GaugeHigh < GaugeLow then
GaugeDiff = GaugeLow - GaugeHigh
GaugeStep = GaugeDiff / 20
Counter = 21
For GaugeArray = GaugeHigh to GaugeLow Step GaugeStep
FuelGauge(Counter) = GaugeArray
Counter = Counter - 1
Next GaugeArray
Counter = 1
END IF
'Flash led to see if program gets this far
set pgmled on
wait 50 10ms
set pgmled off
wait 50 10ms
GCBASIC doesn't handle floating points at all, but I was able to get some pretty good results by multiplying everything by 100. This code fills elements 1 to 20 of an array with (roughly) evenly spaced numbers:
Dim Parts(20)
FillParts 25, 201
Sub FillParts(LowIn, HighIn)
Dim PartSize As Word
'Swap if LowIn > HighIn
If LowIn > HighIn Then
Temp = LowIn
LowIn = HighIn
HighIn = Temp
End If
'Get size of each gap, multiplied by 100 to increase accuracy
PartSize = (HighIn - LowIn) * [word]100 / 19
'Fill array
For Temp = 1 to 20
Parts(Temp) = LowIn + (Temp - 1) * PartSize / 100
Next
End Sub
After running the routine, the array contains these numbers:
23, 32, 41, 50, 60, 69, 78, 87, 97, 106, 115, 124, 134, 143, 152, 161, 171, 180, 189, 198
As for the Ifs, I'm not quite sure what you're trying to do, but it'd be better to do it using a loop, and accessing the array elements directly. Under optiona, I'd try something like this:
optiona:
CurrentFuel = ReadAD(AN4)
For CurrReading = 1 to 20
If CurrentFuel >= FuelSender(CurrReading) Then
HPWM 1, 1, FuelGauge(CurrReading)
Exit For
End If
Next
I've not tried that code, but it should work. It'll save quite a bit of RAM - copying every array element into a temporary variable isn't a good idea unless the same element is accessed over and over again. The code under optionb would be somewhat similar, but you'll need to change the >= in the If. The Exit For exits from the for loop once it has found an appropriate output value - this is the equivalent of the > and < in your original code.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Congratulations on GC Basic, what great tool you've created.
Only a newbie so forgive me if I am overlooking the obvious.
16f88 at 4mhz external osc
I'm writing some code that will read a high and low reading at the (ANx) analogue input. The end user will push a button to set the current (ANx) reading and store it in the EEprom memory (1 button sets high and the other sets low). That piece of code seems fine.
Problem is I want to divide the difference beween the 2 numbers into 20 equal amounts.
(e.g) low is 25 and high is 201. The difference is 176. Then I want to divide it into 20 equal parts. so the answer would be 8.8 in this particular example. You would end up with an array with numbers like
25, 33.8, 42.6, 51.4 .....etc..... 201. don't mind rounding the numbers to make it evenly divisable by 20, just don't know how to go about it
I was trying to use a for - next loop to store the numbers in an array, using variables of course.
How does GCBasic handle floating points, or does it handle them at all? If I do a divide that results in an answer with a point something on the end do I have to define it as a word? or do I need to process the numbers first so they are evenly divisable by 20. I don't know the values before hand so I don't think I can use a lookup table.
To make it harder the code has to handle the possiblity that the low and high values might be the other way around. Instead of low being say 10 and high being 190 it could be that low is 210 and high is 50. You will see my primitive attempt to deal with this.
Am I able to use the 'step' part as a variable in a For - Next loop
The whole idea is to make a device that can have a user use what ever range resistance they have (within reason) and set the low and high values. When running, the main loop the program will compare the current (ANx) to the values in the array and as a result will light a LED to corrosponding brighness (or drive a meter/gauge) by using the PWM output.
Another part that seems unstable is having 20 IF...Then's one after the other seems to make the program loop at the start and not want to proceed thru any further. If I 'rem out the last 10 lines then it seems more responsive.
I'll post some relevant code below. The whole program is a bit lengthy but it compiles without any error. Just doesn't seem to run. I have ommited sections that seem to work fine like the parts where the values get stored in memory.
Just a final note. OptionA and OptionB are meant to do the same thing but I tried doing them differently to see what GCbasic was happiest with. It seems neither
Thanks for any advice, if more code is needed just let me know. Just trying to save some space
Best Regards
Paul
' FuelHigh and FuelLow have been previously set and recovered from eeprom in other parts of the program
IF FuelHigh > FuelLow then
FuelDiff = FuelHigh - FuelLow
FuelStep = FuelDiff / 20
Counter = 1
For FuelArray = FuelLow to FuelHigh Step FuelStep
FuelSender(Counter) = FuelArray
Counter = Counter + 1
Next FuelArray
Counter = 1
END IF
'If FuelLow is greater than FuelHigh this IF will run
IF FuelHigh < FuelLow then
FuelDiff = FuelLow - FuelHigh
FuelStep = FuelDiff / 20
Counter = 21
For FuelArray = FuelHigh to FuelLow step Fuelstep
FuelSender(Counter) = FuelArray
Counter = Counter - 1
Next FuelArray
Counter = 1
END IF
'FuelGauge Array Loading
Dim FuelGauge(21)
'If GaugeHigh is greater than GaugeLow then this IF will run
If GaugeHigh > GaugeLow then
GaugeDiff = GaugeHigh - GaugeLow
GaugeStep = Gaugediff / 20
Counter = 1
For GaugeArray = GaugeLow to GaugeHigh step GaugeStep
FuelGauge(Counter) = GaugeArray
Counter = Counter + 1
next GaugeArray
Counter = 1
END IF
'If GaugeLow is greater than GaugeHigh then this IF will run
If GaugeHigh < GaugeLow then
GaugeDiff = GaugeLow - GaugeHigh
GaugeStep = GaugeDiff / 20
Counter = 21
For GaugeArray = GaugeHigh to GaugeLow Step GaugeStep
FuelGauge(Counter) = GaugeArray
Counter = Counter - 1
Next GaugeArray
Counter = 1
END IF
'Flash led to see if program gets this far
set pgmled on
wait 50 10ms
set pgmled off
wait 50 10ms
'Write FuelSender Array into Temp Variables
fs1 = FuelSender(1)
fs2 = FuelSender(2)
fs3 = FuelSender(3)
fs4 = FuelSender(4)
fs5 = FuelSender(5)
fs6 = FuelSender(6)
fs7 = FuelSender(7)
fs8 = FuelSender(8)
fs9 = FuelSender(9)
fs10 = FuelSender(10)
fs11 = FuelSender(11)
fs12 = FuelSender(12)
fs13 = FuelSender(13)
fs14 = FuelSender(14)
fs15 = FuelSender(15)
fs16 = FuelSender(16)
fs17 = FuelSender(17)
fs18 = FuelSender(18)
fs19 = FuelSender(19)
fs20 = FuelSender(20)
fs21 = FuelSender(21)
'Write FuelGauge Array into Temp Variables
fg1 = FuelGauge(1)
fg2 = FuelGauge(2)
fg3 = FuelGauge(3)
fg4 = FuelGauge(4)
fg5 = FuelGauge(5)
fg6 = FuelGauge(6)
fg7 = FuelGauge(7)
fg8 = FuelGauge(8)
fg9 = FuelGauge(9)
fg10 = FuelGauge(10)
fg11 = FuelGauge(11)
fg12 = FuelGauge(12)
fg13 = FuelGauge(13)
fg14 = FuelGauge(14)
fg15 = FuelGauge(15)
fg16 = FuelGauge(16)
fg17 = FuelGauge(17)
fg18 = FuelGauge(18)
fg19 = FuelGauge(19)
fg20 = FuelGauge(20)
fg21 = FuelGauge(21)
'Flash LED to see if program gets this far
pwmon
set pgmled on
wait 50 10ms
set pgmled off
wait 50 10ms
ChkSender:
'Check for Mode switch
if SenderMode on then goto ProgSender
if GaugeMode on then goto ProgGauge
'Read the Cars Fuel Sender (AN4) as variable CURRENTFUEL and compare to table
'This is 1st Senario Where FuelSender 1 is Greater than FuelSender 21
IF fs1 > fs21 then goto optiona
IF fs1 < fs21 then goto optionb
goto chksender
optiona:
CurrentFuel = ReadAD(AN4)
if CurrentFuel >= fs1 then
hpwm 1, 1, fg1
end if
if CurrentFuel < fs1 and CurrentFuel >= fs2 then
hpwm 1, 1, fg2
end if
if CurrentFuel < fs2 and CurrentFuel >= fs3 then
hpwm 1, 1, fg3
end if
if CurrentFuel < fs3 and CurrentFuel >= fs4 then
hpwm 1, 1, fg4
end if
if CurrentFuel < fs4 and CurrentFuel >= fs5 then
hpwm 1, 1, fg5
end if
if CurrentFuel < fs5 and CurrentFuel >= fs6 then
hpwm 1, 1, fg6
end if
if CurrentFuel < fs6 and CurrentFuel >= fs7 then
hpwm 1, 1, fg7
end if
if CurrentFuel < fs7 and CurrentFuel >= fs8 then
hpwm 1, 1, fg8
end if
if CurrentFuel < fs8 and CurrentFuel >= fs9 then
hpwm 1, 1, fg9
end if
if CurrentFuel < fs9 and CurrentFuel >= fs10 then
hpwm 1, 1, fg10
end if
if CurrentFuel < fs10 and CurrentFuel >= fs11 then
hpwm 1, 1, fg11
end if
if CurrentFuel < fs11 and CurrentFuel >= fs12 then
hpwm 1, 1, fg12
end if
if CurrentFuel < fs12 and CurrentFuel >= fs13 then
hpwm 1, 1, fg13
end if
if CurrentFuel < fs13 and CurrentFuel >= fs14 then
hpwm 1, 1, fg14
end if
if CurrentFuel < fs14 and CurrentFuel >= fs15 then
hpwm 1, 1, fg15
end if
if CurrentFuel < fs15 and CurrentFuel >= fs16 then
hpwm 1, 1, fg16
end if
if CurrentFuel < fs16 and CurrentFuel >= fs17 then
hpwm 1, 1, fg17
end if
if CurrentFuel < fs17 and CurrentFuel >= fs18 then
hpwm 1, 1, fg18
end if
if CurrentFuel < fs18 and CurrentFuel >= fs19 then
hpwm 1, 1, fg19
end if
if CurrentFuel < fs19 and CurrentFuel >= fs20 then
hpwm 1, 1, fg20
end if
if CurrentFuel < fs20 and CurrentFuel >= fs21 then
hpwm 1, 1, fg21
end if
if CurrentFuel < fs21 then
hpwm 1, 1, fg21
end if
wait 50 10ms
goto ChkSender
'This is the Second Senario where Fuel Sender 1 < Fuel Sender 21
optionb:
CurrentFuel = ReadAD(AN4)
if CurrentFuel <= fs1 then hpwm 1, 1, fg1
'if CurrentFuel > fs1 and CurrentFuel <= fs2 then hpwm 1, 1, fg2
'if CurrentFuel > fs2 and CurrentFuel <= fs3 then hpwm 1, 1, fg3
'if CurrentFuel > fs3 and CurrentFuel <= fs4 then hpwm 1, 1, fg4
'if CurrentFuel > fs4 and CurrentFuel <= fs5 then hpwm 1, 1, fg5
'if CurrentFuel > fs5 and CurrentFuel <= fs6 then hpwm 1, 1, fg6
'if CurrentFuel > fs6 and CurrentFuel <= fs7 then hpwm 1, 1, fg7
'if CurrentFuel > fs7 and CurrentFuel <= fs8 then hpwm 1, 1, fg8
'if CurrentFuel > fs8 and CurrentFuel <= fs9 then hpwm 1, 1, fg9
'if CurrentFuel > fs9 and CurrentFuel <= fs10 then hpwm 1, 1, fg10
'if CurrentFuel > fs10 and CurrentFuel <= fs11 then hpwm 1, 1, fg11
'if CurrentFuel > fs11 and CurrentFuel <= fs12 then hpwm 1, 1, fg12
'if CurrentFuel > fs12 and CurrentFuel <= fs13 then hpwm 1, 1, fg13
'if CurrentFuel > fs13 and CurrentFuel <= fs14 then hpwm 1, 1, fg14
'if CurrentFuel > fs14 and CurrentFuel <= fs15 then hpwm 1, 1, fg15
'if CurrentFuel > fs15 and CurrentFuel <= fs16 then hpwm 1, 1, fg16
'if CurrentFuel > fs16 and CurrentFuel <= fs17 then hpwm 1, 1, fg17
'if CurrentFuel > fs17 and CurrentFuel <= fs18 then hpwm 1, 1, fg18
'if CurrentFuel > fs18 and CurrentFuel <= fs19 then hpwm 1, 1, fg19
'if CurrentFuel > fs19 and CurrentFuel <= fs20 then hpwm 1, 1, fg20
'if CurrentFuel > fs20 and CurrentFuel <= fs21 then hpwm 1, 1, fg21
'if CurrentFuel > fs21 then hpwm 1, 1, fg21
wait 50 10ms
goto ChkSender
GCBASIC doesn't handle floating points at all, but I was able to get some pretty good results by multiplying everything by 100. This code fills elements 1 to 20 of an array with (roughly) evenly spaced numbers:
Dim Parts(20)
FillParts 25, 201
Sub FillParts(LowIn, HighIn)
Dim PartSize As Word
'Swap if LowIn > HighIn
If LowIn > HighIn Then
Temp = LowIn
LowIn = HighIn
HighIn = Temp
End If
'Get size of each gap, multiplied by 100 to increase accuracy
PartSize = (HighIn - LowIn) * [word]100 / 19
'Fill array
For Temp = 1 to 20
Parts(Temp) = LowIn + (Temp - 1) * PartSize / 100
Next
End Sub
After running the routine, the array contains these numbers:
23, 32, 41, 50, 60, 69, 78, 87, 97, 106, 115, 124, 134, 143, 152, 161, 171, 180, 189, 198
As for the Ifs, I'm not quite sure what you're trying to do, but it'd be better to do it using a loop, and accessing the array elements directly. Under optiona, I'd try something like this:
optiona:
CurrentFuel = ReadAD(AN4)
For CurrReading = 1 to 20
If CurrentFuel >= FuelSender(CurrReading) Then
HPWM 1, 1, FuelGauge(CurrReading)
Exit For
End If
Next
I've not tried that code, but it should work. It'll save quite a bit of RAM - copying every array element into a temporary variable isn't a good idea unless the same element is accessed over and over again. The code under optionb would be somewhat similar, but you'll need to change the >= in the If. The Exit For exits from the for loop once it has found an appropriate output value - this is the equivalent of the > and < in your original code.
Thanks Hugh, much appreciated