Lightino Lesson 5: How fast is it spinning?

In previous lessons the code calculated the time for a revolution, so as a final exercise, let's display!

Actually, rotational speed is more often measured in revolutions per minute (RPM), but we can easily calculate that.

We calculated the time for a revolution in microseconds (revTime) and there are 60,000,000 microseconds in a minute, so the speed in RPM will be 60,000,000 / revTime. But we need to convert that to text.

The easiest way to do that is using the built-in sprint() function. This takes 3 (or more) arguments:

  1. Somewhere to put the resulting text
  2. A pattern for the text
  3. A value or values to insert into the text.

For the place to put the text we can replace the definition of textString_Up by:

char textString_Up[10];

We've made it 10 characters long, which should be plenty. It's important not to make it too small otherwise we might be overwriting who knows what!

We can now call sprintf() with something like:

sprint(textString_Up, "%4d RPM", 60000000 / revTime);

The %4d means substitute this for the parameter expressed as a 4-digit decimal number. (That's just one of many types of substitution you can specify.)

Why not change textString_Down to display the total number of revolutions?

Warning: Don't try too hard to see how fast you can get it to spin - it has hard edges and you could easily hurt your fingers! 

If you're stuck try reading the Help Sheet!

Previous article Get your head in a spin with this Lightino Fidget Spinner!

Check out our latest products...

BBC micro:bit Watch - Smart Coding Kit (micro:bit not included) - Micro:bit
24V Power Adapter - DC24V - 5A - Power

24V Power Adapter - DC24V - 5A

£16.99 inc VAT
£14.16 ex VAT
OpenMV Cam H7 Telephoto Lens - Accessories

OpenMV Cam H7 Telephoto Lens

£5.99 inc VAT
£4.99 ex VAT
OpenMV Cam H7 Ultra Wide Angle Lens - Accessories

OpenMV Cam H7 Ultra Wide Angle Lens

£17.99 inc VAT
£14.99 ex VAT
OpenMV Cam H7 IR Lens - Cameras

OpenMV Cam H7 IR Lens

£5.99 inc VAT
£4.99 ex VAT
OpenMV Cam H7 Case - Accessories

OpenMV Cam H7 Case

£7.99 inc VAT
£6.66 ex VAT
Lightning Detector - AS3935 (Qwiic) - Sensor

Lightning Detector - AS3935 (Qwiic)

£20.99 inc VAT
£17.49 ex VAT
Qwiic Keypad - 12 Button - Qwiic

Qwiic Keypad - 12 Button

£8.99 inc VAT
£7.49 ex VAT