Neocities /
Hodgepodge /
Programming / RapidQ Ohms
(this is just a screenshot; code is below that)
The icon you'll need when compiling:
(save image as ohm.ico)
The code (ohm.bas):
' cut/paste into text editor and save as "ohm.bas"
' then compile with RapidQ BASIC
' Georg Simon Ohm in a Can
' Ohm's law quick calculator
' (for convenience, so as not to have to do the math yourself)
' ported (with much editing) from my OLD version
' which was written using VB3.
$RESOURCE ohmicon as "ohm.ico" ' GET THE ICON TOO
declare sub enter_Change
declare Function ext (ByVal z As Double) As String
declare Function extohm (ByVal z As Double) As String
declare Sub go ()
declare Function reduc (ByVal z As Double) As String
declare Sub showit ()
declare sub command1_click
DECLARE FUNCTION SetFocus LIB "User32" ALIAS "SetFocus" (hwnd AS LONG) AS LONG
' No need for "setwindowlong" because it doesn't even have a minimize button
const fcolr = &HC0F0D0 ' form's background
const lcolr = &HC0F0D0 ' color for labels
const kcolr = &HC0F0D0 ' key labels
const rcolr = &HC0F0D0 ' results boxes
const ecolr = &HF0F0C0 ' the edit box
create bf as qfont
addstyles(0) ' 0 is BOLD
end create
create ohmform as qform
Color = fcolr
BorderStyle = 3 'fixed double
Caption = "Georg Simon Ohm in a Can"
Height = 240
Left = 225
Width = 420
Top = 169
' the column of labels on the left
create label3 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Type here->"
Height = 20
Left = 6
Top = 6
Width = 143
End create
create label2 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Horsepower :"
Height = 20
Left = 6
Top = 36
Width = 143
End create
create label4 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Power (watts) :"
Height = 20
Left = 6
Top = 66
Width = 143
End create
create label5 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Resistance :"
Height = 20
Left = 6
Top = 96
Width = 143
End create
create label7 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Voltage :"
Height = 20
Left = 6
Top = 126
Width = 143
End create
create label6 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Current (dc or 1phase) :"
Height = 20
Left = 6
Top = 156
Width = 143
End create
create label1 as qlabel
Alignment = 1 'Right Justify
Font = bf
Color = lcolr
Caption = "Current (if 3-phase) :"
Height = 20
Left = 6
Top = 186
Width = 143
End create
' the edit box
create enter as qedit
Color = ecolr
Font = bf
Height = 20
Left = 165
Top = 6
Width = 160
OnChange = enter_Change
End create
create command1 as qbutton
Height = 20
Font = bf
Width = 30
top = 6
left = 340
caption = "?"
onclick = command1_click
end create
' and below that, the results boxes
create lhp as qlabel
Font = bf
Autosize = 0
Caption = ""
Color = rcolr
Height = 20
Left = 165
Top = 36
Width = 200
End create
create lwatt as qlabel
Autosize = 0
Caption = ""
Color = rcolr
Font = bf
Height = 20
Left = 165
Top = 66
Width = 200
End create
create lohm as qlabel
Autosize = 0
Caption = ""
Color = rcolr
Height = 20
Font = bf
Left = 165
Top = 96
Width = 200
End create
create lvolt as qlabel
Autosize = 0
Caption = ""
Color = rcolr
Font = bf
Height = 20
Left = 165
Top = 126
Width = 200
End create
create lamp as qlabel
Autosize = 0
Caption = ""
Font = bf
Color = rcolr
Height = 20
Left = 165
Top = 156
Width = 200
End create
create lcperphase as qlabel
Autosize = 0
Caption = ""
Font = bf
Color = rcolr
Height = 20
Left = 165
Top = 186
Width = 200
End create
' at far right, the labels showing what keys to hit
create lk2 as qlabel
Alignment = 2 'Center
Font = bf
Color = kcolr
Caption = "key"
Height = 20
Left = 380
Top = 6
Width = 25
End create
create lk8 as qlabel
Alignment = 2 'Center
Font = bf
Color = kcolr
Caption = "h"
Height = 20
Left = 380
Top = 36
Width = 25
End create
create lk7 as qlabel
Alignment = 2 'Center
Font = bf
Color = kcolr
Caption = "w"
Height = 20
Left = 380
Top = 66
Width = 25
End create
create lk6 as qlabel
Alignment = 2 'Center
Color = kcolr
Font = bf
Caption = "o"
Height = 20
Left = 380
Top = 96
Width = 25
End create
create lk5 as qlabel
Alignment = 2 'Center
Color = kcolr
Caption = "v"
Font = bf
Height = 20
Left = 380
Top = 126
Width = 25
End create
create lk4 as qlabel
Alignment = 2 'Center
Color = kcolr
Font = bf
Caption = "a"
Height = 20
Left = 380
Top = 156
Width = 25
End create
create lk3 as qlabel
Alignment = 2 'Center
Color = kcolr
Font = bf
Caption = "p"
Height = 20
Left = 380
Top = 186
Width = 25
End create
End create
Dim n As Double
Dim p As Double
Dim r As Double
Dim v As Double
Dim i As Double
Dim ip As Double
Dim hp As Double
Dim unit As Double
Sub Command1_Click ()
cr$ = Chr$(13) + Chr$(10)
s$ = "Ohm's law quick calculator" + cr$
s$ = s$ + "wajamala@yahoo.com" + cr$ + cr$
s$ = s$ + "Calculates voltage, current, resistance, and wattage." + cr$
s$ = s$ + "Type in any 2 of these, and it will calculate the others." + cr$ + cr$
s$ = s$ + "End your entry with any of the following keys:" + cr$
s$ = s$ + "v (for volts), a (for amps), w (for watts), o (for ohms), h (for horsepower), p (amps per phase)" + cr$ + cr$
s$ = s$ + "Metric prefixes are as such:" + cr$
s$ = s$ + "m: milli u: micro k: kilo M: mega (capital M)" + cr$ + cr$
s$ = s$ + "Press c to clear"
messagebox (s$, "OHM", 0)
SetFocus(enter.handle)
End Sub
Sub enter_Change ()
n = Val(enter.text)
if len(enter.text)>0 then s$=right$(enter.text,1) else s$=""
If s$ = "c" Or (lhp.caption > "" And lamp.caption > "" And lvolt.caption > "" And lwatt.caption > "" And lohm.caption > "") Then
lamp.caption = ""
lcperphase.caption = ""
lvolt.caption = ""
lohm.caption = ""
lwatt.caption = ""
lhp.caption = ""
If s$ = "c" Then enter.text = ""
Exit Sub
End If
If s$ = "M" Then unit = 1000000#
If s$ = "K" Or s$ = "k" Then unit = 1000
If s$ = "m" Then unit = 1 / 1000
If s$ = "u" Then unit = 1 / 1000000#
s$ = UCase$(s$)
If s$ = "W" Then lhp.caption = "": p = n * unit: lwatt.caption = reduc(p) + ext(p) + "watts": go(): unit = 1
If s$ = "O" Then r = n * unit: lohm.caption = reduc(r) + extohm(r) + "ohms": go(): unit = 1
If s$ = "V" Then v = n * unit: lvolt.caption = reduc(v) + ext(v) + "volts": go(): keyascii = 0: unit = 1
If s$ = "A" Then lcperphase.caption = "": i = n * unit: lamp.caption = reduc(i) + ext(i) + "amps": go(): unit = 1
If s$ = "P" Then lamp.caption = "": ip = n * unit: lcperphase.caption = reduc(ip) + ext(ip) + "amps/phase": go(): unit = 1
If s$ = "H" Then lwatt.caption = "": hp = n * unit: lhp.caption = Str$(hp) + " hp": go(): unit = 1
End Sub
Function ext (ByVal z As Double) As String
If z >= 1000000# Then
z = z / 1000000#: e$ = "Mega"
ElseIf z >= 1000 Then z = z / 1000: e$ = "Kilo"
ElseIf z < .001 Then z = z * 1000000: e$ = "Micro"
ElseIf z < 1 Then z = z * 1000: e$ = "Milli"
Else
e$ = ""
End If
ext = " " + e$
End Function
Function extohm (ByVal z As Double) As String
If z >= 1000000# Then
z = z / 1000000#: e$ = "Meg"
ElseIf z >= 1000 Then z = z / 1000: e$ = "K "
ElseIf z < .001 Then z = z * 1000000#: e$ = "Micro"
ElseIf z < 1 Then z = z * 1000: e$ = "Milli"
Else
e$ = ""
End If
extohm = " " + e$
End Function
Sub go ()
enter.text = ""
' given only watts, so far, just calc hp
If lwatt.caption > "" And lhp.caption = "" Then lhp.caption = Str$(p / 746) & " hp"
' given hp but not watts
If lhp.caption > "" And lwatt.caption = "" Then p = (hp * 746): lwatt.caption = reduc(p) & ext(p) + "watts"
' given only amps, so far, just calc amps/phase
If lamp.caption > "" And lcperphase.caption = "" Then ip = i / Sqr(3): lcperphase.caption = reduc(ip) & ext(ip) & "amps/phase"
' given hp but not watts
If lcperphase.caption > "" And lamp.caption = "" Then i = ip * Sqr(3): lamp.caption = reduc(i) & ext(i) & "amps"
' given volts and amps, derive Ohms and watts
If lvolt.caption > "" And lamp.caption > "" Then
r = v / i: p = v * i
lwatt.caption = reduc(p) & ext(p) & "watts"
lhp.caption = Str$(p / 746) & "hp"
lohm.caption = reduc(r) & extohm(r) & "ohms"
Exit Sub
End If
' given volts and ohms, derive amps and watts
If lvolt.caption > "" And lohm.caption > "" Then
i = v / r: p = v * i: ip = i / Sqr(3)
lwatt.caption = reduc(p) & ext(p) & "watts"
lhp.caption = Str$(p / 746) & "hp"
lamp.caption = reduc(i) & ext(i) & "amps"
lcperphase.caption = reduc(ip) & ext(ip) & "amps/phase"
Exit Sub
End If
' given ohms and amps, ...
If lamp.caption > "" And lohm.caption > "" Then
v = i * r: p = v * i
lwatt.caption = reduc(p) & ext(p) & "watts"
lhp.caption = Str$(p / 746) & "hp"
lvolt.caption = reduc(v) & ext(v) & "volts"
Exit Sub
End If
' given watts and volts...
If lwatt.caption > "" And lvolt.caption > "" Then
lhp.caption = Str$(p / 746) & "hp"
i = p / v: r = v / i: ip = i / Sqr(3)
lamp.caption = reduc(i) & ext(i) & "amps"
lohm.caption = reduc(r) & extohm(r) & "ohms"
lcperphase.caption = reduc(ip) & ext(ip) & "amps/phase"
Exit Sub
End If
' given watts and amps...
If lwatt.caption > "" And lamp.caption > "" Then
lhp.caption = Str$(p / 746) & "hp"
v = p / i: r = v / i
lohm.caption = reduc(r) & extohm(r) & "ohms"
lvolt.caption = reduc(v) & ext(v) & "volts"
Exit Sub
End If
' given watts and ohms...
If lwatt.caption > "" And lohm.caption > "" Then
v = Sqr(p * r): i = Sqr(p / r): ip = i / Sqr(3)
lvolt.caption = reduc(v) & ext(v) & "volts"
lamp.caption = reduc(i) & ext(i) & "amps"
lcperphase.caption = reduc(ip) & ext(ip) & "amps/phase"
Exit Sub
End If
End Sub
Function reduc (ByVal z As Double) As String
If z >= 1000000# Then
z = z / 1000000#
ElseIf z >= 1000 Then
z = z / 1000
ElseIf z < .001 Then
z = z * 1000000#
ElseIf z < 1 Then
z = z * 1000
End If
reduc = Str$(z)
End Function
Sub showit ()
lvolt.caption = ext(v) & Str$(v)
End Sub
unit = 1
ohmform.icohandle = ohmicon
application.icohandle = ohmicon
ohmform.showmodal