October 29, 2025, 01:09:08 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Strange Logical error "Secundary Coils = 0"

Started by Techno, June 11, 2015, 08:49:04 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Hi all,

When I calculate the parameters Primary Coils, Secundaire Coils, Primary Current and Secundaire Current. Only the value "Secundary Coils is always 0 when the user enter:

Power = 150
Primary Voltage = 220
Secundary Voltage = 12

The output has:
Primary Coil : 477
Secundary Coil : 0  ---> Strange !!!!
Primary Current : 0.6818
Secundary Current : 12.5000

This is my program



'calculate main transformer by Zlatko Vidlanovic/2008
'program written in Creative Basic
DEF w1 : WINDOW
DEF U1 : INT
DEF U2 : INT
DEF N1 : INT
DEF N2 : INT
DEF run : INT
DEF check : INT
DEF FX : FLOAT
DEF P : FLOAT
DEF SFe : FLOAT
DEF f : FLOAT
DEF I1 : FLOAT
DEF I2 : FLOAT
DEF N1$ : STRING
DEF N2$ : STRING
DEF I1$ : STRING
DEF I2$ : STRING

CONST IDC_TXT_POWER = 1
CONST IDC_TXT_UPRIM = 2
CONST IDC_TXT_USEC  = 3
CONST IDC_TXT_PRIM_COIL = 4
CONST IDC_TXT_SEC_COIL  = 5
CONST IDC_TXT_IPRIM = 6
CONST IDC_TXT_ISEC = 7
CONST IDC_BTN_NEW = 8
CONST IDC_BTN_OPEN = 9
CONST IDC_BTN_SAVE = 10
CONST IDC_BTN_ABOUT = 11
CONST IDC_BTN_CALC = 20
CONST B =1: 'B=1:' magnetic induction(T)

'open window ----------------------------------------------
OPENWINDOW(w1,0,0,500,400,@MINBOX,0, "Calculate Traformer v1.0",&mainHandler)
SETWINDOWCOLOR(w1,rgb(210,210,216))
ENABLETABS(w1,1)                                                                                         
SETPRECISION(4) :'set decimal places for floating variables                                     

'display --------------------------------------------------------
DRAWMODE(w1,@TRANSPARENT)

'frame
RECT(w1,20,20,200,220, RGB(0,0,0),RGB(200,200,206))
RECT(w1,22,22,196,216, RGB(239,239,247))
LINE(w1,22,237,217,237,RGB(140,140,166))
LINE(w1,217,237,217,22,RGB(140,140,156))

RECT(w1,30,30,180,200,RGB(0,0,0),RGB(239,239,247))
PSET(w1,20,20,RGB(210,210,216))
PSET(w1,219,20,RGB(210,210,216))
PSET(w1,20,239,RGB(210,210,216))
PSET(w1,219,239,RGB(210,210,216))

SETFONT(w1,"Small Fonts",IDC_TXT_ISEC, 400,0)
MOVE(w1,140,216)
PRINT w1,"---EXAMPLE---"
SETFONT(w1,"Courier New",IDC_BTN_SAVE, 400,0)

'EXAMPLE ---------------------------------------------------------
U2 = 12 :'secondary voltage(V)
move(w1,40,140)
print w1,"U2 = ",U2," V"

f  = 50:' frequency(50 or 60 Hz)
P  = 10:'power(W)-Watt
U1 = 220:'primary voltage(110 or 220V)
move(w1,40,120)
print w1,"U1 = ",U1," V"

'SFe=iron core surface in cm2
'1 inch = 2.45 cm
SFe = 1.2 * (sqrt(P))
move(w1,40,40)
print w1,"Sf =",SFe," cm2"
FX = B * SFe
move(w1,40,60)
print w1, "FX =", FX," Wb"


'FXFX10 ^ -4 : 'measure must be in meters
N1 = U1 / (4.44 * fx * f):' primary coil
move(w1,40,80)
print w1,"N1 = ",N1," turns"

N2 = N1 * (U2 / U1):'secondary coil
move(w1,40,100)
print w1,"N2 = ",N2," turns"

I1 = P / U1
move(w1,40,160)
print w1,"I1 =",I1," A"

I2 = P / U2
move(w1,40,180)
print w1,"I2 =",I2," A"

'Power in Watts
move(w1,40,200)
print w1,"Pw = ",P," W"

'controls panel---------------------------------------------------
'frame -----------------------------------------------------------
RECT(w1,240,64,245,260,RGB(0,0,0),RGB(110,110,136))
PSET(w1,240,64,RGB(210,210,216))
PSET(w1,484,64,RGB(210,210,216))
PSET(w1,240,323,RGB(210,210,216))
PSET(w1,484,323,rgb(210,210,216))

'-----------------------------------------------------------------
'edit controls----------------------------------------------------
CONTROL(w1,@EDIT,"power",390,80,70,22,@TABSTOP|@cteditnumber,IDC_TXT_POWER)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_TXT_POWER)
SETCONTROLCOLOR(w1,IDC_TXT_POWER,RGB(200,220,240),RGB(50,50,50))

CONTROL(w1,@EDIT,"UPrim",390,106,70,22,@TABSTOP|@cteditnumber,IDC_TXT_UPRIM)
SETFONT(w1,"Microsoft Sans Serif",8,400,0,IDC_TXT_UPRIM)
SETCONTROLCOLOR(w1,IDC_TXT_UPRIM,RGB(200,220,240),RGB(50,50,50))

CONTROL(w1,@EDIT,"USec",390,132,70,22,@TABSTOP|@cteditnumber,IDC_TXT_USEC)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_TXT_USEC)
SETCONTROLCOLOR(w1,IDC_TXT_USEC,RGB(200,220,240),RGB(50,50,50))
'-----------------------------------------------------------------
RECT(w1,250,164,220,120,RGB(245,245,255))
PSET(w1,250,164,RGB(110,110,126))
PSET(w1,469,164,RGB(110,110,126))
'-----------------------------------------------------------------
CONTROL(w1,@EDIT,"N1 Pim Coil",390,172,70,22,@CTEDITRO,IDC_TXT_PRIM_COIL)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_TXT_PRIM_COIL)
SETCONTROLCOLOR(w1,IDC_TXT_PRIM_COIL,RGB(200,220,240),RGB(50,50,50))

CONTROL(w1,@EDIT,"N2 Sec Coil",390,200,70,22,@CTEDITRO,IDC_TXT_SEC_COIL)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_TXT_SEC_COIL)
SETCONTROLCOLOR(w1,IDC_TXT_SEC_COIL,RGB(200,220,240),RGB(50,50,50))

CONTROL(w1,@EDIT,"IPrim",390,228,70,22,@CTEDITRO,IDC_TXT_IPRIM)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_TXT_IPRIM)
SETCONTROLCOLOR(w1,IDC_TXT_IPRIM,RGB(200,220,240),RGB(50,50,50))

CONTROL(w1,@EDIT,"ISec",390,256,70,22,@CTEDITRO,IDC_TXT_ISEC)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_TXT_ISEC)
SETCONTROLCOLOR(w1,IDC_TXT_ISEC,RGB(200,220,240),RGB(50,50,50))

'button panel ----------------------------------------------------
'frame -----------------------------------------------------------
RECT(w1,240,20,244,34,RGB(50,50,70),RGB(148,140,160))
PSET(w1,240,20,RGB(210,210,216))
PSET(w1,483,20,rgb(210,210,216))

'buttons ---------------------------------------------------------
CONTROL(w1,@BUTTON,"New",256,28,50,20,@border|0x50000000,IDC_BTN_NEW)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_BTN_NEW)
SETCONTROLCOLOR(w1,IDC_BTN_NEW,RGB(200,220,240),RGB(105,105,131))

