Discussion forum for the BasicX family of microcontroller chips.
CStr to CInt Question - Conrad A***** - Apr 14 18:29:04 2008
I am having an issue with converting a string into a integer for the
use of a do-loop. The section I am having difficult with is the
section with ">>>>". This is what I have:
'---------------------------------------------------------------------
Option Explicit
'BX24 Pinout
Const RedVolt As Byte = 8
Const GreenVolt As Byte = 9
Const VIn As Byte = 13
'---------------------------------------------------------------------
Public Sub BatteryMon()
Dim A As Single
Dim X As String
Dim Z As String
Dim SleepTime As Single
Do
A = (3.206864*5.0*(CSng(GetADC(VIn))))/1024.0 'A/D
conversion
Z = CStr(A)
If (A < 10.0) Then
X = Mid(Z, 1, 1) & Mid(Z, 2, 2) 'Rounds under
9.9v to X.X
ElseIf (A >= 10.0) Then
X = Mid(Z, 1, 2) & Mid(Z, 3, 2) 'Rounds over
10v to 1X.X
End If
If (A < 9.0) Then 'If the battery voltage is less
than 9.0 volts
SleepTime = 0.1 'sleep time for coil output
ElseIf (A < 10.5) Then 'If the battery voltage is 9.1
to 10.5 volts
SleepTime = 0.25
ElseIf (A < 12.0) Then 'If the battery voltage is
10.6 to 12.0 volts
SleepTime = 0.5
ElseIf (A < 13.5) Then 'If the battery voltage is
12.1 to 13.5 volts
SleepTime = 0.75
ElseIf (A > 13.6) Then 'If the battery voltage is
greater than 13.6 volts
SleepTime = 1.0
End If
Call VoltmeterLED(Z)
Call Sleep(0.1)
Loop
End Sub
'---------------------------------------------------------------------
Public Sub VoltmeterLED(ByVal X As String)
Dim FirstDigit As String
Dim SecondDigit As String
Dim SecondDigitInt As Single
Dim ThirdDigit As String
Dim ThirdDigitInt As Single
Do
Debug.Print "Begin"
FirstDigit = Mid(X, 1, 1)
Debug.Print FirstDigit
>>>> How do I convert the FirstDigit (in this case if the input
voltage is 12.5v, the FirstDigit value will be "1" (as per the
Debug.Print). Now I want to convert that string value to an integer
so it can be used as "HowManyTimes" to control how many times the LED
will blink. In essence this will be a voltmeter using a single LED
(dual color) which will blink the voltage.
SecondDigit = Mid(X, 2, 1)
Debug.Print SecondDigit
'SecondDigitInt = CSng(SecondDigit)
ThirdDigit = Mid(X, 4, 1)
Debug.Print ThirdDigit
'ThirdDigitInt = CSng(ThirdDigit)
Debug.Print "End"
'Call GreenBlinkSlow(FirstDigit)
'Call RedBlinkFast(SecondDigitInt)
'Call GreenBlinkFast(ThirdDigitInt)
Loop
End Sub
'---------------------------------------------------------------------
Public Sub GreenBlinkSlow (byVal HowManyTimes as Integer)
Dim I as Integer
For I = 0 to HowManyTimes
Call PutPin(GreenVolt, 1)
Call Sleep(2.0)
Call PutPin(GreenVolt, 0)
Call Sleep(1.0)
Next
End Sub
'---------------------------------------------------------------------
Public Sub GreenBlinkFast (byVal HowManyTimes as Integer)
Dim I as Integer
For I = 0 to HowManyTimes
Call PutPin(GreenVolt, 1)
Call Sleep(1.0)
Call PutPin(GreenVolt, 0)
Call Sleep(1.0)
Next
End Sub
'---------------------------------------------------------------------
Public Sub RedBlinkFast (byVal HowManyTimes as Integer)
Dim I as Integer
For I = 0 to HowManyTimes
Call PutPin(RedVolt, 1)
Call Sleep(1.0)
Call PutPin(RedVolt, 0)
Call Sleep(1.0)
Next
End Sub
'---------------------------------------------------------------------
Once I can figure out how to do the first digit, then the 2nd and 3rd
will be a duplicate essentially. I have tried CInt(FirstDigit) and
it just doesn't allow me. Any ideas on what I can do?
------------------------------------
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.
(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )
Re: CStr to CInt Question - Tom Becker - Apr 14 20:22:24 2008
I think the instruction your looking for is ASC(). You can convert an
ASCII digit, "0" through "9", to its numerical value by subtracting
ASC("0") from it ASCII value, e.g:
> FirstDigit = Mid(X, 1, 1)
HowManyTimes = CInt(Asc(FirstDigit) - Asc("0"))
Tom
------------------------------------

(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )
RE: CStr to CInt Question - "James R. Parish" - Apr 15 8:03:22 2008
Conrad, check out the FMT function... It will eliminate a lot of code
for you...
The way "I" would do it is this. Convert the voltage to a FIXED one
decimal value using FMT( VOLTAGE,1). Then I would just step through the
string FOR X = 1 to LEN(FMT(VOLTAGE,1)) and just have a routine called
to do the blinking, when you get to the decimal point do a yellow blink.
A Red/Green led can put out yellow/orange with an "AC" voltage on it,
not so good on a SMT like onboard the BX24 though. Just have a routine
on two pins switch L-H at about 120 Hz should do it. Looks better on a
opaque T1 round Bi-Color LED for sure. You should be able to just use
CASE SELECT on the FOR-NEXT counter X to just step through the string.
LEN of 3 means less than 10 ie 9.0, LEN of 4 means > 10 ie 11.0
From: b...@yahoogroups.com [mailto:b...@yahoogroups.com] On
Behal...*****
Sent: Monday, April 14, 2008 6:27 PM
To: b...@yahoogroups.com
Subject: [BasicX] CStr to CInt Question
I am having an issue with converting a string into a integer for the
use of a do-loop. The section I am having difficult with is the
section with ">>>>". This is what I have:
'----------------------------------------------------------
Option Explicit
'BX24 Pinout
Const RedVolt As Byte = 8
Const GreenVolt As Byte = 9
Const VIn As Byte = 13
'----------------------------------------------------------
Public Sub BatteryMon()
Dim A As Single
Dim X As String
Dim Z As String
Dim SleepTime As Single
Do
A = (3.206864*5.0*(CSng(GetADC(VIn))))/1024.0 'A/D
conversion
Z = CStr(A)
If (A < 10.0) Then
X = Mid(Z, 1, 1) & Mid(Z, 2, 2) 'Rounds under
9.9v to X.X
ElseIf (A >= 10.0) Then
X = Mid(Z, 1, 2) & Mid(Z, 3, 2) 'Rounds over
10v to 1X.X
End If
If (A < 9.0) Then 'If the battery voltage is less
than 9.0 volts
SleepTime = 0.1 'sleep time for coil output
ElseIf (A < 10.5) Then 'If the battery voltage is 9.1
to 10.5 volts
SleepTime = 0.25
ElseIf (A < 12.0) Then 'If the battery voltage is
10.6 to 12.0 volts
SleepTime = 0.5
ElseIf (A < 13.5) Then 'If the battery voltage is
12.1 to 13.5 volts
SleepTime = 0.75
ElseIf (A > 13.6) Then 'If the battery voltage is
greater than 13.6 volts
SleepTime = 1.0
End If
Call VoltmeterLED(Z)
Call Sleep(0.1)
Loop
End Sub
'----------------------------------------------------------
Public Sub VoltmeterLED(ByVal X As String)
Dim FirstDigit As String
Dim SecondDigit As String
Dim SecondDigitInt As Single
Dim ThirdDigit As String
Dim ThirdDigitInt As Single
Do
Debug.Print "Begin"
FirstDigit = Mid(X, 1, 1)
Debug.Print FirstDigit
>>>> How do I convert the FirstDigit (in this case if the input
voltage is 12.5v, the FirstDigit value will be "1" (as per the
Debug.Print). Now I want to convert that string value to an integer
so it can be used as "HowManyTimes" to control how many times the LED
will blink. In essence this will be a voltmeter using a single LED
(dual color) which will blink the voltage.
SecondDigit = Mid(X, 2, 1)
Debug.Print SecondDigit
'SecondDigitInt = CSng(SecondDigit)
ThirdDigit = Mid(X, 4, 1)
Debug.Print ThirdDigit
'ThirdDigitInt = CSng(ThirdDigit)
Debug.Print "End"
'Call GreenBlinkSlow(FirstDigit)
'Call RedBlinkFast(SecondDigitInt)
'Call GreenBlinkFast(ThirdDigitInt)
Loop
End Sub
'----------------------------------------------------------
Public Sub GreenBlinkSlow (byVal HowManyTimes as Integer)
Dim I as Integer
For I = 0 to HowManyTimes
Call PutPin(GreenVolt, 1)
Call Sleep(2.0)
Call PutPin(GreenVolt, 0)
Call Sleep(1.0)
Next
End Sub
'----------------------------------------------------------
Public Sub GreenBlinkFast (byVal HowManyTimes as Integer)
Dim I as Integer
For I = 0 to HowManyTimes
Call PutPin(GreenVolt, 1)
Call Sleep(1.0)
Call PutPin(GreenVolt, 0)
Call Sleep(1.0)
Next
End Sub
'----------------------------------------------------------
Public Sub RedBlinkFast (byVal HowManyTimes as Integer)
Dim I as Integer
For I = 0 to HowManyTimes
Call PutPin(RedVolt, 1)
Call Sleep(1.0)
Call PutPin(RedVolt, 0)
Call Sleep(1.0)
Next
End Sub
'----------------------------------------------------------
Once I can figure out how to do the first digit, then the 2nd and 3rd
will be a duplicate essentially. I have tried CInt(FirstDigit) and
it just doesn't allow me. Any ideas on what I can do?
[Non-text portions of this message have been removed]
------------------------------------
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.
(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )
Re: CStr to CInt Question - Conrad A***** - Apr 15 14:02:27 2008
This is what I have so far...not much:
'---------------------------------------------------------------------
Public Sub VoltmeterLED(ByVal A As Single)
Dim I As Integer
Dim Voltage As String
Dim VoltLength As Integer
Voltage = Fmt(A, 1) 'A is voltage as calculated from
BatteryMon() sub, ex. 8.784428 for used 9v battery, Voltage = 8.8
VoltLength = Len(Voltage) 'If 3 positions, then less than or
equal to 9.9v. If 4 then greater than or equal to 10.0v
If (VoltLength = 3) Then
For I = 1 to 3 Step +1
???
Next
ElseIf (VoltLength = 4) Then
For I = 1 to 4 Step +1
???
Next
End If
'---------------------------------------------------------------------
Now how do I go about cutting the Voltage into each digit? Then
assigning each digit as an integer?
Tom thanks for the advice, I tried it but got a value of 255 for some
reason?
I've never used some of these commands so that's largely why I am
confused. Haha.
Conrad
--- In b...@yahoogroups.com, "James R. Parish"
wrote:
>
> Conrad, check out the FMT function... It will eliminate a lot of
code
> for you...
>
>
>
> The way "I" would do it is this. Convert the voltage to a FIXED one
> decimal value using FMT( VOLTAGE,1). Then I would just step
through the
> string FOR X = 1 to LEN(FMT(VOLTAGE,1)) and just have a routine
called
> to do the blinking, when you get to the decimal point do a yellow
blink.
> A Red/Green led can put out yellow/orange with an "AC" voltage on
it,
> not so good on a SMT like onboard the BX24 though. Just have a
routine
> on two pins switch L-H at about 120 Hz should do it. Looks better
on a
> opaque T1 round Bi-Color LED for sure. You should be able to just
use
> CASE SELECT on the FOR-NEXT counter X to just step through the
string.
> LEN of 3 means less than 10 ie 9.0, LEN of 4 means > 10 ie 11.0
>
>
>
> From: b...@yahoogroups.com [mailto:b...@yahoogroups.com] On
> Behal...*****
> Sent: Monday, April 14, 2008 6:27 PM
> To: b...@yahoogroups.com
> Subject: [BasicX] CStr to CInt Question
>
>
>
> I am having an issue with converting a string into a integer for
the
> use of a do-loop. The section I am having difficult with is the
> section with ">>>>". This is what I have:
>
> '----------------------------------------------------------
> Option Explicit
>
> 'BX24 Pinout
> Const RedVolt As Byte = 8
> Const GreenVolt As Byte = 9
> Const VIn As Byte = 13
> '----------------------------------------------------------
> Public Sub BatteryMon()
>
> Dim A As Single
> Dim X As String
> Dim Z As String
> Dim SleepTime As Single
>
> Do
> A = (3.206864*5.0*(CSng(GetADC(VIn))))/1024.0 'A/D
> conversion
> Z = CStr(A)
>
> If (A < 10.0) Then
> X = Mid(Z, 1, 1) & Mid(Z, 2, 2) 'Rounds under
> 9.9v to X.X
> ElseIf (A >= 10.0) Then
> X = Mid(Z, 1, 2) & Mid(Z, 3, 2) 'Rounds over
> 10v to 1X.X
> End If
>
> If (A < 9.0) Then 'If the battery voltage is less
> than 9.0 volts
> SleepTime = 0.1 'sleep time for coil output
> ElseIf (A < 10.5) Then 'If the battery voltage is 9.1
> to 10.5 volts
> SleepTime = 0.25
> ElseIf (A < 12.0) Then 'If the battery voltage is
> 10.6 to 12.0 volts
> SleepTime = 0.5
> ElseIf (A < 13.5) Then 'If the battery voltage is
> 12.1 to 13.5 volts
> SleepTime = 0.75
> ElseIf (A > 13.6) Then 'If the battery voltage is
> greater than 13.6 volts
> SleepTime = 1.0
> End If
>
> Call VoltmeterLED(Z)
>
> Call Sleep(0.1)
> Loop
>
> End Sub
> '----------------------------------------------------------
> Public Sub VoltmeterLED(ByVal X As String)
>
> Dim FirstDigit As String
> Dim SecondDigit As String
> Dim SecondDigitInt As Single
> Dim ThirdDigit As String
> Dim ThirdDigitInt As Single
>
> Do
> Debug.Print "Begin"
> FirstDigit = Mid(X, 1, 1)
> Debug.Print FirstDigit
>
> >>>> How do I convert the FirstDigit (in this case if the input
> voltage is 12.5v, the FirstDigit value will be "1" (as per the
> Debug.Print). Now I want to convert that string value to an integer
> so it can be used as "HowManyTimes" to control how many times the
LED
> will blink. In essence this will be a voltmeter using a single LED
> (dual color) which will blink the voltage.
>
> SecondDigit = Mid(X, 2, 1)
> Debug.Print SecondDigit
> 'SecondDigitInt = CSng(SecondDigit)
> ThirdDigit = Mid(X, 4, 1)
> Debug.Print ThirdDigit
> 'ThirdDigitInt = CSng(ThirdDigit)
> Debug.Print "End"
> 'Call GreenBlinkSlow(FirstDigit)
> 'Call RedBlinkFast(SecondDigitInt)
> 'Call GreenBlinkFast(ThirdDigitInt)
>
> Loop
>
> End Sub
> '----------------------------------------------------------
> Public Sub GreenBlinkSlow (byVal HowManyTimes as Integer)
>
> Dim I as Integer
>
> For I = 0 to HowManyTimes
> Call PutPin(GreenVolt, 1)
> Call Sleep(2.0)
> Call PutPin(GreenVolt, 0)
> Call Sleep(1.0)
> Next
>
> End Sub
> '----------------------------------------------------------
> Public Sub GreenBlinkFast (byVal HowManyTimes as Integer)
>
> Dim I as Integer
>
> For I = 0 to HowManyTimes
> Call PutPin(GreenVolt, 1)
> Call Sleep(1.0)
> Call PutPin(GreenVolt, 0)
> Call Sleep(1.0)
> Next
>
> End Sub
> '----------------------------------------------------------
> Public Sub RedBlinkFast (byVal HowManyTimes as Integer)
>
> Dim I as Integer
>
> For I = 0 to HowManyTimes
> Call PutPin(RedVolt, 1)
> Call Sleep(1.0)
> Call PutPin(RedVolt, 0)
> Call Sleep(1.0)
> Next
>
> End Sub
> '----------------------------------------------------------
>
> Once I can figure out how to do the first digit, then the 2nd and
3rd
> will be a duplicate essentially. I have tried CInt(FirstDigit) and
> it just doesn't allow me. Any ideas on what I can do?
>
>
>
> [Non-text portions of this message have been removed]
>
------------------------------------

(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )Re: CStr to CInt Question - Conrad A***** - Apr 22 18:14:23 2008
Well I sat down and hashed it through, this is what I did to get a
single LED to show the battery voltage. A is a public/global
variable.
'---------------------------------------------------------------------
Public Sub VoltageMonitor()
Do
A = (3.206864*5.0*(CSng(GetADC(VIn))))/1024.0 'A/D
conversion
Call Sleep(0.25) 'Read battery voltage at 4Hz
Loop
End Sub
'---------------------------------------------------------------------
Public Sub VoltageDisplay()
Dim Voltage As String
Dim VoltLength As Integer
Dim FirstDigit As String
Dim FirstDigitInt As Integer
Dim SecondDigit As String
Dim SecondDigitInt As Integer
Dim ThirdDigit As String
Dim ThirdDigitInt As Integer
Do
Voltage = Fmt(A, 1) 'formats to 1 decimal place
VoltLength = Len(Voltage) 'If 3 positions, then less
than or equal to 9.9v. If 4 then greater than or equal to 10.0v
If (VoltLength = 3) Then 'equal to or less than 9.9v
FirstDigit = Mid(Voltage, 1, 1)
FirstDigitInt = CInt(Asc(FirstDigit)- ASC
("0"))
SecondDigit = Mid(Voltage, 3, 1)
SecondDigitInt = CInt(Asc(SecondDigit)- ASC
("0"))
Call RedBlinkFast(FirstDigitInt)
Call GreenBlinkFast(SecondDigitInt)
ElseIf (VoltLength = 4) Then 'equal to or greater
than 10.0v
SecondDigit = Mid(Voltage, 2, 1)
SecondDigitInt = CInt(Asc(SecondDigit)- ASC
("0"))
ThirdDigit = Mid(Voltage, 4, 1)
ThirdDigitInt = CInt(Asc(ThirdDigit)- ASC
("0"))
Call GreenBlinkSlow() 'will always be 1,
FirstDigit doesn't need to be calculated
Call RedBlinkFast(SecondDigitInt)
Call GreenBlinkFast(ThirdDigitInt)
End If
Loop
End Sub
'---------------------------------------------------------------------
Public Sub GreenBlinkSlow()
Call PutPin(GreenVolt, 1)
Call Sleep(1.0)
Call PutPin(GreenVolt, 0)
Call Sleep(1.0)
End Sub
'---------------------------------------------------------------------
Public Sub RedBlinkFast(byVal HowManyTimes as Integer)
Dim I as Integer
For I = 1 to HowManyTimes
Call PutPin(RedVolt, 1)
Call Sleep(0.4)
Call PutPin(RedVolt, 0)
Call Sleep(0.3)
Next
Call Sleep(1.0)
End Sub
'---------------------------------------------------------------------
Public Sub GreenBlinkFast(byVal HowManyTimes as Integer)
Dim I as Integer
For I = 1 to HowManyTimes
Call PutPin(GreenVolt, 1)
Call Sleep(0.4)
Call PutPin(GreenVolt, 0)
Call Sleep(0.3)
Next
Call Sleep(1.0)
End Sub
'---------------------------------------------------------------------
Public Sub Orange()
Call PutPin(RedVolt, 1)
Call PutPin(GreenVolt, 1)
Call Sleep(1.0)
Call PutPin(RedVolt, 0)
Call PutPin(GreenVolt, 0)
Call Sleep(1.0)
End Sub
'---------------------------------------------------------------------
The VoltageMonitor and VoltageDisplay are called up as tasks, reason
being is that the VoltageMonitor "A" is used elsewhere.
I know I could clean up the code in the Do-Loop in the VoltageDisplay
to (for 9.9v or less) to:
FirstDigitInt = CInt(Asc(Mid(Voltage, 1, 1))- ASC("0"))
SecondDigitInt = CInt(Asc(Mid(Voltage, 3, 1))- ASC("0"))
Call RedBlinkFast(FirstDigitInt)
Call GreenBlinkFast(SecondDigitInt)
The odd thing is that it requires a larger buffer...and sometimes it
returns a weird value (-3xxxx for ex) for SecondDigitInt. Any
suggestions?
Conrad
------------------------------------
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.
(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )