/* Blink Turns on an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the Uno and Leonardo, it is attached to digital pin 13. If you're unsure what pin the on-board LED is connected to on your Arduino model, check the documentation at http://www.arduino.cc This example code is in the public domain. modified 8 May 2014 by Scott Fitzgerald */ const int buttonC = 7; const int buttonS = 8; const int groen = 6; const int rood = 9; const int blauw = 10; const int echoPin = A0; const int pingPin = 12; int maximumRange = 50; // Maximum range needed int minimumRange = 0; // Minimum range needed long duration, distance; // Duration used to calculate distance int black[3] = { 0, 0, 0 }; int white[3] = { 100, 100, 100 }; int red[3] = { 100, 0, 0 }; int green[3] = { 0, 100, 0 }; int blue[3] = { 0, 0, 100 }; int yellow[3] = { 40, 95, 0 }; int dimWhite[3] = { 30, 30, 30 }; int redVal = black[0]; int grnVal = black[1]; int bluVal = black[2]; int wait = 1; // 10ms internal crossFade delay; increase for slower fades int hold = 0; // Optional hold when a color is complete, before the next crossFade int loopCount = 60; // How often should DEBUG report? int repeat = 0; // How many times should we loop before stopping? (0 for no stop) int j = 0; // Loop counter for repeat int prevR = redVal; int prevG = grnVal; int prevB = bluVal; int sensorPin = A1; int sensorValue = 0; int statusButton; int color = 0; int state = 0; // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. Serial.begin(9600); pinMode(buttonC, INPUT); pinMode(buttonS, INPUT); //pinMode(sensorPin, INPUT); pinMode(groen, OUTPUT); pinMode(rood, OUTPUT); pinMode(blauw, OUTPUT); pinMode(pingPin, OUTPUT); //pinMode(echoPin, INPUT); } // the loop function runs over and over again forever void loop() { statusButton = digitalRead(buttonS); if(statusButton == HIGH) { delay(50); statusButton = digitalRead(buttonS); while(statusButton == HIGH) { statusButton = digitalRead(buttonS); delay(50); } state++; } if(state >= 3) { state = 0; } if(state == 0) { colorSelect(); } if(state == 1) { fade(); distanceDetect(); } if(state == 2) { soundDetect(); } } void colorSelect() { int colorButton; colorButton = digitalRead(buttonC); if(colorButton == HIGH) { delay(50); colorButton = digitalRead(buttonC); while(colorButton == HIGH) { colorButton = digitalRead(buttonC); delay(50); } color++; if(color >= 7)color=0; states(color); } } void fade() { if(state == 1)crossFade(red);distanceDetect(); if(state == 1)crossFade(green);distanceDetect(); if(state == 1)crossFade(blue);distanceDetect(); if(state == 1)crossFade(yellow);distanceDetect(); if (repeat) { // Do we loop a finite number of times? j += 1; if (j >= repeat) { // Are we there yet? exit(j); // If so, stop. } } } void soundDetect() { sensorValue = analogRead (sensorPin); if(sensorValue > 500) { color++; delay(100); } if(color >= 7)color=0; Serial.println (sensorValue, DEC); states(color); } void states(int kleur) { Serial.println(kleur); switch(kleur) { case 0: digitalWrite(blauw, LOW);digitalWrite(rood, HIGH);digitalWrite(groen, HIGH);break; case 1: digitalWrite(blauw, HIGH);digitalWrite(rood, LOW);digitalWrite(groen, HIGH);break; case 2: digitalWrite(blauw, HIGH);digitalWrite(rood, HIGH);digitalWrite(groen, LOW);break; case 3: digitalWrite(blauw, LOW);digitalWrite(rood, LOW);digitalWrite(groen, HIGH);break; case 4: digitalWrite(blauw, LOW);digitalWrite(rood, HIGH);digitalWrite(groen, LOW);break; case 5: digitalWrite(blauw, HIGH);digitalWrite(rood, LOW);digitalWrite(groen, LOW);break; case 6: digitalWrite(blauw, LOW);digitalWrite(rood, LOW);digitalWrite(groen, LOW);break; } } int calculateStep(int prevValue, int endValue) { int step = endValue - prevValue; // What's the overall gap? if (step) { // If its non-zero, step = 1020/step; // divide by 1020 } return step; } int calculateVal(int step, int val, int i) { if ((step) && i % step == 0) { // If step is non-zero and its time to change a value, if (step > 0) { // increment the value if step is positive... val += 1; } else if (step < 0) { // ...or decrement it if step is negative val -= 1; } } // Defensive driving: make sure val stays in the range 0-255 if (val > 255) { val = 255; } else if (val < 0) { val = 0; } return val; } void crossFade(int color[3]) { // Convert to 0-255 int R = (color[0] * 255) / 100; int G = (color[1] * 255) / 100; int B = (color[2] * 255) / 100; int stepR = calculateStep(prevR, R); int stepG = calculateStep(prevG, G); int stepB = calculateStep(prevB, B); for (int i = 0; i <= 1020; i++) { redVal = calculateVal(stepR, redVal, i); grnVal = calculateVal(stepG, grnVal, i); bluVal = calculateVal(stepB, bluVal, i); analogWrite(rood, redVal); // Write current values to LED pins analogWrite(groen, grnVal); analogWrite(blauw, bluVal); statusButton = digitalRead(buttonS); if(statusButton == HIGH) { delay(50); statusButton = digitalRead(buttonS); while(statusButton == HIGH) { statusButton = digitalRead(buttonS); delay(50); } state++; break; } delay(wait); // Pause for 'wait' milliseconds before resuming the loop } // Update current values for next loop prevR = redVal; prevG = grnVal; prevB = bluVal; delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop } void distanceDetect() { long duration, inches, cm; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); // The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); // convert the time into a distance cm = microsecondsToCentimeters(duration); Serial.println(cm); if(cm < 150) { digitalWrite(blauw, HIGH); digitalWrite(rood, HIGH); digitalWrite(groen, HIGH); while(cm < 150) { pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); cm = microsecondsToCentimeters(duration); Serial.println(cm); } } } long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }