Dear Support
How can I create and programming an sine wave, square wave and triangle wave.
I try to create an sine wave but I don't see it on the screen an sine wave
CONST PI = 3.1415926535897932384626433832795
IF CREATESCREEN( 800, 600, 32) < 0
MESSAGEBOX 0,"Error creating screen","error"
END
ENDIF
DO
FILLSCREEN RGB(255,0,0)
'WRITETEXT 0, 0, "Press any key to close"
'FLIP
DEF u AS DOUBLE
DEF i AS WORD
u = 2 * sin((2 * PI * 500 * 5*10^-3) + PI/4)
FOR i = 1 TO 180
DRAWLINE 0, 0, u, u, RGB(255,0,255)
NEXT i
UNTIL GETKEY <> ""
CLOSESCREEN
END
Kind regards
Stephane
You need to calculate new sine for every X:$define KILL_VERTICAL_LINE
CREATESCREEN( 800, 600, 32)
const MiddleY = (600/2)-1
const MaxX = 800-1
double dStartAngle, dAngle
int x, y, prevX, prevY
' animate the sine
dStartAngle = 0.0
DO
$ifndef KILL_VERTICAL_LINE
prevY = 0
$endif
dAngle = dStartAngle
FILLSCREEN RGB(255,0,0)
'=====================================
' draw sine
FOR x = 0 TO MaxX
y = (sind(dAngle) * MiddleY)
$ifdef KILL_VERTICAL_LINE
if (x=0) then prevY = y
$endif
prevX = x-1
if (prevX<0) then prevX = 0
DRAWLINE prevX, prevY +MiddleY, x, MiddleY +y, RGB(255,0,255)
' for next loop
prevY = y
dAngle += 1.0
NEXT x
DRAWLINE 0, MiddleY, MaxX, MiddleY, RGB(0,0,200)
FLIP
' animation speed
dStartAngle += 0.5
UNTIL GETKEY <> ""
CLOSESCREEN
END
Sapero
Thanks
I try to program an triangle en square wave but I don't know how I can do this:
In the mathematics is for an lineair function (line)
y = ax + b
dy/dx = -b/a
How can I program an triangle en square wave?
Stephane
Can you give an example
Hi...
Try this formula:
e=e/1.41
FOR i = 40 to 400 step 1
'try this diferencial wave
'y = int(140+e*tan(i)+PI/2 )
'triangle wave
y=int(140 +e*Tan(Sin(i*(2*PI) / s))-1.41)
LINE w2,i,y,rgb(160,0,0)
Hello
Can you please show this in an application for show square wave and triangle signals?
Thanks
I try this:
$define KILL_VERTICAL_LINE
CREATESCREEN( 800, 600, 32)
const MiddleY = (600/2)-1
const MaxX = 800-1
double dStartAngle, dAngle
int x, y, prevX, prevY
' animate the sine
dStartAngle = 0.0
DO
$ifndef KILL_VERTICAL_LINE
prevY = 0
$endif
dAngle = dStartAngle
FILLSCREEN RGB(255,0,0)
'=====================================
' draw sine
FOR x = 0 TO MaxX
y = (sind(dAngle) * MiddleY)
$ifdef KILL_VERTICAL_LINE
if (x=0) then prevY = y
$endif
prevX = x-1
if (prevX<0) then prevX = 0
DRAWLINE prevX, prevY +MiddleY, x, MiddleY +y, RGB(255,0,255)
' for next loop
prevY = y
dAngle += 1.0
NEXT x
DEF e AS DOUBLE
DEF i AS INT
e = e / 1.41
FOR i = 40 to 400 step 1
'try this diferencial wave
'y = int(140+e*tan(i)+PI/2 )
'triangle wave
y = INT(140 + e * TAN(SIN(i * (2 * PI) / s)) - 1.41)
LINE w2,i,y,rgb(160,0,0)
NEXT i
DRAWLINE 0, MiddleY, MaxX, MiddleY, RGB(0,0,200)
FLIP
' animation speed
dStartAngle += 0.5
UNTIL GETKEY <> ""
CLOSESCREEN
END
Hi Techno ....
Program is here on Creative board called Osciloscope Sim. :)
aurelCB
Can you please give me the function how I can program an triangle wave and square wave and impuls signal?
Kind regards
Stephane
Sapero
Can you please help me for written this function triangle wave, square wave for my electronics project
Greets
Stephane
Stephane, this is much easier than interfacing i2c expander!int scrX,scrY
getscreensize scrX,scrY
if (CREATESCREEN( scrX,scrY, 32))
if (CREATESCREEN( scrX,scrY, 24)) then end
endif
int y ' triangle A,B
int c_y ' triangle C
int c_dir
int d_y ' rectangle D
DO
FILLSCREEN RGB(255,0,0)
c_y = 100
c_dir = 1
FOR x = 0 TO scrX-1
top = 0
' /| /| /| triangle A
' / |/ |/ |
y = x%100
WRITEPIXEL x, top+100-y, 0xFFFFFF
if (y=0) then DRAWLINE x, top, x, 100 +top, 0xFFFFFF
top += 130
' |\ |\ |\ triangle B
' | \| \| \
y = x%100 /*dupe!*/
WRITEPIXEL x, top +y, 0xFFFFFF
if (y=99) then DRAWLINE x, top, x, 100 +top, 0xFFFFFF
top += 130
' /\ /\ triangle C
' / \/ \
WRITEPIXEL x, top +c_y, 0xFFFFFF
c_y += c_dir
if (c_y > 99) : c_y = 99 : c_dir *= -1 : endif
if (c_y < 0) : c_y = 0 : c_dir *= -1 : endif
top += 130
' __ _ rectangle D
' __| |__|
if ((x%100)=99)
DRAWLINE x, top, x, 100 +top, 0xFFFFFF
d_y = d_y || 1
endif
WRITEPIXEL x, top+(99*d_y), 0xFFFFFF
top += 130
' sine
WRITEPIXEL x, top+((sind(x*2)+1)*45), 0xFFFFFF
NEXT x
FLIP
UNTIL GETKEY <> ""
CLOSESCREEN
END
I'm little bit confusefd with this sintax.
Is top += 130 same as top = top + 130 or something else?
And how translate this : d_y = d_y || 1 to CB code?
Uf this one is tuff:
if (c_y > 99) : c_y = 99 : c_dir *= -1 : endif
IF (c_y > 99)
c_y = 99
c_dir = c_dir* -1
ENDIF
is this ok?
I'm confused becose i never before use this kind of mathematical expressions.
I'll try
regards
zlatko
Sapero
Thanks for your part solutions
I try this program in functions
Creative can handle the || operator (xor), no changes needed. The first bit is toggled so the variable d_y is one time 0, another time 1.
c_dir *= -1 and the other you can expand as you did, but now I see that c_dir=-c_dir will be faster than c_dir=c_dir*-1.
A little more optimized: top += 130 /* top = top + 130 */
' /\ /\ triangle C
' / \/ \
WRITEPIXEL x, top +c_y, 0xFFFFFF
c_y += c_dir
if (c_y > 99) : c_y = 99 : c_dir = -c_dir : endif
if (c_y < 0) : c_y = 0 : c_dir = -c_dir : endif
top += 130
' __ _ rectangle D
' __| |__|
if ((x%100)=99)
DRAWLINE x, top, x, 100 +top, 0xFFFFFF
d_y = d_y || 99
endif
WRITEPIXEL x, top+d_y, 0xFFFFFF
Next optimization for triangle C: top += 130
' /\ /\ triangle C
' / \/ \
' WRITEPIXEL x, top+50+(y* (((x/100) & 1)*2)-1), 0x00FF00 bug?
y = (x%100)-50
if ((x/100) & 1) then y = -y
WRITEPIXEL x, top+50+y, 0xFF00FF
Zlatko, run this example to see what is ||def k, n as int
k = 0
openconsole
for n=0 to 10
print k
k = k || 1
next n
Yes i'm try...
result is :
0
1
0
1
0
1
0
1
0
1
0
Yes i see that is XOR.
I forget this operation ::)
Hello
I try create an function for generate an impulssignal
'===================================================================
top += 130
IF ((x % 100) = 99)
DRAWLINE x, top, x, 100 + top, 0xFFFFFF
d_y = d_y || 99
ENDIF
WRITEPIXEL x, top + d_y, 0xFFFFFF
'===================================================================
Can someone help me how I can do show an impuls signal?
Stephane
You have already a negative impuls.
top += 130
' impuls
WRITEPIXEL x, top+100, 0xFFFFFF
if ((x%100)=0) then DRAWLINE x, top, x, 100 +top, 0xFFFFFF
x%100 will be zero for x={0,100,200,300 ...}
Attached a manchester encoder simulator, shows the clock, data, and encoded data bits. It is initialized with "Stephane" string.