Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer and led — Parallax Forums

Accelerometer and led

edited 2008-03-28 18:58 in Learn with BlocklyProp
I'm using the code from the online books on the accelerometer and i decided to try to make a led light up when the x-tilt reached 10 degrees.
Of course it is not working and i would like some help please.
Heres the code.
' {$STAMP BS2} ' BASIC Stamp Directive
' {$PBASIC 2.5} ' PBASIC Directive
' -----[noparse][[/noparse] Constants ]----------------------------------------------------------
Negative CON 1 ' Sign - .bit15 of Word variables
Positive CON 0
' -----[noparse][[/noparse] Variables ]----------------------------------------------------------
x VAR Word ' Memsic x-axis measurement
y VAR Word ' Memsic y-axis measurement
side VAR Word ' trig subroutine variable
angle VAR Word ' result angle - degrees
sign VAR Bit ' Sign bit
' -----[noparse][[/noparse] Initialization ]-----------------------------------------------------
DEBUG CLS ' Clear Debug Terminal
' -----[noparse][[/noparse] Main Routine ]-------------------------------------------------------
DO
PULSIN 6, 1, x ' x-axis measurement
PULSIN 7, 1, y ' y-axis measurement
' Scale and offset x and y-axis values to -127 to 127.
x = (x MIN 1875 MAX 3125) - 1875 ** 13369 - 127
y = (y MIN 1875 MAX 3125) - 1875 ** 13369 - 127
[b][u]IF x << 010 THEN HIGH 5  'Here is my little addon <<<<<<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[/b][/u]
' Calculate and display Arcsine of x-axis measurement.
side = x
GOSUB Arcsine
DEBUG HOME, "x tilt angle = ", CLREOL, SDEC3 angle, CR
' Calculate and display Arcsine of y-axis measurement.
side = y
GOSUB Arcsine
DEBUG "y tilt angle = ", CLREOL, SDEC3 angle
PAUSE 50 ' Pause 1/20 second
LOOP ' Repeat DO...LOOP
' -----[noparse][[/noparse] Subroutine - Arcsine ]-----------------------------------------------
' This subroutine calculates arcsine based on the y coordinate on a circle
' of radius 127. Set the side variable equal to your y coordinate before
' calling this subroutine.
Arcsine: ' Inverse sine subroutine
GOSUB Arccosine ' Get inverse cosine
angle = 90 - angle ' sin(angle) = cos(90 - angle)
RETURN
' -----[noparse][[/noparse] Subroutine - Arccosine ]---------------------------------------------
' This subroutine calculates arccosine based on the x coordinate on a circle
' of radius 127. Set the side variable equal to your x coordinate before
' calling this subroutine.
Arccosine: ' Inverse cosine subroutine
sign = side.BIT15 ' Save sign of side
side = ABS(side) ' Evaluate positive side
angle = 63 - (side / 2) ' Initial angle approximation
DO ' Successive approximation loop
IF (COS angle <= side) THEN EXIT ' Done when COS angle <= side
angle = angle + 1 ' Keep increasing angle
LOOP
angle = angle */ 361 ' Convert brads to degrees
IF sign = Negative THEN angle = 180 - angle' Adjust if sign is negative.
RETURN



it just lights up as soon as i start the program

Post Edited (Ctrl.Alt.De1337ed) : 3/28/2008 4:37:06 AM GMT

Comments

  • BeanBean Posts: 8,129
    edited 2008-03-28 18:24
    If think you only want one "<" symbol. As in "IF x < 10 THEN HIGH 5"

    "<<" means shift left.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • edited 2008-03-28 18:58
    Thanks it works now! However there is an interesting bug, it lights the led at 4 degrees rather than 10, any suggestions? Also i added an else low 5 statement at the end so now it looks like:
    IF x < 010 THEN HIGH 5 ELSE LOW 5
    



    I think i found it, the angle you want has to be twice that in real life, so, if i wanted 10 degrees, then led light up, I would have to put in a 20 in the code... (don't know why)

    new code:
    IF x < 20 THEN HIGH 5 ELSE LOW 5
    

    Post Edited (Ctrl.Alt.De1337ed) : 3/28/2008 7:37:47 PM GMT
Sign In or Register to comment.