/*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 <Servo.h>
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((i<threshold)&&(i>0)){
//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((j<threshold)&&(j>0)){
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<TIME){
Serial.print("Game over. Your score is");
Serial.print(score);
while(1){
}
}
}
//////////////////////
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
///////////////////////////////////////////////////////
int tworandom(int i){
//chooses second random number that is not the same as the fist or equal to that of its pair
int k=0;
k=random(targets);//not redundant--if i is not in the first position setting m to 0 at first would cause it to be 0 almost always
while(((k-3)==i)||(k==i)||((k+3)==i)){
k=random(targets);
}
return k;
}
int initialize(int servonum){
//set all servos to an inital 90 degrees. Allows for rotation in either direction of 90 degrees
if(servonum==0){
servo0.write(90);
}
if(servonum==1){
servo1.write(90);
}
if(servonum==2){
servo2.write(90);
}
if(servonum==3){
servo3.write(90);
}
if(servonum==4){
servo4.write(90);
}
if(servonum==5){
servo5.write(90);
}
delay(150);
}
int moves(int servonum){
//moves servo to the popped up positon--all move same way because we can just flip the servo
if(servonum==0){
servo0.write(0);
}
if(servonum==1){
servo1.write(0);
}
if(servonum==2){
servo2.write(0);
}
if(servonum==3){
servo3.write(0);
}
if(servonum==4){
servo4.write(0);
}
if(servonum==5){
servo5.write(0);
}
delay(150);
}
int reads(int led){
int val = 0;
int vcc=anodePin;
int ground=led;
//already emitting light
delay(50);
//switch potentials -- charge LED to -5V
digitalWrite(vcc, LOW);
digitalWrite(ground, HIGH);
//measure time for potential to equalize (for cathode to be LOW)
//switch pinmode
pinMode(ground, INPUT);
//measure time it takes for cathodePin to go to zero
//this value probably depends on the chip clock or something
while(digitalRead(ground) != 0){
delay(1);
val++;
}
pinMode(ground, OUTPUT);
digitalWrite(vcc, HIGH);
digitalWrite(ground, LOW);
//Serial.println(val, DEC);
return val;
}
int readsavg(int n, int led){
int val=0;
for(int i=0; i<n; i++){
val=val+reads(led);//read n times and sum
}
val=val/n;//divide by n for average
return val;
}
int reset(int servonum1, int servonum2){
if(servonum1==m){
initialize(m);//lower servo m
delaysm=0;//reset delay before setting new m
int j=tworandom(servonum2);//choose new m based on n
/*Serial.print("the new m is");
Serial.println(m);
Serial.print("the old n was");
Serial.println(servonum2);//*/
return j;
}
if(servonum1==n){
initialize(n);//lower servo n
delaysn=0;//reset delay before setting new n
int j=tworandom(servonum2);//choose new n based on m
/*Serial.print("the new n is");
Serial.println(n);
Serial.print("the old m was");
Serial.println(servonum2);//*/
return j;
}
}
int scores(int servonum){
//scores==score when shot
if((servonum==0)||(servonum==1)||(servonum==2)){
//servos 0, 1, 2 are humans
score--;//don't shoot humans
}
if((servonum==3)||(servonum==4)||(servonum==5)){
//servos 3, 4, 5 are aliens
score++;//do shoot aliens
}
}
int scored(int servonum){
//scored==scored when did not shoot
if((servonum==0)||(servonum==1)||(servonum==2)){
//servos 0, 1, 2 are humans
score++;//good you did not shoot the humans
}
if((servonum==3)||(servonum==4)||(servonum==5)){
//servos 3, 4, 5 are aliens
score--;//baka, you forgot to shoot the aliens--have you seen Independence Day?
}
}