Tabs

Programming

Its difficult to document progress on coding, but suffice to say that i've finished tweaking everything, and now the code works properly! This is actually the final code that was downloaded onto the arduino.  The most complex thing about this code is the blinking time indicator light- since it blinks asynchronously with everything else, and arduino doesn't support multithreaded programming, i had to use an elaborate workaround....

//zap a mole code
//Sasha Anferov 03/06/12

//clamp values for acceptable inputs
int max = 800;
int min = 400;

void setup(){
  Serial.begin(9600);
  //set up variables--------------------
  //setup random
  randomSeed(analogRead(A3));
  //initialize all 8 address pins (D2-D9)
  for(int x = 2; x <= 9; x++){
    pinMode(x, OUTPUT);
  }
  //initialize extra in/out
  pinMode(10, OUTPUT);  //pin 10 = CLOCK
  pinMode(11, OUTPUT);  //pin 11 = DISPLAY RESET
  pinMode(12, OUTPUT);  //pin 12 = BIG LED
  pinMode(A0, INPUT);   //pin A0 = Board Input
  pinMode(A1, INPUT);   //pin A1 = RESET bttn
  pinMode(A2, OUTPUT);  //pin A2 = clock status light
  //RESET clock
  resetClock();

  click read more to see full code




  //-------------------------------------
  //start function
  while(analogRead(A1)<500){
    start();
  }
  //reset clock again
  resetClock();
}

void start(){
  //do crazy things and blink right here
  for(int i=6;i<=9;i++){
    for(int j=2;j<=5;j++){
      digitalWrite(i, HIGH);
      digitalWrite(j, HIGH);
      digitalWrite(10, HIGH);
      delay(5);
      digitalWrite(i, LOW);
      digitalWrite(j, LOW);
      digitalWrite(10, LOW);
    }
  }
  digitalWrite(12, HIGH);
  delay(5);
  digitalWrite(12, LOW);
  digitalWrite(A2, HIGH);
  delay(5);
  digitalWrite(A2, LOW);
  
}

void loop(){
  //intro sequence
  intro();
  //main game loop
  game();
  //game over sequence
  gameOver();
  //standby mode
  
  
}

void intro(){
  //give player time to get ready
  //reset clock
  resetClock();
  //L bracket slowly goes away
  int XLBracket[7] = {9, 9, 9, 9, 8, 7, 6};
  int YLBracket[7] = {5, 4, 3, 2, 2, 2, 2};
  
  for(int i=7; i>0; i--){  //each time remove one value from cycle
    for(int t=0; t<(60/i); t++){  //repeat this many times 
      for(int j=0; j<i; j++){  //itinerate through 
        flash(XLBracket[j],YLBracket[j],5);
      }
    }
  }
  delay(100);
  
}

void game(){
  // the actual game mechanic
  int timer = 11200;
  int counter = 0;
  timer = 11200;
  counter = 0;
  boolean on = true;
  digitalWrite(A2, HIGH);
  while(timer > 0){
    
    //choose random buttton
    int x = random(6,10);
    int y = random(2,6);
    if(x == 8 && y==5){  //if the button doesn't work, choose a new one
      y = random(2,5);
    }
    
    boolean hit = false;
    digitalWrite(x, HIGH);
    digitalWrite(y, HIGH);
    for(int i=0; i<500 && !hit && timer > 0; i++){  //------------------
      int sensorValue = analogRead(A0);
      if(x==7 && y==4){
          sensorValue-= 60;
        }
      //timers block:
      timer--;  //main timer decrement
      counter++; //counter (for LED) increment
      if( analogRead(A1)>500){
        timer = 0;
      }
      
      if(counter >= (timer*0.00387)){  //hard to explain...
        if(on){ // toggle the led
          digitalWrite(A2, LOW);
        }else{
          digitalWrite(A2, HIGH); 
        }
        counter = 0;
        on = !on;
      }
      
      
      //------------------
      if(sensorValue < max && sensorValue > min){
        hit = true;
        digitalWrite(12, HIGH);  //big led on
        digitalWrite(10, HIGH);  //clock up
      }
      //------------------
      delay(6);
    }                                    //-------------------
    hit = false;
    digitalWrite(x, LOW);
    digitalWrite(y, LOW);
    digitalWrite(12, LOW);  //big led off
    digitalWrite(10, LOW);  //clock
    
  }
  
}

void gameOver(){
  //game is over, let player know
  //choose random buttton
  int x = random(6,10);
  int y = random(2,6);
  if(x == 8 && y==5){  //if the button doesn't work, choose a new one
    y = random(2,5);
  }
  //flash 3 buttons- random board button, time button, big LED in sync
  while(analogRead(A1)<500){
    digitalWrite(x, HIGH);        //turn on lights
    digitalWrite(y, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(A2, HIGH);
    for(int i=0;i<100 && analogRead(A1)<500; i++){  //delay 100, but also wait for button
      delay(4);
    }
    
    digitalWrite(x, LOW);      //turn off lights
    digitalWrite(y, LOW);
    digitalWrite(12, LOW);
    digitalWrite(A2, LOW);
    for(int i=0;i<100 && analogRead(A1)<500; i++){  //delay 100, but also wait for button
      delay(4);
    }
  }
}



void resetClock(){
  digitalWrite(11, HIGH);
  delay(5);
  digitalWrite(11, LOW);
}

void flash(int x, int y, int time){
  digitalWrite(x, HIGH);
  digitalWrite(y, HIGH);
  delay(time);
  digitalWrite(x, LOW);
  digitalWrite(y, LOW);
      
}

No comments:

Post a Comment