the main purpose of the boxes behavior is to create a series of reactions between the cubes. each box rotates 90 degrees once a sensor is activated so as to face the surfaces of the cubes beside it. when the sound sensor is activated the cube after the clockwise rotation produces light, the light itself activates the sound production from the cube beside. a system of reactions is produced by the cubes, which once it is interrupted by the human action it changes rotating direction and produces light so as to activate a different series of sequencies.
here is the script uploaded to the circuit:
#include <WProgram.h>
void setup();
void loop();
int main();
// declare your custom functions here
void playNote(byte note, byte duration);
void playRandomNote(byte duration);
void setLampOn();
void setLampOff();
void rotateClockwise(int steps);
void rotateAntiClockwise(int steps);
void readLightSensor();
void readSoundSensor();
void readTouchSensor();
// set variables, do not change
int soundSensorVcc = 0;
int lightSensorPin = 1;
int soundSensorPin = 3;
int touchSensorPin = 2;
int lampPin = 3;
int speakerPin = 4;
int lightSensor;
int soundSensor;
int touchSensor;
int val;
int state;
int mode;
byte names[] ={
‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’};
int tones[] = {
1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int motorPins[] = {
7, 8, 9, 10};
int count = 0;
int count2 = 0;
int motorSpeed = 60;
//declare your custom variables here
int rotation = 0;
bool direction;
int position=1;
// functions executed only once after start up go here
void setup() {
for (count = 0; count < 4; count++) {
pinMode(motorPins[count], OUTPUT);
}
pinMode (soundSensorVcc,OUTPUT);
pinMode(speakerPin, OUTPUT);
pinMode (lampPin,OUTPUT);
playRandomNote(100);
digitalWrite(soundSensorVcc,HIGH);
mode = 1;
/* START UP */
direction = 1;
for (int i=0;i<25;i++) {
rotateClockwise(1);delay(10);setLampOn();delay(500);setLampOff();
}
}
// functions executed forever in a loop go here
void loop() {
// read all sensors
readLightSensor();
readSoundSensor();
readTouchSensor();
// check mode
switch (mode) {
case 1:
/* LOOP */
if (soundSensor) {rotateClockwise(25);delay(10);setLampOn();delay(500);setLampOff();}
if (touchSensor) {rotateAntiClockwise(25);delay(10);setLampOn();delay(500);setLampOff();}
if (lightSensor) {rotateClockwise(25);delay(10);playNote(’a',100);delay(300);playNote(’c',100);delay(300);playNote(’g',100);}
case 2:
break;
}
}
// custom functions should go here
void readLightSensor() {
val = analogRead(lightSensorPin);
if (val < 950) {
lightSensor = HIGH;
}
else {
lightSensor = LOW;
}
}
void readSoundSensor() {
// val = analogRead(soundSensorPin); // discard first reading
val = analogRead(soundSensorPin);
if (val < 83) {
soundSensor = HIGH;
}
else {
soundSensor = LOW;
}
}
void readTouchSensor() {
val = analogRead(touchSensorPin);
if (val > 10) {
touchSensor = HIGH;
}
else {
touchSensor = LOW;
}
}
void rotateClockwise(int steps) {
for (int i=0;i<steps;i++) {
if ((count2 == 0) || (count2 == 1)) {
count2 = 16;
}
count2>>=1;
for (count = 3; count >= 0; count–) {
digitalWrite(motorPins[count], count2>>count&0×01);
}
delay(motorSpeed);
}
}
void rotateAntiClockwise(int steps) {
for (int i=0;i<steps;i++) {
if ((count2 == 0) || (count2 == 1)) {
count2 = 16;
}
count2>>=1;
for (count = 3; count >= 0; count–) {
digitalWrite(motorPins[3 - count], count2>>count&0×01);
}
delay(motorSpeed);
}
}
void playNote (byte note,byte duration) {
for (count=0;count<=8;count++) { // look for the note
if (names[count] == note) { // ahh, found it
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
}
}
}
}
void playRandomNote (byte duration) {
count = random(7);
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
}
}
void setLampOn () {
digitalWrite(lampPin,HIGH);
// analogWrite(lampPin,255);
}
void setLampOff () {
digitalWrite(lampPin,LOW);
// digitalWrite(lampPin,LOW);
}
// do not modify the main function
int main() {
init();
setup();
for (;;)
loop();
return 0;
}