Thursday, July 21, 2022

Giving the Playground Express a Spin

 The latest addition to our single chip computer zoo is Adafruit's Circuit Playground Express. It is sold for about 30$ and comes with a lot of GIO pins, 10 RGB LEDs, a small speaker, lots of sensors (including acceleration, temperature, IR,...) and 1.5MB of flash rom. The excuse for buying it is that I might interest the kids in it (being better equipped on board than an Arduino while being less complex than a RaspberryPi.


As the ten LEDs are arranged around the circular shape, I thought a natural idea for a first project using the accelerometer would be to simulate a ball going around the circumference.



The video does not really capture the visual impression due to overexposure of the lit LEDs.

The Circuit Playground Express comes with a graphical programming language (like Scratch) and an embedded version of Python. But you can also directly program it with the Arduino IDE to code in C which I used since this is what I am familiar with.

Here is the source code (as always with GPL 2.0)
// A first project simulating a ball rolling around the Playground Express

#include <Adafruit_CircuitPlayground.h>

uint8_t pixeln = 0;
float phi = 0.0;
float phid = 0.10;

void setup() {
  CircuitPlayground.begin();
  CircuitPlayground.speaker.enable(1);
}

int phi2pix(float alpha) {
   alpha *= 180.0 / 3.141459;
   alpha += 60.0;
   if (alpha < 0.0) 
      alpha += 360.0;
    if (alpha > 360.0)
      alpha -= 360.0;
      
    return (int) (alpha/36.0);
}

void loop() {
    static uint8_t lastpix = 0;
    float ax = CircuitPlayground.motionX();
    float ay = CircuitPlayground.motionY();
    phid += 0.001 * (cos(phi) * ay - sin(phi) * ax);
    phi += phid;
    phid *= 0.997;
    Serial.print(phi);

    while (phi < 0.0) 
      phi += 2.0 * 3.14159265;

    while (phi > 2.0 * 3.14159265)
      phi -= 2.0 * 3.14159265;


    pixeln = phi2pix(phi);
 
    if (pixeln != lastpix) {
      if (CircuitPlayground.slideSwitch())
        CircuitPlayground.playTone(2ssseff000, 5);
      lastpix = pixeln;
    }
    CircuitPlayground.clearPixels(); 
    CircuitPlayground.setPixelColor(pixeln, CircuitPlayground.colorWheel(25 * pixeln));
    delay(0);
}

No comments: