EnableAccelerometer ( [enabled][,hertz] )

Parameters

enabled (optional) - 1 starts sampling (default); 0 stops it.

hertz (optional) - requested samples per second, clamped to 1 through 120; 60 (default).

Description

Starts or stops accelerometer sampling and returns 1 when accepted, or 0 on failure/unavailability. Sampling starts asynchronously: check status and sample age. Disable always succeeds. Restarting discards previous samples. Focus loss and debugger suspension stop delivery; foreground resume restarts a still-requested sensor. The actual sample rate is device dependent.

iOS and Android provide native touch and acceleration, and web pages on phones and tablets provide touch. Other backends return neutral data. Read the mobile input guide for lifecycle and snapshot rules.

See also: UpdateMobileInput, GetTouchEvent, AccelerometerStatus.

Example

; EnableAccelerometer Example
; ---------------------------

Graphics 640,480,0,2
SetBuffer BackBuffer()
value$="No event yet"
running=True
count=0
mode=1
accepted=EnableAccelerometer()
While running
    UpdateMobileInput
    id=TouchID(1)
    phase=GetTouchEvent()
    toggle=KeyHit(57)
    While phase
        If phase=1
            count=count+1
            If TouchEventX()>530 And TouchEventY()<60 Then running=False
            If TouchEventY()>400 Then toggle=True
        EndIf
        phase=GetTouchEvent()
    Wend
    If KeyHit(1) Then running=False
    If MouseHit(1)
        If MouseX()>530 And MouseY()<60 Then running=False
        If MouseY()>400 Then toggle=True
    EndIf
    If toggle
        mode=1-mode
        accepted=EnableAccelerometer(mode)
    EndIf
    value$="Accepted: "+accepted+"  Status: "+AccelerometerStatus()
    Cls
    Color 70,100,150
    Rect 535,10,95,45
    Rect 10,405,620,60
    Color 255,255,255
    Text 553,23,"Exit"
    Text 20,80,"EnableAccelerometer: "+value$
    Text 20,115,"Touch/drag the screen. Escape exits on desktop."
    Text 20,145,"Sensor status: "+AccelerometerStatus()+"  Age: "+AccelerometerAge()
    Oval 305+AccelerometerX()*100,245+AccelerometerY()*100,30,30,False
    Text 20,425,"Tap here or press Space to toggle"
    Flip
Wend
accepted=EnableAccelerometer(0)
End

Index