/*Liquid Tree Project Code
*Author: JiaYi Lin 
*date: 5 Nov 2008
*Liquid Tree Project was created in the course Ambient Visualization with *Devices in Semester 2, 2008 at the Faculty of Architecture, Design 
*Science and Planning, University of Sydney.
*
*The code is based on the Stepper Unipolar advanced example on the 
*Arduino website http://www.arduino.cc/en/Tutorial/StepperUnipolar
*
*The highlight (Orange) parts were from the David Cuartielles’s Stepper Unipolar *Advanced
* -------------------------
* Stepper Unipolar Advanced*
* (cleft) 2005 DojoDave for K3
* http://www.0j0.org | http://arduino.berlios.de
* @author: David Cuartielles
* @date: 20 Oct. 2005
*/

 

 

int ledPin01 = 12; // choose the pin for the LED

int ledPin02 = 13;

int inPin01 = 3;   // choose the input pin (for a pushbutton)

int inPin02 = 2;   // choose the input pin (for a pushbutton02, motor Backward)

 

int motorPins[] = {8, 9, 10, 11};

int count = 0;

int count2 = 0;

int motorPins02[] = {4, 5, 6, 7};

 

int delayTime = 500;

int delayTime02 = 500;

 

int val_Switch = 0;     // variable for reading the pin status

int val_Switch_back = 0; 

int val = 0; //variable for reading the pot status

int val02 = 1;

 

void setup() {

   Serial.begin(9600);

 

  pinMode(ledPin01, OUTPUT);  // declare LED as output

  pinMode(ledPin02, OUTPUT);

  pinMode(inPin01, INPUT);    // declare pushbutton as input

  pinMode(inPin02, INPUT);

 

   for (count = 0; count < 4; count++) {

    pinMode(motorPins[count], OUTPUT);

  }

  for (count = 0; count < 4; count++) {

    pinMode(motorPins02[count], OUTPUT);

  }

 

}

 

void moveForward() {

  if ((count2 == 0) || (count2 == 1)) {

    count2 = 16;

  }

  count2>>=1;

  for (count = 3; count >= 0; count--) {

    digitalWrite(motorPins[count], count2>>count&0x01);

  }

  delay(delayTime);

}

 

void moveBackward() {

  if ((count2 == 0) || (count2 == 1)) {

    count2 = 16;

  }

  count2>>=1;

  for (count = 3; count >= 0; count--) {

    digitalWrite(motorPins[3 - count], count2>>count&0x01);

  }

  delay(delayTime);

}

 

void moveForward02() {

  if ((count2 == 0) || (count2 == 1)) {

    count2 = 16;

  }

  count2>>=1;

  for (count = 3; count >= 0; count--) {

    digitalWrite(motorPins02[count], count2>>count&0x01);

  }

  delay(delayTime02);

}

 

void moveBackward02() {

  if ((count2 == 0) || (count2 == 1)) {

    count2 = 16;

  }

  count2>>=1;

  for (count = 3; count >= 0; count--) {

    digitalWrite(motorPins02[3 - count], count2>>count&0x01);

  }

  delay(delayTime02);

}

 

void loop(){

   val_Switch = digitalRead(inPin01);  // read input value

   val_Switch_back = digitalRead(inPin02);

   val = analogRead(0);

   val02 = analogRead(1);

   

  if (val_Switch == HIGH) {         // check if the input is HIGH (button released)

    digitalWrite(ledPin01, LOW);  // turn LED OFF

    digitalWrite(ledPin02, HIGH);

   

    if (val_Switch_back == LOW) { 

    delayTime = 20;

    moveBackward();

    }

    else {

      delayTime = 60000UL;

    }

   

    if (val > 50) {

    // move faster the higher the value from the potentiometer

    delayTime = 2048 - 1024 * val / 512 + 1;

    moveForward();

    }

    else if (val< 50){

    delayTime = 60000UL;

  }

  }

   

  else {

    digitalWrite(ledPin01, HIGH);  // turn LED ON

    digitalWrite(ledPin02, LOW);

   

    if (val_Switch_back == LOW) { 

    delayTime02 = 20;

    moveBackward02();

    }

    else {

      delayTime02 = 60000UL;

    }

   

    if (val > 50){

    delayTime02 = 2048 - 1024 * val / 512 + 1;

    moveForward02();

     }

    else if (val< 50){

    delayTime02 = 60000UL;

  }

  }

}