Project 2

 

Solenoid Driven Harmonium prototype

 

IMG_2618 IMG_2615IMG_2617

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Code:

 

 

Step One:

 

 

Wiring 5 solenoids to have them run when a button is pressed,

LED13 is also lit for confirmation :

 

*/

const int buttonPin1 = 7;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

const int solPin1 =  6;

const int solPin2 =  5;

const int solPin3 =  4;

const int solPin4 =  3;

const int solPin5 =  2;

int buttonState1 = 0;         // variable for reading the pushbutton status

 

 

void setup() {

// initialize serial communications (for debugging only):

Serial.begin(9600);

pinMode(ledPin, OUTPUT);         // initialize the LED pin as an output

pinMode(solPin1, OUTPUT);

pinMode(solPin2, OUTPUT);

pinMode(solPin3, OUTPUT);

pinMode(solPin4, OUTPUT);

pinMode(solPin5, OUTPUT);

pinMode(buttonPin1, INPUT);         // initialize the pushbutton pin as an input:

}

 

 

void loop(){

// read the state of the pushbutton value:

buttonState1 = digitalRead(buttonPin1);

 

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState1 == HIGH) {

digitalWrite(ledPin, HIGH); //turn on LED

digitalWrite(solPin1, HIGH);

digitalWrite(solPin2, HIGH);

digitalWrite(solPin3, HIGH);

digitalWrite(solPin4, HIGH);

digitalWrite(solPin5, HIGH);

Serial.println(“button pressed down”);

}

else {

digitalWrite(ledPin, LOW);   // turn LED off

digitalWrite(solPin1, LOW);

digitalWrite(solPin2, LOW);

digitalWrite(solPin3, LOW);

digitalWrite(solPin4, LOW);

digitalWrite(solPin5, LOW);

Serial.println(“button NOT pressed down”);

}

}

 

 

 

 

 

 

Introducing patterns into the solenoids / ala LEDs

patterns are fixed, with the addition of the some sound that plays when the button is pressed:

 

 

 

/*

patterns by pressing the button

*/

 

 

const int buttonPin1 = 7;     // the number of the pushbutton pin

const int ledPin =  13;

 

int SolPins[] = {2,3,4,5,6};

int buttonState1 = 0;

int thisDelay = thisDelay;

 

 

 

void setup()

{

Serial.begin(9600);

//Set each pin connected to an LED to output mode (pulling high (on) or low (off)

for(int i = 0; i < 5; i++){         //this is a loop and will repeat twice

pinMode(SolPins[i],OUTPUT);

pinMode(ledPin, OUTPUT);

pinMode(buttonPin1, INPUT);

 

 

}

}

 

/*

* loop() – this function will start after setup finishes and then repeat

* we call a function called oneAfterAnother(). if you would like a different behaviour

* uncomment (delete the two slashes) one of the other lines

*/

void loop()                     // run over and over again

{

int sensorReading = analogRead(A3);

int sensorReading1 = analogRead(A0);

int sensorReadingsound = sensorReading * sensorReading1 /100 ;

int sensorReadingDelay = sensorReading / sensorReading1;

buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == HIGH) {

digitalWrite(ledPin, HIGH);

int thisPitch = map(sensorReadingsound, 100, 10000, 10, 500);

tone(12, thisPitch, 10);

oneAfterAnotherNoLoop();   //this will turn on each LED one by one then turn each off

//oneAfterAnotherLoop();   //does the same as oneAfterAnotherNoLoop but with

//much less typing

//oneOnAtATime();          //this will turn one LED on then turn the next one

//on turning the

//former off (one LED will look like it is scrolling

//along the line

//inAndOut();              //lights the two middle LEDs then moves them out then back

//in again

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

thisDelay = map(sensorReadingDelay * 100, 100, 1000, 100,1000);

Serial.println(sensorReadingDelay * 100);

delay(70);        // delay in between reads for stability

}

 

 

 

