Adnan Obissi - Code

Here's the text file

final.txt

#include <Servo.h>

 //Two servos

Servo myservo;

Servo myservo1;

unsigned long time;

unsigned long timetest;

 

//constants for servos

int val = 0;

int down = 255;

int up = 0;

int pos = 90;

int pos1 = 90;

//pin assignments for nodes

int In1 = 14;

int In2 = 15;

int In3 = 16;

int In4 = 17;

//Base values for nodes

int Base1 = 0;

int Base2 = 0;

int Base3 = 0;

int Base4 = 0;

//Voltage readings for nodes

int Out1 = 0;

int Out2 = 0;

int Out3 = 0;

int Out4 = 0;

//Difference in voltage between nodes

int dif1 = 0;

int dif2 = 0;

//int dif3 = 0;

//int dif4 = 0;

 

 

void setup()

{

  Serial.begin(9600);

  pinMode(In1, INPUT);

  pinMode(In2, INPUT);

  pinMode(In3, INPUT);

  pinMode(In4, INPUT);

  //Setting new base values

  Base1 = analogRead(In1);

  Base2 = analogRead(In2);

  Base3 = analogRead(In3);

  Base4 = analogRead(In4);

 

  myservo.attach(12);

  myservo1.attach(11);

  myservo.write(pos);

  myservo.write(pos1);

 

  //time = millis();  

}

 

void loop()

{

//Sets the output values of the nodes in relation to their base values (allows for

//values closer to zero)

  Out1 = analogRead(In1) - Base1;

  Serial.println(Out1);

 

  Out2 = analogRead(In2) - Base2;

  Serial.println(Out2);

 

  Out3 = analogRead(In3) - Base3;

  Serial.println(Out3);

 

  Out4 = analogRead(In4) - Base4;

  Serial.println(Out4);

  //Out1 and Out 2 are in series - so are Out3 and Out4

 dif1 = Out1 - Out2;

 dif2 = Out3 - Out4;

 //dif3 = Out3 - Base3;

 //dif4 = Out4 - Base4;

 

 Serial.println(dif1);

 Serial.println(dif2);

 //Serial.println(dif3);

 //Serial.println(dif4);

 

 //Checks to see the voltage difference between nodes

 //If the voltage for Out2 goes up, it's darker than Out1 and the servo turns away from it

  if (dif1 < -10)

  {

    if (pos < 180) 

     moveR();

 

    if (pos1 < 180)

     moveL1(); 

 

  }

 //Out1 is darker than Out2

  else if (dif1 > 10)

  {

    if (pos < 180) 

     moveR();

 

    if (pos1 > 0)

     moveR1();  

  }

  //Out4 is darker than Out3

  else if (dif2 < -10)

  {

    if (pos > 0) 

     moveL();

 

    if (pos1 > 0)

     moveR1();

  }

  ////Out3 is darker than Out4

  else if (dif2 > 10)

  {

    if (pos > 0) 

     moveL();

 

    if (pos1 < 180)

     moveL1();

  }

  timetest = millis() - time;

 

}

 //Moves the vertical axis left or right

void moveL()

{

    pos = pos + 1 ;                           

   myservo.write(pos);

 

    Serial.println("Moving Left");

    Serial.println("New position: " + (pos));

 

  return;

}

  //Moves the horizontal axis right or left

void moveR()

{

    pos = pos - 1 ;                            

    myservo.write(pos);            

    Serial.println("Moving Right");

    Serial.println("New position: "+ (pos));

 

 

 

  return;

}

 

void moveL1()

{

    pos1 = pos1 - 1 ;                           

   myservo1.write(pos1);

 

    Serial.println("Moving Left");

    Serial.println("New position: " + (pos1));

 

  return;

}

 

void moveR1()

{

    pos1 = pos1 + 1 ;                            

    myservo1.write(pos1);            

    Serial.println("Moving Right");

    Serial.println("New position: "+ (pos1));

 

 

  return;

}

 

 

void reset()

{

   Base1 = analogRead(In1);

   Base2 = analogRead(In2);

   Base3 = analogRead(In3);

   Base4 = analogRead(In4);

   time = millis();

}