CONTROL(w1,@BUTTON,"Open",310,28,50,20,@border|0x50000000,IDC_BTN_OPEN)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_BTN_OPEN)
SETCONTROLCOLOR(w1,IDC_BTN_OPEN,RGB(200,220,240),RGB(110,110,136))

CONTROL(w1,@BUTTON,"Save",364,28,50,20,@border|0x50000000,IDC_BTN_SAVE)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_BTN_SAVE)
SETCONTROLCOLOR(w1,IDC_BTN_SAVE,RGB(200,220,240),RGB(110,110,136))

CONTROL(w1,@BUTTON,"About",418,28,50,20,@border|0x50000000,IDC_BTN_ABOUT)
SETFONT(w1,"Microsoft Sans Serif",8, 400,0,IDC_BTN_ABOUT)
SETCONTROLCOLOR(w1,IDC_BTN_ABOUT,RGB(200,220,240),RGB(110,110,136))

'button CALCULATE -----------------------------------------------
CONTROL(w1,@BUTTON,"Calc",320,290,90,26,@border|0x50000000,IDC_BTN_CALC)
SETFONT(w1,"Tahoma",12,200,0,IDC_BTN_CALC)
SETCONTROLCOLOR(w1,IDC_BTN_CALC,RGB(225,255,255),RGB(110,110,136))
'----------------------------------------------------------------

'right panel ----------------------------------------------------
Frontpen(w1,RGB(245,245,255))
SETFONT(w1,"MS Sans Serif",9, 400,0)
MOVE(w1,260,82)
PRINT w1,"Transformer Power ( W )"

MOVE(w1,260,110)
PRINT w1,"Primary Voltage ( V )"

MOVE(w1,260,136)
PRINT w1,"Secondary Voltage ( V )"

MOVE(w1,260,176)
PRINT w1,"Primary Coil ( turns )"

MOVE(w1,260,204)
PRINT w1,"Secondary Coil ( turns )"

MOVE(w1,260,230)
PRINT w1,"Primary Current ( A )"

MOVE(w1,260,258)
PRINT w1,"Secondary Current ( A )"

SETFONT(w1,"Small Fonts",7, 400,0)
MOVE(w1,390,67)
PRINT w1,"> USE TAB KEY <"

'cables ------------------------------------------------------------------------
RECT(w1,440,53,10,12,RGB(0,0,0))
RECT(w1,445,54,4,10,RGB(50,50,50),RGB(70,70,70))
RECT(w1,220,180,21,20,RGB(0,0,0))
RECT(w1,220,190,21,10,rgb(0,0,0),RGB(70,70,70))
'-------------------------------------------------------------------------------

run = 1
Waituntil(run = 0)
Closewindow(w1)
End

'---------------------------
SUB mainHandler()
SELECT @CLASS
Case @IDCLOSEWINDOW
run = 0
'---------------------------
Case @IDCREATE
Centerwindow(w1)
'-----------------------
Case @IDCONTROL
SELECT @CONTROLID
Case IDC_BTN_NEW
ClearDisplay()
ClearControls()
ClearValues()
setfocus(w1)

Case IDC_BTN_CALC
ClearDisplay()
Check = 0
ClearValues()
ClearControls2()
GetValues()
CalcTrafo()
SetOut()
SetDisplay()
setfocus(w1)

case IDC_BTN_ABOUT
aboutT()
ENDSELECT
'-----------------------
ENDSELECT
RETURN 0
END sub

'-----------------------------------------
SUB ClearDisplay()
RECT(w1,30,30,180,200,rgb(0,0,0),rgb(239,239,247))
RETURN 0
end sub

SUB ClearControls()
For c = IDC_TXT_POWER to IDC_TXT_ISEC
SETCONTROLTEXT(w1,c,"")
Next c
RETURN 0
end sub

SUB ClearControls2()
For cn = IDC_TXT_PRIM_COIL to IDC_TXT_ISEC
SETCONTROLTEXT(w1,cn,"")
Next cn
RETURN 0
END sub

