Geek dad for Mad Scientists Dodge a laser experiment code

Ok, so my daughter and I were working on this dodge a laser experiment from the geek dads book for aspiring Mad Scientists. We got all the parts from Radio Shack including an Arduino Uno and put them together following their instructions (wishing they had instructed us to use a breadboard, but following instructions none-the-less.)

What I did is to make the code compile and add a bit for troubleshooting the sensor readings.
This is not the most elegant code I have written, but I thought it would be helpgul for the next guy who comes along and wants to get their project working.

Please note that in the loop function there are several funtions commented out. For testing you will want to just uncomment the one you want to use. Then save (ctrl + s), compile (ctrl + r), upload (ctrl + u), then monitor it (ctrl + shift + m)

The code included with the book which will not even compile because of a missing function is included here for Google searching and for reference:

================================================================
============ This code did not work for me ========================

const int ready_light = 2;
const int fire_light = 3;
const int lazzor = 4;
const int fire_switch = 7;
const int sensor_in=0;
int seed_timer = 0;
long sensor_val = 0;


void setup(){
  pinMode(fire_switch,INPUT);
  pinMode(ready_light,OUTPUT);  
  pinMode(fire_light,OUTPUT);
  pinMode(lazzor,OUTPUT);
  Serial.begin(9600);
}

void loop (){
 //check_alignment();
  //read_sensor();
fire_the_lazzor();
}

long read_sensor(){
  int base_line = 0;
  int trip_point = 3 * base_line;
  sensor_val = analogRead(sensor_in);
  if (sensor_val > 500){
    digitalWrite(ready_light,HIGH);
    return sensor_val;
  }
  else{
    
    digitalWrite(ready_light,LOW);
    return 0;
  }
  
}

void check_alignment (){
  int count =0;
  long sensor_read = 0;
  for (int i=0; i < 4; i++){
    digitalWrite(ready_light, HIGH);
    delay(250);
    digitalWrite(ready_light,LOW);
    delay(250);
  }
  Serial.println("Calibrating Laser Please wait");
  digitalWrite (lazzor,HIGH);
  while (count < 10){
    sensor_read = read_sensor();
   Serial.println("Adjust Lazzor until Ready Light turns on"); 
    if(sensor_read > 300){
      Serial.print("Sensor Reading ");
      Serial.println(sensor_read);
      count++;
      delay(100);
    }
    else{
      count=0;
    }
  }
  Serial.println("Lazzor is aligned ... commence firing");
  //return 1;
}
    
  

void fire_the_lazzor(){
  long random_seed=0;
  long delay_time=0;
  static unsigned long startTime = 0;
  static unsigned long reactionTime = 0;
  digitalWrite(fire_light,LOW);
   digitalWrite(lazzor,LOW);
  Serial.println("Press The Fire Button");
  Serial.println("When the Red Light goes on the lazzor will fire ....");
  random_seed=switchTime();
  if (random_seed > 0){    
    delay_time = random(random_seed);
    delay(delay_time);
    digitalWrite(fire_light,HIGH);
    digitalWrite(lazzor,HIGH);
    //delay(500);
    sensor_val = analogRead(sensor_in);
  }
  while(sensor_val < 500){
    digitalWrite(lazzor,HIGH);
  }
  reactionTime = millis()-startTime;
  Serial.print("Reactioj Time is ...");
  Serial.println(reactionTime);
  Serial.println("Hmmm took longer than I expected ... looks like you got burned");   
}


================================================
============  Working Code follows ================
================================================

const int ready_light = 3;
const int fire_light = 4;
const int lazzor = 5;
const int fire_switch = 7;
const int sensor_in=0;
int seed_timer = 0;
long sensor_val = 0;

void setup(){
  pinMode(fire_switch,INPUT);
  pinMode(ready_light,OUTPUT);  
  pinMode(fire_light,OUTPUT);
  pinMode(lazzor,OUTPUT);
  Serial.begin(9600);
}

void loop (){
  //check_alignment();
  //read_sensor();
  //read_switch();
  fire_the_lazzor();
  //switchTime();
}

long read_sensor(){
  sensor_val = analogRead(sensor_in);
  if (sensor_val > 700){
    digitalWrite(ready_light,HIGH);
    return sensor_val;
  }
  else{
    digitalWrite(ready_light,LOW);
    return 0;
  }
  
}

void check_alignment (){
  int count =0;
  long sensor_read = 0;
  for (int i=0; i < 4; i++){
    digitalWrite(ready_light, HIGH);
    delay(250);
    digitalWrite(ready_light,LOW);
    delay(250);
  }
  Serial.println("Calibrating Laser Please wait");
  digitalWrite (lazzor,HIGH);
  while (count < 10){
    sensor_read = read_sensor();
   Serial.println("Adjust Lazzor until Ready Light turns on"); 
    if(sensor_read > 600){
      Serial.print("Sensor Reading ");
      Serial.println(sensor_read);
      count++;
      delay(100);
    }
    else{
      count=0;
    }
  }
  Serial.println("Lazzor is aligned ... commence firing");
  //return 1;
}
    
  

void fire_the_lazzor(){
  long random_seed=0;
  long delay_time=0;
  static unsigned long startTime = 0;
  static unsigned long reactionTime = 0;
  digitalWrite(fire_light,LOW);
   digitalWrite(lazzor,LOW);
  Serial.println("Press The Fire Button");
  Serial.println("When the Red Light goes on the lazzor will fire ....");
  
  while(switchTime() < 100){
  random_seed=switchTime();
  }
  
  if (random_seed > 0){    
    delay_time = random(random_seed);
    delay(delay_time*20);
    digitalWrite(fire_light,HIGH);
    digitalWrite(lazzor,HIGH);
    startTime = millis();
    //delay(500);
    sensor_val = analogRead(sensor_in);
  }
  while(sensor_val < 700){
    digitalWrite(lazzor,HIGH);
    sensor_val = analogRead(sensor_in);
  }
  reactionTime = millis()-startTime;
  Serial.print("Reaction Time is ...");
  Serial.println(reactionTime);
  Serial.println("Hmmm took longer than I expected ... looks like you got burned");   
}

long switchTime()
{
  static unsigned long startTime = 0;	   // the time the switch state change was first detected
  static boolean state = digitalRead(fire_switch);		  // the current state of the switch
  
  if(digitalRead(fire_switch) != state) // check to see if the switch has changed state
  {
    state = ! state;	 //yes,  invert the state
    startTime = millis();  // store the time
  }
  if( state == LOW)
    return millis() - startTime;   // return the time in milliseconds
  else
    return 0; // return 0 if the switch is not pushed (in the LOW state);
} 

int read_switch(){
if (digitalRead(fire_switch) == HIGH)
Serial.println('H');
else
Serial.println('L');
delay(1000);
}

Moderator edit: Code tags added.