-
Sin
Does anyone have any example code or have a good explanation of how to use the SIN operator? In the manual it says that SIN starts with a value in binary radians as opposed to normal degrees. I have done a few debug tests and have got quite weird results.
e.g
SIN 0 =0
SIN 255=65533
SIN 127=3
It says that the output is from -127 to 127 in two's compliment. Does that mean that -127 would be represented as 65408?
Regards,
Andy Peaple
-
Sin and Cos
I realise the question was asked several years ago - I came across it on a search - the problem with SIN() and COS() in PICBASIC is that they return a twos complement number. Also they deal with "binary radians" (not the same as trig radians) where 0 is 0 degrees/radians and 255 is 360 degrees or 2PI radians.
Here is my solution to displaying a barchart of the sine wave function 128+sin(i)
(using http://www.picbasic.co.uk/forum/showthread.php?t=2359 to display):
Value var word 'must use word variable for BARGRAPH
twoC var byte 'must use byte variable
include "LCDbar_INC.bas"
'BARgraph Value, Row, Col, Width, Range, Style
'........... I use in a loop and increment i etc............
twoC = sin(i)
if (twoc & %10000000)=0 then PosS 'else negative 2's complement
' value = 127-((twoc ^ %11111111) +1) 'restore negative value
value = 126 - (twoc ^ %11111111) 'I wanted a 128 offset
goto ShowS
PosS: value = 128 + twoc
ShowS:
@ BARgraph _Value, 1, 0, 16, 256, lines
'............ Fiddle/pause and go round the loop..........
Peter Finch
-
here is a link to a website i found a while ago, that helps with a lot of the math. the site was written for a basic stamp, but it all works out the same.
http://www.emesystems.com/BS2math3.htm#arcCos
and here has a bunch of other math functions for the stamp along with explaining some of the other functions that can be done with it..
http://www.emesystems.com/BS2index.htm
-
SIN example code
Hello,
The code below was used in an incubator. The user could set the mean temperature and the range. The day was divided into 96 fifteen minute segments and at 6 AM and 6 PM the temperature was set to the user commanded mean temperature. the "range" is a sinusoidal function that adds or subtracts from the mean. For example a mean of 24 with a range of 10 would cause the incubator to be at 24 degrees at 6 AM and 6 PM. At noon the temperature would be 24 + 10 = 34 and at midnight the temperature would be 24 - 10 = 14 Celsius.
CalculateSetPoint:
read 0, meantemp
read 1, range
' SetPoint = 10* (meantemp + range * sine (timeslot * 255/96)) with allowances
' for the sign and integer truncation plus an 18 hour time shift.
w = (255 * timeslot/96) ' 0 to 255 in 2.667 chunks
b = sin w ' 0 to 127 in 2's complement
if b.7 = 1 then negative
positive:
x = 10*meantemp + (20*b*range/255)
goto timeslotdone
negative:
b = (b ^ %11111111) + 1 ' 2's complement to unsigned integer
x = 10*meantemp - (20*b*range/255)
TimeSlotDone:
setpoint = x
HTH
BrianT