Page 1 of 1

Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 9:23 am
by glenn9
Hi,

Over the last few days I've started to explore the Accelerometer on mobile with some initial success.

It then came into my head whether I could use the outputs of the Accelerometer to change, I guess somewhat randomly, the background colour of a field as the phone moved so put together this code (which of course didn't work and I can't understand why?!)

Code: Select all

on accelerationChanged pX, pY, pZ
   
   -- just to display the values in a field for my interest (I rounded the values to try and calm the number changes visually) - this all works
   put round(pX) into field"px"
   put round(pY) into field"py"
   put round(pZ) into field"pz"
 
 -- I was hoping that this would populate variables...
   put pX*25.5 into tR
   put pY*25.5 into tG
   put pZ*25.5 into tB
   
   -- ...that would then give RGB values that the field"col" could then respond to - which doesn't work!
   set the backcolor of field"col" to tR, tG, tB

end accelerationChanged
I'm not understanding why it doesn't work!

Regards,

Glenn

Re: Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 9:46 am
by Klaus
Hi Glenn,

did you -> mobileEnableAccelerometer XX at one point (openstack/opencard)?


Best

Klaus

Re: Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 11:18 am
by glenn9
Hi Klaus,

Yep, all enabled, this my full card code:

Code: Select all

on opencard
   if the environment is "mobile" then
      mobileEnableAccelerometer 
   end if
end opencard

on closecard
   mobileDisableAccelerometer
end closecard

Re: Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 11:21 am
by glenn9
I also get all the accelerometer numbers in the fields px, py, pz and so I was wondering why the backcolor wasn't responding to the RGB numbers represented by the variables tR, tG, tB

Thanks,

Glenn

Re: Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 11:55 am
by Xero
Did you set an interval with your mobileEnableAccelerometer command?
mobileEnableAccelerometer 100 for example?
The dictionary seems to state:
"The mobileEnableAccelerometer command will cause accelerationChanged events to be delivered to the current card of the defaultStack at the specified interval."
Maybe without an interval specified, there is no delivery of the event...
I tried your code with random(255) rather than accelerometer input, and it worked, so that part of the code is solid.
XdM

Re: Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 12:11 pm
by elanorb
Hi Glenn

The R, G and B values need to be between 0 and 255 to be valid. Because the accelerometer values can be negative, and multiplying by 25.5 can create fractional numbers you will need to use the round and abs functions to make sure the values you use are valid for an RGB colour.

Code: Select all

put abs(round(pX*25.5)) into tR
   put abs(round(pY*25.5)) into tG
   put abs(round(pZ*25.5)) into tB
I hope that helps.

Elanor

Re: Using Accelerometer outputs to change a field colour

Posted: Fri Feb 26, 2021 12:36 pm
by glenn9
Thanks Elanor,

that helped a lot and I now understand!

All works perfectly now!

Kind regards,

Glenn