Aaron_code

//written by Aaron Pittenger
//help provided from Arduino demo code

#include <Servo.h> //includes servo library
 
int xaxis = A3; // x-axis of the accelerometer
int yaxis = A2; // y-axis
//int top = 2; //top green led
//int left = 3; //left green led
//int bottom = 4; //bottom green led
//int right = 5; //right green led
int fail;
int red = 7; //red led
int x;
int y;
int randno;
int count=0;
//servo position for each direction
int bottomserv=40;
int leftserv=130;
int topserv=95;
int rightserv=70;
 
//thresholds for checking accel
int xhi=338;
int xlo=317;
int yhi=343;
int ylo=322;
 
Servo spin;//setup servo
 
void setup() {               
  // initialize the digital pin as an output.
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT); 
  pinMode(7, OUTPUT); 
  randomSeed(analogRead(0));//for a truly random number
  spin.attach(9);
 
 
}
 
void loop()
{
  randno=random(2,6);//generate random number
 
  digitalWrite(randno, HIGH);//lights green led based on random number
 
//spins servo to proper location
 if (randno==2)
  {spin.write(topserv);}
 
    if (randno==3)
  {spin.write(leftserv);}
 
    if (randno==4)
  {spin.write(bottomserv);}
 
    if (randno==5)
  {spin.write(rightserv);}
 
  delay(2000-(count*25)); //decreasing delay time
 
  x=analogRead(xaxis);//read accel for board position
  y=analogRead(yaxis);
 
 if (randno==2 && x<xhi)//check to see if board is in correct position
  {
fail=1;//if not sets fail flag
  }
 
  if (randno==3 && y<yhi)
  {
fail=1;
  }
 
  if (randno==4 && x>xlo)
  {
fail=1;
  }
 
  if (randno==5 && y>ylo)
  {
fail=1;
  }
 
 if (fail==1)//if fail flag is set readout score and light all green leds
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
  for (int flash=1; flash<=(count/2); flash++) //score readout 
 {delay(500);
  digitalWrite(red, HIGH);
  delay(500);
  digitalWrite(red, LOW);}
  count=-1;//resets count (1 will be added below)
  fail=0;//resets fail flag
  delay(2000);
digitalWrite(2, LOW);//resets all leds to low
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
 digitalWrite(randno, LOW);
  count=count++;  //add 1 to count (used to increase speed and calc score
  }