Skip to content

Instantly share code, notes, and snippets.

@zSpaceSheikh
Created May 31, 2019 18:27
Show Gist options
  • Save zSpaceSheikh/224918c7606b41232ce24480c0d2d62a to your computer and use it in GitHub Desktop.
Save zSpaceSheikh/224918c7606b41232ce24480c0d2d62a to your computer and use it in GitHub Desktop.
Final Project - Interactive Window Box
// Full code for the interactive window box
#include <CapacitiveSensor.h>
#define CAPTOUCHPOWER 13
#define CAPTOUCHRECIEVE 5
CapacitiveSensor cs_4_2 = CapacitiveSensor(CAPTOUCHPOWER,CAPTOUCHRECIEVE); // Needs a 1MOhm resistor between them
#include <Servo.h>
Servo servoOne;
Servo servoTwo;
#define SERVOONEPIN 8
#define SERVOTWOPIN 9
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIXELPINONE 4 // NeoPixel Strip → Pin 4
#define PIXELPINTWO 7 // NeoPixel Strip → Pin 7
#define SHORTPIXELPIN 10 // NeoPixel Strip → Pin 10
#define INNERPIXELPIN 6 // NeoPixel Strip → Pin 6
#define ONEPIN_GATE_IN 2 // Gate → Pin 2
#define ONEIRQ_GATE_IN 0
#define ONEPIN_LED_OUT 13
#define ONEPIN_ANALOG_IN A0 // Envelope → A1
#define TWOPIN_GATE_IN 3 // Gate → Pin 3
#define TWOIRQ_GATE_IN 0
#define TWOPIN_LED_OUT 13
#define TWOPIN_ANALOG_IN A2 // Envelope → A2
#define NUMPIXELS 30
#define STRAND 10
Adafruit_NeoPixel pixelsOne = Adafruit_NeoPixel(NUMPIXELS, PIXELPINONE, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelsTwo = Adafruit_NeoPixel(NUMPIXELS, PIXELPINTWO, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelsShort = Adafruit_NeoPixel(3, SHORTPIXELPIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelsInner = Adafruit_NeoPixel(14, INNERPIXELPIN, NEO_GRB + NEO_KHZ800);
int delayval = 50; // delay time
int j; // counter strand 2
int m; // counter strand 3
int mid = NUMPIXELS/2; // center of the strip
int trigPin = 11; // Trigger
int echoPin = 12; // Echo
long duration, cm, inches;
int audioVal1;
int audioVal2;
int audioAve;
//Color values for the whole strip between 0 and 255 -------- If the values are too high the Arduino pulls too much power
int R = 100;
int G = 100;
int B = 100;
long rand1;
long rand2;
int rand3;
int rand4;
void soundISR(){
int pin_val1;
int pin_val2;
pin_val1 = digitalRead(ONEPIN_GATE_IN);
digitalWrite(ONEPIN_LED_OUT, pin_val1);
pin_val2 = digitalRead(TWOPIN_GATE_IN);
digitalWrite(TWOPIN_LED_OUT, pin_val2);
}
void setup() {
// This initializes the NeoPixel library.
pixelsOne.begin();
pixelsTwo.begin();
pixelsShort.begin();
pixelsInner.begin();
Serial.begin(9600);
// Configure LED pin as output
pinMode(ONEPIN_LED_OUT, OUTPUT);
pinMode(TWOPIN_LED_OUT, OUTPUT);
// configure input to interrupt
pinMode(ONEPIN_GATE_IN, INPUT);
attachInterrupt(ONEIRQ_GATE_IN, soundISR, CHANGE);
pinMode(TWOPIN_GATE_IN, INPUT);
attachInterrupt(TWOIRQ_GATE_IN, soundISR, CHANGE);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servoOne.attach(8);
servoTwo.attach(9);
servoOne.write(15);
servoTwo.write(155);
}
void loop() {
//Ultrasonic Sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
// While there isn't a user close to the box
while(cm>49){
// Check the noise in the room
audioVal1 = analogRead(ONEPIN_ANALOG_IN);
audioVal2 = analogRead(TWOPIN_ANALOG_IN);
audioAve = (audioVal1+audioVal2)/2;
// Low noise
if(audioAve<40){
rand1 = random(0,STRAND/3);
rand2 = random(0,STRAND/3);
rand3 = random(0,STRAND/3);
// Set first strand section LEFT
for(int i=0; i<rand1+1; i++){
pixelsOne.setPixelColor(i, pixelsOne.Color(R,0,0));
pixelsOne.show();
delay(10);
}
if(rand1<STRAND/3){
for(int k=(2*STRAND)/3; k>rand1; k--){
pixelsOne.setPixelColor(k, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
// Set first strand section RIGHT
for(int i=0; i<rand1+1; i++){
pixelsTwo.setPixelColor(i, pixelsTwo.Color(R,0,0));
pixelsTwo.show();
delay(10);
}
if(rand1<STRAND/3){
for(int k=(2*STRAND)/3; k>rand1; k--){
pixelsTwo.setPixelColor(k, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
// Set second strand section RIGHT
for(int i=0; i<=rand2+1; i++){
j=(2*STRAND)-i;
pixelsTwo.setPixelColor(j, pixelsTwo.Color(0,G,0));
pixelsTwo.show();
delay(10);
}
if(rand2<(2*STRAND)-(STRAND/3)){
for(int k=(2*STRAND)/3; k>rand2; k--){
j=(2*STRAND)-k;
pixelsTwo.setPixelColor(j, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
// Set second strand section LEFT
for(int i=0; i<=rand2+1; i++){
j=(2*STRAND)-i;
pixelsOne.setPixelColor(j, pixelsOne.Color(0,G,0));
pixelsOne.show();
delay(10);
}
if(rand2<(2*STRAND)-(STRAND/3)){
for(int k=(2*STRAND)/3; k>rand2; k--){
j=(2*STRAND)-k;
pixelsOne.setPixelColor(j, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
// Set third strand section RIGHT
for(int i=0; i<rand3+1; i++){
m=(2*STRAND)+i;
pixelsTwo.setPixelColor(m, pixelsTwo.Color(0,0,B));
pixelsTwo.show();
delay(10);
}
if(rand3<STRAND/3){
for(int k=(2*STRAND)/3; k>rand3; k--){
m=(2*STRAND)+k;
pixelsTwo.setPixelColor(m, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
// Set third strand section LEFT
for(int i=0; i<rand3+1; i++){
m=(2*STRAND)+i;
pixelsOne.setPixelColor(m, pixelsOne.Color(0,0,B));
pixelsOne.show();
delay(10);
}
if(rand3<STRAND/3){
for(int k=(2*STRAND)/3; k>rand3; k--){
m=(2*STRAND)+k;
pixelsOne.setPixelColor(m, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
}
// Medium noise
else if(audioAve>39 && audioAve<60){
rand1 = random(STRAND/3,(2*STRAND)/3);
rand2 = random(STRAND/3,(2*STRAND)/3);
rand3 = random(STRAND/3,(2*STRAND)/3);
// Set first strand section LEFT
for(int i=0; i<rand1; i++){
pixelsOne.setPixelColor(i, pixelsOne.Color(R,0,0));
pixelsOne.show();
delay(10);
}
if(rand1<(2*STRAND)/3){
for(int k=STRAND; k>rand1; k--){
pixelsOne.setPixelColor(k, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
// Set first strand section RIGHT
for(int i=0; i<rand1; i++){
pixelsTwo.setPixelColor(i, pixelsTwo.Color(R,0,0));
pixelsTwo.show();
delay(10);
}
if(rand1<(2*STRAND)/3){
for(int k=STRAND; k>rand1; k--){
pixelsTwo.setPixelColor(k, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
// Set second strand section LEFT
for(int i=0; i<rand2+1; i++){
j=(2*STRAND)-i;
pixelsOne.setPixelColor(j, pixelsOne.Color(0,G,0));
pixelsOne.show();
delay(10);
}
if(rand2<(2*STRAND)-(2*STRAND)/3){
for(int k=STRAND; k>rand2; k--){
j=(2*STRAND)-k;
pixelsOne.setPixelColor(j, pixelsOne.Color(0,0,0));
pixelsOne.show();
//delay(10);
}
}
// Set second strand section RIGHT
for(int i=0; i<rand2+1; i++){
j=(2*STRAND)-i;
pixelsTwo.setPixelColor(j, pixelsTwo.Color(0,G,0));
pixelsTwo.show();
delay(10);
}
if(rand2<(2*STRAND)-(2*STRAND)/3){
for(int k=STRAND; k>rand2; k--){
j=(2*STRAND)-k;
pixelsTwo.setPixelColor(j, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
//delay(10);
}
}
// Set third strand section LEFT
for(int i=0; i<rand3; i++){
m=(2*STRAND)+i;
pixelsOne.setPixelColor(m, pixelsOne.Color(0,0,B));
pixelsOne.show();
delay(10);
}
if(rand3<(2*STRAND)/3){
for(int k=STRAND; k>rand3; k--){
m=(2*STRAND)+k;
pixelsOne.setPixelColor(m, pixelsOne.Color(0,0,0));
pixelsOne.show();
//delay(10);
}
}
// Set third strand section RIGHT
for(int i=0; i<rand3; i++){
m=(2*STRAND)+i;
pixelsTwo.setPixelColor(m, pixelsTwo.Color(0,0,B));
pixelsTwo.show();
delay(10);
}
if(rand3<(2*STRAND)/3){
for(int k=STRAND; k>rand3; k--){
m=(2*STRAND)+k;
pixelsTwo.setPixelColor(m, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
//delay(10);
}
}
}
// High noise
else{
rand1 = random((2*STRAND)/3, STRAND);
rand2 = random((2*STRAND)/3, STRAND);
rand3 = random((2*STRAND)/3, STRAND);
// Set first strand section LEFT
for(int i=0; i<rand1; i++){
pixelsOne.setPixelColor(i, pixelsOne.Color(R,0,0));
pixelsOne.show();
delay(10);
}
if(rand1<STRAND){
for(int k=STRAND; k>rand1; k--){
pixelsOne.setPixelColor(k, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
// Set first strand section RIGHT
for(int i=0; i<rand1; i++){
pixelsTwo.setPixelColor(i, pixelsTwo.Color(R,0,0));
pixelsTwo.show();
delay(10);
}
if(rand1<STRAND){
for(int k=STRAND; k>rand1; k--){
pixelsTwo.setPixelColor(k, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
// Set second strand section LEFT
for(int i=0; i<rand2+1; i++){
j=(2*STRAND)-i;
pixelsOne.setPixelColor(j, pixelsOne.Color(0,G,0));
pixelsOne.show();
delay(10);
}
if(rand2<(2*STRAND)){
for(int k=(2*STRAND); k>rand2; k--){
j=(2*STRAND)-k;
pixelsOne.setPixelColor(j, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
// Set second strand section RIGHT
for(int i=0; i<rand2+1; i++){
j=(2*STRAND)-i;
pixelsTwo.setPixelColor(j, pixelsTwo.Color(0,G,0));
pixelsTwo.show();
delay(10);
}
if(rand2<(2*STRAND)){
for(int k=(2*STRAND); k>rand2; k--){
j=(2*STRAND)-k;
pixelsTwo.setPixelColor(j, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
// Set third strand section LEFT
for(int i=0; i<rand3; i++){
m=(2*STRAND)+i;
pixelsOne.setPixelColor(m, pixelsOne.Color(0,0,B));
pixelsOne.show();
delay(10);
}
if(rand3<STRAND){
for(int k=STRAND; k>rand3; k--){
m=(2*STRAND)+k;
pixelsOne.setPixelColor(m, pixelsOne.Color(0,0,0));
pixelsOne.show();
delay(10);
}
}
// Set third strand section RIGHT
for(int i=0; i<rand3; i++){
m=(2*STRAND)+i;
pixelsTwo.setPixelColor(m, pixelsTwo.Color(0,0,B));
pixelsTwo.show();
delay(10);
}
if(rand3<STRAND){
for(int k=STRAND; k>rand3; k--){
m=(2*STRAND)+k;
pixelsTwo.setPixelColor(m, pixelsTwo.Color(0,0,0));
pixelsTwo.show();
delay(10);
}
}
}
// Ultrasonic sensor check
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
Serial.print(cm);
Serial.print("cm");
Serial.println();
}
// Turns off all of the ambient nosie visualizations
//RIGHT
for(int i=0; i<NUMPIXELS; i++){
pixelsTwo.setPixelColor(i, pixelsTwo.Color(0,0,0));
}
pixelsTwo.show();
//LEFT
for(int i=0; i<NUMPIXELS; i++){
pixelsOne.setPixelColor(i, pixelsOne.Color(0,0,0));
}
pixelsOne.show();
// Opens the shutter doors by turning on the servos
//Servo 1 moving to open position
for(int i=15; i<151; i++){
servoOne.write(i);
delay(20);
}
//Servo 2 moving to open position
for(int i=155; i>49; i--){
servoTwo.write(i);
delay(20);
}
// Inside the box the lights turn on to their base grey glow
R=10;
G=10;
B=10;
// Acrylic lit
for(int i=0; i<NUMPIXELS; i++){
pixelsInner.setPixelColor(i, pixelsInner.Color(R,G,B));
}
pixelsInner.show();
//Plant Light
for(int i=0; i<NUMPIXELS; i++){
pixelsShort.setPixelColor(i, pixelsShort.Color(100,100,100));
}
pixelsShort.show();
// While the user is detected let their interactions change the lights
while(cm<50){
// Capacitive touch sensor for the plant
long touch = cs_4_2.capacitiveSensor(30);
if(touch>500){
R=R+1;
G=G+1;
B=B+1;
}
for(int i=0; i<NUMPIXELS; i++){
pixelsInner.setPixelColor(i, pixelsInner.Color(R,G,B));
}
pixelsInner.show();
delay(50);
//Ultrasonic Sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
/*
Serial.print(cm);
Serial.print("cm");
Serial.println();
*/
}
// After the user leaves the lights turn off
// Acrylic lit
for(int i=0; i<NUMPIXELS; i++){
pixelsInner.setPixelColor(i, pixelsInner.Color(0,0,0));
}
pixelsInner.show();
//Plant Light
for(int i=0; i<NUMPIXELS; i++){
pixelsShort.setPixelColor(i, pixelsShort.Color(0,0,0));
}
pixelsShort.show();
// The RGB values go back to normal
R=10;
G=10;
B=10;
// Closes the shutter doors by turning on the servos again
for(int i=150; i>14; i--){
servoOne.write(i);
delay(20);
}
//Servo 2 moving to closed position
for(int i=50; i<156; i++){
servoTwo.write(i);
delay(20);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment