/*For six targets with 0,1,2 on one shaft, and 3,4,5 on the other with 0 and 3 in the same position, 1 and 4 in the same postion and 2 and 5 in ths same position. */ #include int n;//first random number int m;//second random number int threshold=30; int targets=6; int score=0;//initialized to 0 int time=3000;//time to hit target once it pops up int TIME=60000;//total time for game==1 minute--and yes 60000 fits into an int int TIME_Played=0;//initialize time played Servo servo0;//servo names Servo servo1; Servo servo2; Servo servo3; Servo servo4; Servo servo5; int anodePin=4;//all led's use this pin for their long leg int cathodePin0=14;//A0 int cathodePin1=15;//A1 int cathodePin2=16;//A2 int cathodePin3=17;//A3 int cathodePin4=18;//A4 int cathodePin5=19;//A5 int delaysn=0; //starts all delays at 0 int delaysm=0; int tworandom(int i);//random funcition generator for second number int initialize(int servonum);//sets initial servo positons int moves(int servonum);//pops up servos int reads(int led);//read led int readsavg(int n, int led);//takes average of n readings for led int reset(int servonum);//initialize current servo, reset delay of current servo, assign a new serov to n or m int scores(int servonum);//score when you shoot (shooting aliens==++) int scored(int servonum);//score when you don't shoot (don't shoot humans==++) void setup() { //deactivates pullup resistors _SFR_IO8(0x35) |= 4; _SFR_IO8(0x35) |= (1<<4); randomSeed(analogRead(0));//select start point of random number chain before setting pin A0 to output--reading analog should allow for random start in chain--wiring led to there may get rid of random start--will just cause every game to be the same--will change for final design, but for this week it would not be a problem n=random(targets); //choose first random number m=tworandom(n); //choose second random number based off of the first pinMode(anodePin, OUTPUT); //set pins associated with led light sensors to outputs pinMode(cathodePin0, OUTPUT); pinMode(cathodePin1, OUTPUT); pinMode(cathodePin2, OUTPUT); pinMode(cathodePin3, OUTPUT); pinMode(cathodePin4, OUTPUT); pinMode(cathodePin5, OUTPUT); digitalWrite(anodePin, HIGH); digitalWrite(cathodePin0,LOW); digitalWrite(cathodePin1,LOW); digitalWrite(cathodePin2,LOW); digitalWrite(cathodePin3,LOW); digitalWrite(cathodePin4,LOW); digitalWrite(cathodePin5,LOW); servo0.attach(3); //use all of the pwms pins servo1.attach(5); servo2.attach(6); servo3.attach(9); servo4.attach(10); servo5.attach(11); servo0.write(90); //initialize servo positons servo1.write(90); servo2.write(90); servo3.write(90); servo4.write(90); servo5.write(90); Serial.begin(9600);//set up serial communication to monitor } void loop(){ Serial.print("n and m are "); Serial.print(n); Serial.println(m);//*/ moves(m);//pop up the first two servos moves(n); int number=3;//number of times led is read //all delays initialized at 0 or reset to 0 in "reset" delaysm=delaysm+(2*number*50);//each read delays 50 so n times 50 is full delay delaysn=delaysn+(2*number*50); //read sensors int i = readsavg(number, (n+14));//servo will be 0 to 5 but led is 14 to 19 int j = readsavg(number, (m+14)); /*Serial.print("i and j are"); Serial.print(i ); Serial.println(j);//*/ if((i0)){ //you hit target n Serial.println("You hit n"); scores(n); int sameasbeforen=n; while(n==sameasbeforen){ n=reset(n,m); } /*delaysn=0; initialize(n); n=tworandom(m);//*/ } if((j0)){ Serial.println("You hit m"); //you hit target m scores(m); /*delaysm=0; initialize(m); m=tworandom(n);*/ while(m==sameasbeforem){ m=reset(m,n); } } if((delaysn>time)&&(delaysm>time)){ //Serial.println("ran out of time 0"); /*delaysn=0; delaysm=0; initialize(m); initialize(n); n=random(targets); //choose first random number m=tworandom(n); //choose second random number based off of the first*/ int sameasbeforen=n; while(n==sameasbeforen){ n=reset(n,m); } int sameasbeforem=m; while(m==sameasbeforem){ m=reset(m,n); } } if(delaysn>time){ //Serial.println("ran out of time 1"); //you ran out of time for target n scored(n); /*delaysn=0; initialize(n); n=tworandom(m);//*/ int sameasbeforen=n; while(n==sameasbeforen){ n=reset(n,m); } } if(delaysm>time){ //Serial.println("ran out of time 2"); //you ran out of time for target m scored(m); /*delaysm=0; initialize(m); m=tworandom(n);*/ int sameasbeforem=m; while(m==sameasbeforem){ m=reset(m,n); } } TIME_Played=TIME_Played+(2*number*50); if(TIME_Played