- Deal with the code-work VS work BETTER
- The printed tilt sensor data in the loop
- Color change pattern from Neopixel; Program LED lights with more creative patterns
I’ve spent a lot of time this week refining my code. It’s like I’m back to square one, the beginning of the semester – I’m finding and rebalancing my mindset, because code can be infinitely refined, but it’s a challenge to capture the equilibrium of needs and expectations.
First of all, there was the fundamental logic that had to be done, namely the merging of the tilt sensor and Neopixel functionality. I had many setbacks in this step, and for a while I thought I was having the exact same problem with Madeline, which is the LEDs were not of good quality, they would go on and off, and the tilt sensor was not printing values nearly as fast as when it was running on its own. I then realised that it turned out to be that the two together LOOP in sequential order, and that it was very slow to complete a turn. I needed the two systems to run efficiently together, so I came up with two solutions.
One was to separate the two completely. Unfortunately, this isn’t compatible with this version of Arduino, so I gave up after half an hour of fiddling with it. The other was to shorten the instructions for each loop session. Simply put, split the part of the light change round into light on and light off. Then keep interspersing tilt sensor print instructions in between. Fortunately, this worked! The frequency of light flashes and tilt sensor prints increased dramatically.
In addition, for visual interest, I set the base colours so that the whole strip would flash six different colours at the same time (since there are six beads on each strip), and they would all come on and go off at the same time. I added two variables: the brightness of the lights and the frequency of the lights flashing. Firstly the brightness: it is controlled by the X value. That is, when the dancers close their arms inwards, or spread their arms like a swan spreading its wings, the light will become extra bright. And for the flickering frequency of the light, since the wings are most of the time perpendicular to the ground, even if stretched outwards or internally, I decided to make a huge change in the frequency of the corresponding light when it curls up (usually at the same time the actor stretches his arms violently forwards or backwards). A rainbow band that changes extremely fast and switches in the same colour would stretch from the end of the wing to the front.
Here’s the altered code. I’m really happy with the result. It’s a question of “good” and “better”: when there’s no definitive final solution to a project, how satisfied am I? My answer is that the moment I see the lonely stars turn into a smooth river of stars between movements, the moment the testers get excited, I know that I’ve accomplished what I wanted both aesthetically and functionally.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <Adafruit_CircuitPlayground.h>
#ifndef MY_HEADER_H
#define MY_HEADER_H
// Contents of the header file
#endif // MY_HEADER_H
#define LED_PIN 10
#define LED_COUNT 6
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Declaration of the colorWipe function
void colorWipe(uint32_t color, int wait);
void rainbow(int wait);
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin();
strip.show();
strip.setBrightness(50);
Serial.begin(9600);
Serial.println("Circuit Playground test!");
CircuitPlayground.begin();
}
void loop() {
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(0, 0, 255), 50); // Blue
rainbow(20); // Adjust the delay value as needed
}
// Definition of the colorWipe function
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
// Definition of the rainbow function
void rainbow(int wait) {
for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
strip.rainbow(firstPixelHue);
// Get motion values
float motionZ = CircuitPlayground.motionZ();
float motionY = CircuitPlayground.motionY();
// Adjust brightness based on motionX
int brightness = map(abs(motionZ), 0, 10, 1, 50);
// Adjust speed based on motionY
int speed = map(abs(motionY), 0, 10, 0, 255);
strip.setBrightness(brightness);
// Display the results (acceleration is measured in m/s*s)
Serial.print("X: "); Serial.print(CircuitPlayground.motionX());
Serial.print(" \tY: "); Serial.print(CircuitPlayground.motionY());
Serial.print(" \tZ: "); Serial.print(CircuitPlayground.motionZ());
Serial.println(" m/s^2");
strip.show();
delay(speed);
}
}
* Note: Because we were so excited, we forgot to shoot the video. This is part of the video that was taken later.