//1st sequence

 

void oneAfterAnotherNoLoop(){

int delayTime = thisDelay;

digitalWrite(SolPins[0], HIGH);  //Turns on Solenoid #0 (connected to pin 2 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[1], HIGH);  //Turns on Solenoid #1 (connected to pin 3 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[2], HIGH);  //Turns on Solenoid #2 (connected to pin 4 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[3], HIGH);  //Turns on Solenoid #3 (connected to pin 5 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[4], HIGH);  //Turns on Solenoid #4 (connected to pin 6 )

delay(delayTime);                //waits delayTime milliseconds

 

digitalWrite(SolPins[4], LOW);  //Turns off Solenoid #4 (connected to pin 6 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[3], LOW);  //Turns off Solenoid #3 (connected to pin 5 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[2], LOW);  //Turns off Solenoid #2 (connected to pin 4 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[1], LOW);  //Turns off Solenoid #1 (connected to pin 3 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[0], LOW);  //Turns off Solenoid#0 (connected to pin 2)

delay(delayTime);                //waits delayTime milliseconds

}

 

 

 

 

 

 

//2nd Sequence (same as first but less typing)

 

void oneAfterAnotherLoop(){

int delayTime = thisDelay;

for(int i = 0; i <= 7; i++){

digitalWrite(SolPins[i], HIGH);

delay(delayTime);                //gets one added to it so this will repeat

}                                  //8 times the first time i will = 0 the final

//time i will equal 7;

 

//Turn Each Solenoid off one after another

for(int i = 7; i >= 0; i–){  //same as above but rather than starting at 0 and counting up

//we start at seven and count down

digitalWrite(SolPins[i], LOW);  //Turns off LED #i each time this runs i

delay(delayTime);                //gets one subtracted from it so this will repeat

}                                  //8 times the first time i will = 7 the final

//time it will equal 0

}

 

 

 

 

 

 

 

 

 

//3rd Sequence

 

 

void oneOnAtATime(){

int delayTime = thisDelay; //the time (in milliseconds) to pause between LEDs

//make smaller for quicker switching and larger for slower

for(int i = 0; i <= 7; i++){

int offSol = i – 1;  //Calculate which LED was turned on last time through

if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will

offSol = 7;        //turn on LED 2 and off LED 1)

}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)

//instead we turn off LED 7, (looping around)

digitalWrite(SolPins[i], HIGH);     //turn on LED #i

digitalWrite(SolPins[offSol], LOW); //turn off the LED we turned on last time

delay(delayTime);

}

}

 

 

 

 

 

 

/*

* inAndOut() – This will turn on the two middle Solenoids then the next two out

* making an in and out look

*/

void inAndOut(){

int delayTime = thisDelay; //the time (in milliseconds) to pause between LEDs

//make smaller for quicker switching and larger for slower

//runs the Solds out from the middle

for(int i = 0; i <= 3; i++){

int offSol = i – 1;  //Calculate which LED was turned on last time through

if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will

offSol = 3;        //turn on LED 2 and off LED 1)

}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)

//instead we turn off LED 7, (looping around)

int onSol1 = 3 – i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED

//#0 when i = 3

int onSol2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED

//#7 when i = 3

int offSol1 = 3 – offSol; //turns off the LED we turned on last time

int offSol2 = 4 + offSol; //turns off the LED we turned on last time

digitalWrite(SolPins[onSol1], HIGH);

digitalWrite(SolPins[onSol2], HIGH);

digitalWrite(SolPins[offSol1], LOW);

digitalWrite(SolPins[offSol2], LOW);

delay(delayTime);

}

 

//runs the LEDs into the middle

for(int i = 3; i >= 0; i–){

int offSol = i + 1;  //Calculate which LED was turned on last time through

if(i == 3) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will

offSol = 0;        //turn on LED 2 and off LED 1)

}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)

