How do I - Check for a crank revolution?

So using on crank do how do I then check how much crank has been used? Is there a way to get a full crank revolution? if I do a manual check variable like the one below, will that tally up to the actual crank movements, are there 360 "clicks" in a revolution on the actual hardware?

on crank do
crankdeg++
if crankdeg==360 then
crankdeg=0
end
end

Otherwise, there any accessible variables in relation to the crank, for example crank.position (how many degrees it currently sits at)?

Cheers!

In every event, its aa and ra members will be set to the current absolute angle and the amount of change since the last frame of the crank in degrees.

In the docs under Playdate PulpScript see:

In every event, its aa and ra members will be set to the current absolute angle and the amount of change since the last frame of the crank in degrees.

That should be enough to do the calculations you want.

The number does go up to 360, and if you want to count revolutions you'll need to store that in your own variable, probably adding the "ra" value every frame.

1 Like

Hey @BoldBigflank Yeah I saw that in the docs but don't fully understand it. It seems to imply that the crank doesn't have a "fixed" position or number of clicks logged on the hardware and that the crank is more a relative thing. Unless i'm misinterpreting it.

aa is the absolute position of the crank. 360/0 is the top, 180 is the bottom as far as I can tell in the web player.

1 Like

Yep, I don't think the crank clicks. It's being measured in degrees, but measured as a floating point number.

1 Like

Yeah cool, that's nice and easy then! Cheers

Edit:

Nevermind, being stupid, update isn't a loop lol.

Try this out. It'll record the total crank angle in a single direction as well as the direction it was turning.

on crank do
  if event.ra < 0 then
    direction = "backwards"
  else
    direction = "forwards"
  end
  if direction != last_direction then
    total_cranked = 0
  end
	total_cranked += event.ra
	last_direction = direction
end

on draw do
	label "{total_cranked}" at 0,0
	label "direction: {direction}" at 0,1
end
4 Likes

Yeah that works a treat for crank logging, cheers!

Thank you for sharing this! I was able to repurpose this for exactly what I needed.

I'm still working things out, but the idea is that your character attacking uses a charge, and you recharge yourself by using the crank. The code you provided help give me the foundation for making the crank increase the charge. My next step is figuring out the attack stuff.


// Crank Logic
on crank do
	if event.ra<0 then
		direction = "backwards"
	else
		direction = "forwards"
	end
	// if the forward crank is detected, add to the charge.
	if direction=="forwards" then
		charge++
	end
	// if the charge is at the maxCharge value, don't go any higher.
	if charge>=maxCharge then
		charge = maxCharge
	end
	if direction!=last_direction then
		total_cranked = 0
	end
	total_cranked += event.ra
	last_direction = direction
end