Using Accelerometer outputs to change a field colour

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Using Accelerometer outputs to change a field colour

Post by glenn9 » Fri Feb 26, 2021 9:23 am

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

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using Accelerometer outputs to change a field colour

Post by Klaus » Fri Feb 26, 2021 9:46 am

Hi Glenn,

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


Best

Klaus

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Using Accelerometer outputs to change a field colour

Post by glenn9 » Fri Feb 26, 2021 11:18 am

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

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Using Accelerometer outputs to change a field colour

Post by glenn9 » Fri Feb 26, 2021 11:21 am

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

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Using Accelerometer outputs to change a field colour

Post by Xero » Fri Feb 26, 2021 11:55 am

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

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: Using Accelerometer outputs to change a field colour

Post by elanorb » Fri Feb 26, 2021 12:11 pm

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
Elanor Buchanan
Software Developer
LiveCode

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Using Accelerometer outputs to change a field colour

Post by glenn9 » Fri Feb 26, 2021 12:36 pm

Thanks Elanor,

that helped a lot and I now understand!

All works perfectly now!

Kind regards,

Glenn

Post Reply