SUB ClearValues()
U1 = 0
U2 = 0
N1 = 0
N2 = 0
fx = 0
P = 0
SFe= 0
I1 = 0
I2 = 0
RETURN 0
END sub

SUB GetValues()
'Ophalen van P
P = val(GETCONTROLTEXT(w1, IDC_TXT_POWER))
IF(P = 0) then
messagebox(w1,"Power not entered !","Error")
return
Endif

'Ophalen van U1
U1 = val(GETCONTROLTEXT(w1, IDC_TXT_UPRIM))
IF(U1 = 0) then
messagebox(w1,"Primary voltage not entered !","Error")
return
Endif

'Ophalen van U2
U2 = val(GETCONTROLTEXT(w1,IDC_TXT_USEC))
IF(U2 = 0) then
messagebox(w1,"Secondary voltage not entered !","Error")
return
Endif

IF(P > 0) &(U1 > 0) &(U2 > 0) then check = 1
RETURN
END sub


SUB calctrafo()
IF(check = 0) THEN
Return
Endif

f = 50
SFe = 1.2 * sqrt(P)
fx = B  * SFe
fx = fx * (10^-4)
N1 = U1 / (6.28 * fx * f)
N2 = N1 * (U2 / U1)
I1 = P / U1
I2 = P / U2
RETURN
END sub

SUB setout()
IF(check = 0) then
Return
Endif

SETCONTROLTEXT(w1,IDC_TXT_PRIM_COIL,str$(N1))
SETCONTROLTEXT(w1,IDC_TXT_SEC_COIL,str$(N2))
SETCONTROLTEXT(w1,IDC_TXT_IPRIM,str$(I1))
SETCONTROLTEXT(w1,IDC_TXT_ISEC,str$(I2))
RETURN
END sub

SUB SetDisplay()
IF(check = 0) then
ClearDisplay()
Return
Endif

Drawmode(w1,@transparent)
Frontpen(w1,rgb(0,0,150))
SETFONT(w1,"Courier New",10, 400,0)

move(w1,40,140)
print w1,"U2 = ",U2," V"

move(w1,40,120)
print w1,"U1 = ",U1," V"

move(w1,40,40)
print w1,"Sf =",SFe," cm2"

move(w1,40,60)
print w1,"fx =",fx," Wb"

move(w1,40,80)
print w1,"N1 = ",N1," turns"

move(w1,40,100)
print w1,"N2 = ",N2," turns"

move(w1,40,160)
print w1,"I1 =",I1," A"

move(w1,40,180)
print w1,"I2 =",I2," A"

move(w1,40,200)
print w1,"Pw = ",P," W"
RETURN
end sub

'---------------------------------------------
SUB aboutT()
about$="Transformer Calc     "+Chr$(10)
about1$="Written in Creative Basic   "+chr$(10)
about2$="Aurel 2008   "+chr$(10)

MessageBox(w1,about$+chr$(10)+about1$+about2$,"About Transformer Calc",0|64)
RETURN
end sub


billhsln

Not strange.

N1 = U1 / (6.28 * fx * f) = 220 / 734.82 = 0.299 = 0 as an INT.

Using all FLOAT will solve most of this problem.

Bill
When all else fails, get a bigger hammer.

Egil

Quote from: Techno on June 11, 2015, 08:49:04 AM
Hi all,

When I calculate the parameters Primary Coils, Secundaire Coils, Primary Current and Secundaire Current. Only the value "Secundary Coils is always 0 when the user enter:

Power = 150
Primary Voltage = 220
Secundary Voltage = 12

The output has:
Primary Coil : 477
Secundary Coil : 0  ---> Strange !!!!
Primary Current : 0.6818
Secundary Current : 12.5000

This is my program


You use integers for the most important parameters, and experience rounding problems.
Use these definitions instead:


DEF U1    : FLOAT
DEF U2   : FLOAT
DEF N1   : FLOAT
DEF N2   : FLOAT

Support Amateur Radio  -  Have a ham  for dinner!