#define TRUE  1
#define FALSE 0

// The next lines are for pin usage.
#define BUTTON  2 
#define MOTOR   9
#define BUZZER 11

// The next lines are used for the siren. 

#define MIN_FREQ   300.0 
#define MAX_TIME  2000
#define FREQ_STEP    0.15
#define DELAY_TIME   5

void setup() {
  pinMode(BUTTON, INPUT); // No pullup this time.
  pinMode(MOTOR, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  digitalWrite(MOTOR, LOW);
} // setup()

void loop() {
  double freq = MIN_FREQ;  
  static int running = FALSE; // Static so it remembers its value.
  static int time_since_last_push = 2000; // long enough 

  if (running) {
    for (int t = 0; t < 2*MAX_TIME; t += DELAY_TIME) {
      delay(DELAY_TIME);
      time_since_last_push += DELAY_TIME; 
      if (time_since_last_push > 500 && 
          digitalRead(BUTTON) == HIGH) {
        time_since_last_push = 0; 
        running = FALSE; 
        digitalWrite(MOTOR, LOW); // Stop motor. 
        noTone(BUZZER); // Stop siren.
        break; // Stop changing frequency. 
      } // if (digitalRead(BUTTON) == HIGH) */
      if (t < MAX_TIME) {
        freq += FREQ_STEP*DELAY_TIME; // Siren increases frequency.  
      } else {
        freq -= FREQ_STEP*DELAY_TIME; 
      } // if 
      tone(BUZZER, freq); 
    } // for
  } else { // Car is not running. 
/* If the car is not running, check to see if 
   the button is pressed.  If the button is pressed, 
   start the car running. */

    delay(DELAY_TIME); // For stability. 
    time_since_last_push += DELAY_TIME; 
    if (time_since_last_push > 500 && 
        digitalRead(BUTTON) == HIGH) {
      time_since_last_push = 0; 
      running = TRUE; 
      digitalWrite(MOTOR, HIGH); 
    } // if (digitalRead(BUTTON) == HIGH)
  } // if (running) 
  
  

} // loop()