int sensorBottom, sensorTop; boolean switchTop = false; boolean switchBottom = false; boolean topDebug = false; int switchey = 1; int unSpoolCounter = 1; int upDown = 1; int LED1 = 11; //all on PWM pins int LED2 = 10; int LED3 = 9; int LED4 = 5; int LED_WHITE = 3; //four bright white leds controlled through one tip120 //these variables are in case a sensor doesn't do it's job unsigned long topToBottomInterval = 6000; //if it's taking longer than this, then something is up unsigned long bottomToTopInterval = 8000; // unsigned long previousMillis; int errorCounter = 0; //if this racks up too many points, the whole thing shuts down void setup() { //CW is up and CC is down pinMode (13, OUTPUT); //DPDT relay pinMode(12, OUTPUT); //SPST relay pinMode(LED1,OUTPUT); //4 12V LEDs through tip120s pinMode(LED2,OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); pinMode(LED_WHITE, OUTPUT); pinMode (7, OUTPUT); //DPDT relay for motorBottom pinMode (6, OUTPUT); //spst relay for motorBottom Serial.begin(9600); //debug //start by picking up //when everything is hi, //motor goes in clockwise //if red is in DPDT relay //and black is in 5v SPST relay digitalWrite(13, HIGH); digitalWrite(12, HIGH); //digitalWrite(LED_WHITE, LOW); } void loop() { //read the sensors sensorBottom = analogRead(0); sensorTop = analogRead(1); //get stuck in infinite if loops// //if top sensor is activated wait for 4 sec and then let down //if ((sensorTop > 40)&&(switchTop == false)||(topDebug==true)){ if ((sensorTop > 40)&&(switchTop == false)){ switchBottom=false; //flip switch switchTop=true; //topDebug=false; digitalWrite(12, LOW); for(int i = 0; i < 20; i++) { digitalWrite(LED_WHITE, HIGH); delay(100); digitalWrite(LED_WHITE,LOW); delay(100); } //delay(4000); //watch dog - start counting until sensorBottom is activated previousMillis=millis(); } if (switchTop == true) { //lower object until switch is hit digitalWrite(13, LOW); digitalWrite(12, HIGH); //watch dog - if it takes longer than 6 seconds to //flip bottom switch, flip it and add a point to error if(millis() > previousMillis + topToBottomInterval) { errorCounter++; //log an error sensorBottom=20; //flip the switch because it's not working previousMillis=millis(); } } //sensorBottom = analogRead(0); //lots of readings because sensor is finicky if ((sensorBottom > 10)&&(switchBottom == false)) { switchBottom=true; switchTop=false; //previousMillis=millis(); delay(210); //delay a bit because bottomSensor is too high -just changes 140-180 3/31//change to 210 4/25 digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(LED1, HIGH); delay(200); digitalWrite(LED2, LOW); digitalWrite(LED3, HIGH); delay(200); digitalWrite(LED4, LOW); digitalWrite(LED1, HIGH); delay(200); digitalWrite(LED1, LOW); digitalWrite(LED2, HIGH); delay(200); digitalWrite(LED3, LOW); //2) turn on all lights and spin motor delay(200); digitalWrite(LED1, HIGH); digitalWrite(LED2, HIGH); digitalWrite(LED3, HIGH); digitalWrite(LED4, HIGH); //reverse polarity of bottom motor to avoid string winding up if (switchey == 1) { digitalWrite(6, HIGH); digitalWrite(7, HIGH); } else { digitalWrite(6, HIGH); digitalWrite(7, LOW); switchey = 0; } switchey++; delay(6000); //3)turn everything off and start over again digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); digitalWrite(LED3, LOW); digitalWrite(LED4, LOW); digitalWrite(6, LOW); unSpoolCounter++; upDown++; if (unSpoolCounter > 10) { //if it has done so many rotations //pick it up a bit //previousMillis=millis(); digitalWrite(13, HIGH); digitalWrite(12, HIGH); delay(500); //and let it unravel for ten seconds while lights pulsate digitalWrite(12,LOW); pulsate(); unSpoolCounter = 0; } if (upDown > 15) { //previousMillis=millis(); upAnddown (); upAnddown (); upDown = 0; } } if (switchBottom == true) { //pick UP digitalWrite(13, HIGH); digitalWrite(12, HIGH); //if (millis() > previousMillis+bottomToTopInterval) { // topDebug=true; // errorCounter++; //} } //finicky sensors! keep reading them! sensorBottom = analogRead(0); sensorTop = analogRead(1); //if errorCounter gets more than 5 errors, just turn on an LED and let the machine chill if (errorCounter>5) { while(1) { digitalWrite(12, LOW); //turn off motor digitalWrite(LED1, HIGH); delay(500); digitalWrite(LED1,LOW); } } } /////////mysterious functions//////////////////////// /////(funcitons to unspool the diamond/////////////// /////and make it seem like the mechanism is breaking ///////////////////////////////////////////////////// void pulsate () { for (int i = 0; i < 7; i++) { // fade in from min to max in increments of 5 points: for(int fadeValue = 10 ; fadeValue <= 255; fadeValue +=5) { // sets the value (range from 0 to 255): analogWrite(LED1, fadeValue); analogWrite(LED2, fadeValue); analogWrite(LED3, fadeValue); analogWrite(LED4, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } // fade out from max to min in increments of 5 points: for(int fadeValue = 255 ; fadeValue >= 10; fadeValue -=5) { // sets the value (range from 0 to 255): analogWrite(LED1, fadeValue); analogWrite(LED2, fadeValue); analogWrite(LED3, fadeValue); analogWrite(LED4, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } } digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); digitalWrite(LED3, LOW); digitalWrite(LED4, LOW); } void upAnddown () { digitalWrite(13, HIGH); digitalWrite(12, HIGH); digitalWrite(LED1, HIGH); digitalWrite(LED2, HIGH); digitalWrite(LED3, HIGH); digitalWrite(LED4, HIGH); delay(2500); digitalWrite(13, LOW); digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); digitalWrite(LED3, LOW); digitalWrite(LED4, LOW); delay(1000); digitalWrite(13, HIGH); delay(2000); digitalWrite(13, LOW); digitalWrite(LED1, HIGH); digitalWrite(LED2, HIGH); digitalWrite(LED3, HIGH); digitalWrite(LED4, HIGH); delay(1500); digitalWrite(12,LOW); digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); digitalWrite(LED3, LOW); digitalWrite(LED4, LOW); delay(1000); }