//instead we turn off LED 7, (looping around)

int onSol1 = 3 – i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED

//#0 when i = 3

int onSol2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED

//#7 when i = 3

int offSol1 = 3 – offSol; //turns off the LED we turned on last time

int offSol2 = 4 + offSol; //turns off the LED we turned on last time

digitalWrite(SolPins[onSol1], HIGH);

digitalWrite(SolPins[onSol2], HIGH);

digitalWrite(SolPins[offSol1], LOW);

digitalWrite(SolPins[offSol2], LOW);

delay(delayTime);

}

}

 

 

 

 

 

 

 

 

2 buttons one to control which sequence and one to trigger movements:

 

 

/*

patterns by pressing the button

*/

 

 

const int buttonPin1 = 7;

const int buttonPin2 = 8;// the number of the pushbutton pin

const int ledPin =  13;

 

int SolPins[] = {2,3,4,5,6};

int buttonState1 = 0;

int buttonState2 = 0;

int thisDelay = thisDelay;

int sequence = 1;

 

 

void setup()

{

Serial.begin(9600);

//Set each pin connected to an LED to output mode (pulling high (on) or low (off)

for(int i = 0; i < 5; i++){         //this is a loop and will repeat twice

pinMode(SolPins[i],OUTPUT);

pinMode(ledPin, OUTPUT);

pinMode(buttonPin1, INPUT);

pinMode(buttonPin2, INPUT);

 

 

}

}

 

/*

* loop() – this function will start after setup finishes and then repeat

* we call a function called oneAfterAnother(). if you would like a different behaviour

* uncomment (delete the two slashes) one of the other lines

*/

void loop()                     // run over and over again

{

int sensorReading = analogRead(A3);

int sensorReading1 = analogRead(A0);

int sensorReadingsound = sensorReading * sensorReading1 /100 ;

int sensorReadingDelay = sensorReading / sensorReading1;

buttonState1 = digitalRead(buttonPin1);

buttonState2 = digitalRead(buttonPin2);

if(buttonState2 == HIGH) {

if(sequence == 3) // if sequence is at 3 already, make it 1 again

{sequence = 1;

} else

{

sequence++; // otherwise go to the next sequence

}

delay(100);

}

 

if (buttonState1 == HIGH) {

digitalWrite(ledPin, HIGH);

int thisPitch = map(sensorReadingsound, 100, 10000, 10, 500);

tone(12, thisPitch, 10);

switch(sequence)

{

case 1:

oneAfterAnotherNoLoop();

break;

case 2:

oneOnAtATime();

break;

case 3:

inAndOut();

break;

}

oneAfterAnotherNoLoop();   //this will turn on each LED one by one then turn each off

//oneAfterAnotherLoop();   //does the same as oneAfterAnotherNoLoop but with

//much less typing

//oneOnAtATime();          //this will turn one LED on then turn the next one

//on turning the

//former off (one LED will look like it is scrolling

//along the line

//inAndOut();              //lights the two middle LEDs then moves them out then back

//in again

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

thisDelay = map(sensorReadingDelay * 100, 100, 1000, 100,1000);

Serial.println(sensorReadingDelay * 100);

delay(70);        // delay in between reads for stability

}

 

 

 

//1st sequence

 

void oneAfterAnotherNoLoop(){

int delayTime = thisDelay;

digitalWrite(SolPins[0], HIGH);  //Turns on Solenoid #0 (connected to pin 2 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[1], HIGH);  //Turns on Solenoid #1 (connected to pin 3 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[2], HIGH);  //Turns on Solenoid #2 (connected to pin 4 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[3], HIGH);  //Turns on Solenoid #3 (connected to pin 5 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[4], HIGH);  //Turns on Solenoid #4 (connected to pin 6 )

delay(delayTime);                //waits delayTime milliseconds

 

digitalWrite(SolPins[4], LOW);  //Turns off Solenoid #4 (connected to pin 6 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[3], LOW);  //Turns off Solenoid #3 (connected to pin 5 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[2], LOW);  //Turns off Solenoid #2 (connected to pin 4 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[1], LOW);  //Turns off Solenoid #1 (connected to pin 3 )

delay(delayTime);                //waits delayTime milliseconds

digitalWrite(SolPins[0], LOW);  //Turns off Solenoid#0 (connected to pin 2)

delay(delayTime);                //waits delayTime milliseconds

}

 

 

 

 

 

 

//2nd Sequence (same as first but less typing)

 

void oneAfterAnotherLoop(){

int delayTime = thisDelay;

for(int i = 0; i <= 7; i++){

digitalWrite(SolPins[i], HIGH);

delay(delayTime);                //gets one added to it so this will repeat

}                                  //8 times the first time i will = 0 the final

//time i will equal 7;

 

//Turn Each Solenoid off one after another

for(int i = 7; i >= 0; i–){  //same as above but rather than starting at 0 and counting up

//we start at seven and count down

digitalWrite(SolPins[i], LOW);  //Turns off LED #i each time this runs i

delay(delayTime);                //gets one subtracted from it so this will repeat

}                                  //8 times the first time i will = 7 the final

//time it will equal 0

}

 

 

 

 

 

 

 

 

 

//3rd Sequence

 

 

void oneOnAtATime(){

int delayTime = thisDelay; //the time (in milliseconds) to pause between LEDs

//make smaller for quicker switching and larger for slower

for(int i = 0; i <= 7; i++){

int offSol = i – 1;  //Calculate which LED was turned on last time through

if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will

offSol = 7;        //turn on LED 2 and off LED 1)

}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)

//instead we turn off LED 7, (looping around)

digitalWrite(SolPins[i], HIGH);     //turn on LED #i

digitalWrite(SolPins[offSol], LOW); //turn off the LED we turned on last time

delay(delayTime);

}

}

 

 

 

 

 

 

