Overall, my project ended up working, but not exactly the way that I wanted it to. My biggest hurdle were my tilt switch sensors and the hard, stiff wiring that I soldered them too. This prevented the flexibility and sensitivity I ultimately needed from the sensors to detect movement from the tubes. Overall, the code and sensors worked, some tubes just had a hard time picking up the motion, while others were more sensitive. Also, it is way harder to not only work, but also fabricate a project that hangs! I have learned my lesson.
I was unable to have enough time to get the serial communication to work properly so that the sensors that already trigger the LEDs would also trigger a byte to send to my P5 sketch and call the sound. The code was taken from Danny Rozen and can be found here. At first it would not read binary, but ASCII. I was using the p5.serial control, but when I selected my port (/dev/cu.usbmodem14201), It would keep switching back to Bluetooth instead. I was able to play the sound files from the P5 sketch by themselves, so I knew they worked. My problem was getting the Arduino code to send the proper byte to my P5 when the sensor reached one.
With Arduino, I tried:
byte byteToSend = 1;
Serial.write(byteToSend);
But I was confused where to put this code. When I put it within the code below, twice with different numbers 1 and 50 so I could track where it was reading the bytes, it only caught the 50, but not the 1. I feel like there is something wrong with my states in sending the proper change to trigger the sound, but what makes me confused about that is if I had something wrong with my states, then the LEDs shouldn’t respond to the tilt switch sensors?
At the end, I had didn’t have enough time and had to focus on the sensors.
Below is a video of my frustrations and confusions with working with a hanging project and my stiff wired sensors that failed to be sensitive enough to hand movements.
Code Code Code:
Previously Documented Progress of How I Got Here:
Code Set Up:
First, I needed to include the FastLED library I would be using to program my LEDs to the sensors I was using as well as define the number of LEDs I wanted to program, which was 39 in total:
#include “FastLED.h”
#define NUM_LEDS 39
Then, I wanted to program the microcontroller to read the tilt switch sensors. I only cared about the sensor being off (not touched, 0) or on (touched, 1) – two states. In order to do this, not only would I need to know the current state of the sensor but also remember it’s previous state – it’s state change detection. I would have to set up 6 global variables to store each of the six tilt switch sensors state detection:
int lastSensorState1 = 0;
int lastSensorState2 = 0;
int lastSensorState3 = 0;
int lastSensorState4 = 0;
int lastSensorState5 = 0;
int lastSensorState6 = 0;
I also wanted to set up some other global variables such as hue, counter and LED state that would relate back to the LEDs later in my code:
int hue = 0;
int counter = 0;
int fadeIncrement = 1;
int LEDstate = false; //rest state
CRGB leds[39];
Now for the set up of my code, I needed to link each pin on my microcontroller to an input:
void setup() {
FastLED.addLeds<NEOPIXEL, 6>(leds, 39);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
Then I needed to link each of these inputs to one of my six sensors:
void loop() {
int sensorState1 = digitalRead(2);
int sensorState2 = digitalRead(3);
int sensorState3 = digitalRead(4);
int sensorState4 = digitalRead(5);
int sensorState5 = digitalRead(7);
int sensorState6 = digitalRead(8);
Linking LEDs to the Sensors:
Now that I had everything set up in my code, I wanted to then link each of the six sensors to a certain number of individual LEDs on my strip, which would create a tube on my wind chime. I needed to make sure that my code would read the sensor, check the current sensor state against it’s last state and then save that current state of the sensors in a variable for the next time the code loops. Then I would know if sensorState is not equal to lastSensorState, my sensor has changed.
I also wanted to create an LEDstate to make it easier for me to code that if the sensor state was on (1), then that should trigger my LEDstate to be true, meaning that my LED also had two states depending on the sensor state. If my sensor state was on (1) then it would trigger my LEDstate to display a certain pattern (in this case a rainbow movement). Otherwise, if my sensor state was off (0) then my LEDs would continue their blue/violet glow.
//////// TUBE 1 ////////
if (sensorState1 != lastSensorState1 && sensorState1 == 1) {
LEDstate = true;
} else {
LEDstate = false;
}
I also wanted to set up a counter so that my state change and LEDchange into the rainbow pattern would only last a certain length of time. I would also program a certain number of LEDs to be affected by this sensor change and LEDstate (which was the number of LEDs in tube 1 or any tube linked to that sensor).
if(LEDstate){
for(counter=0; counter<100; counter++){
for (int i = 1; i < 10; i++) {
leds[i] = CRGB::White;
FastLED.show();
}
LEDstate = false;
}
} else {
for (int i = 1; i < 10; i++) {
leds[i] = CRGB::Blue;
FastLED.showColor(CHSV(hue++, 255, 255));
}
}
lastSensorState1 = sensorState1;
}
I wanted to get the sensors linked to the LEDs properly before I then properly coded the colors that I wanted. In this case above, I set the colors of the LEDs to be either white or blue to signify clearly if my sensors were triggering my LEDs properly.
Below is a video of me doing a simple sensor state detection with the tilt switch sensor and LEDs using the serial monitor to track my sensor state on one tube only:
Color Tuning the Code:
HSVHue {
HUE_RED = 0, HUE_ORANGE = 32,
HUE_YELLOW = 64, HUE_GREEN = 96,
HUE_AQUA = 128, HUE_BLUE = 160,
HUE_PURPLE = 192, HUE_PINK = 224
}
Looking at FastLED’s Hue color code, I know that I only want half of this color spectrum (blue, purple and pink) so my hue should only be above 128, which would cut out the colors I did not want.
Now, this may not be the cleanest way to write out the code, but I knew that instead of of the hue being hue++ along 0 to 255, I wanted hue to go back and forth between 128 and 224. Start at 128 and go up each increment, then at 224, go down each increment and repeat. So I created a global variable for the direction of the hue called direct and put it in an if and else if loop. I also made the global variable hue start at 128.
FastLED.showColor(CHSV(hue+=direct, 255, 255));
if (hue < 128){
direct = 1;
} else if (hue > 224) {
direct = – 1;
}
Circuits:
Now I had to figure out how to organize all my tilt switch sensor circuits…
I also tried to diffuse the LEDs more by adding bubble wrap to the tubes, but making enough room for the sensors to be affected by movement and touch of the user.