taliafpf-code 2

/* This code operates a bottle opener with a photoresistor and a green LED on it
which lets the resistance differ enough between contrasting colors that you can tell
them apart based on how the resistance effects the voltage.  The bottle opener has
a current set up to run through the bottle cap and when the circuit is complete by opening
the bottle, the voltage going into the A2D is read and the color is determined.  Then based
on what the voltage is, it moves a servo either left or right to indicate which specified
beverage was opened. */

#include <Servo.h>

//Three time keepers
  long time = 0;
  long count = 0;
  int data = 0;
 
Servo myservo; //Creates the servo object
int pos = 90; //stores servo position
 


void setup() {
  //pins we used.
  myservo.attach(9); //attaches the servo on pin 9 to the servo object.
 
  pinMode(5, INPUT);  //voltage sensor
  pinMode(3, INPUT);  //Data in
  pinMode(2, OUTPUT);  //CS
  pinMode(4, OUTPUT); //I/O clock
 

}

//Main loop
void loop(){

  TimeKeeper();
  CS();
  if(count%4 == 0)
  {
    dataRead();
  }
  delay(10);

}

/*This part of the kode makes a 25 Hz clock.
We used mod functions to make our clocks because it
takes the least amount of code*/

void TimeKeeper(){
  if(time != millis())  // Incraments count if micros() changes
  {
    count = count + 1;
    time = millis();
  }
  if(count == 50000)  // 1 Second Interval
  {
    count = 0;
  }
 
  if(count%4 == 0 ||count%4 == 1)  // even = low
  {
    digitalWrite(4, LOW);
  }
  else if(count%4 == 2 || count%4 == 3)
  {
    digitalWrite(4, HIGH);   // odd = high
  }
}


/*This is the Chip select which has
to be up for 8 clock cycles to read the data.*/

void CS(){
 if(count%64 <= 29)  // even = low
  {
    digitalWrite(2, LOW);
  }
  else
  {
    digitalWrite(2, HIGH);   // odd = high
  }
}


/*  This reads our data and coverts
it form binary to decimal. */

void dataRead()
{
 
 
  if(count%64 == 0)
  {
    data = 0;
  }
  if(count%64==0)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 128;
    }
  }
  else if(count%64==4)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 64;
    }
  }
  else if(count%64==8)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 32;
    }
  }
  else if(count%64==12)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 16;
    }
  }
  else if(count%64==16)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 8;
    }
  }
  else if(count%64==20)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 4;
    }
  }
  else if(count%64==24)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 2;
    }
  }
  else if(count%64==28)
  {
    if (digitalRead(3) == HIGH)
    {
      data = data + 1;
    }
  }
  if(count%64 == 56)
  {
    lightUp();
    data = 0;

    
  }
    
}

void lightUp(){
  if(digitalRead(5) == HIGH){
    if(data > 170){
      myservo.write(100);
    }
    else{
     myservo.write(80);
    }
  }
  else{
      myservo.write(90);
    }
}