/*

* inAndOut() – This will turn on the two middle Solenoids then the next two out

* making an in and out look

*/

void inAndOut(){

int delayTime = thisDelay; //the time (in milliseconds) to pause between LEDs

//make smaller for quicker switching and larger for slower

//runs the Solds out from the middle

for(int i = 0; i <= 3; i++){

int offSol = i – 1;  //Calculate which LED was turned on last time through

if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will

offSol = 3;        //turn on LED 2 and off LED 1)

}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)

//instead we turn off LED 7, (looping around)

int onSol1 = 3 – i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED

//#0 when i = 3

int onSol2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED

//#7 when i = 3

int offSol1 = 3 – offSol; //turns off the LED we turned on last time

int offSol2 = 4 + offSol; //turns off the LED we turned on last time

digitalWrite(SolPins[onSol1], HIGH);

digitalWrite(SolPins[onSol2], HIGH);

digitalWrite(SolPins[offSol1], LOW);

digitalWrite(SolPins[offSol2], LOW);

delay(delayTime);

}

 

//runs the LEDs into the middle

for(int i = 3; i >= 0; i–){

int offSol = i + 1;  //Calculate which LED was turned on last time through

if(i == 3) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will

offSol = 0;        //turn on LED 2 and off LED 1)

}                    //however if i = 0 we don’t want to turn of led -1 (doesn’t exist)

//instead we turn off LED 7, (looping around)

int onSol1 = 3 – i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED

//#0 when i = 3

int onSol2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED

//#7 when i = 3

int offSol1 = 3 – offSol; //turns off the LED we turned on last time

int offSol2 = 4 + offSol; //turns off the LED we turned on last time

digitalWrite(SolPins[onSol1], HIGH);

digitalWrite(SolPins[onSol2], HIGH);

digitalWrite(SolPins[offSol1], LOW);

digitalWrite(SolPins[offSol2], LOW);

delay(delayTime);

}

}

 

 

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: