-/* 
-This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
-It won't work with v1.x motor shields! Only for the v2's with built in PWM
-control
+/* door lock test
 
-For use with the Adafruit Motor Shield v2 
+This sketch tests basic functionality of an electric car door lock mechanisim
+using a v2 Adafruit motor shield.
+
+This is a test with a single mechanisim, and uses simple delay() calls to control
+timing.  When multiple devices are being used, the structure will need to change
+to use the current state, and timing information, to perform later operations in
+later passes through void loop().  Similar to the blink without delay example
+code.
+
+For use with the Adafruit Motor Shield v2
 ---->	http://www.adafruit.com/products/1438
 */
 

#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
unsigned long const pulseTime = 60;//ms
unsigned int const testSpeed = 255;// 0 .. 255
unsigned long const inTime = 150;// 2 seconds
unsigned long const outTime = 50;// 2 seconds

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);

  myMotor->run(RELEASE);
}

// For 'good' drum strikes, the mechanisim needs to accelerate the stick toward the drum,
// then 'release' it to travel the final distance without directly being 'pushed', so that
// the stick will 'bounce' off of the drum head.  As the stick comes back up, finish
// pulling it back up for the next strike.
void loop() {
  singleStrike();
  // The time before the next swing, which will need to be the beat spacing minus
  // the time needed to do a single strike and recovery
  delay(beatDelay);
}

void singleStrike() {
  Serial.println("strike");
  swingStick();
  // Wait for the bounce before pulling back
  delay(bounceTime);
  Serial.print("lift");
  recoverStick();
}

void swingStick() {
  myMotor->run(FORWARD);// swing the stick at the drum
  myMotor->setSpeed(strikeSpeed);
  // Time needed to get to the end of the travel for the lock mechanisim
  delay(travelTime);
  // Remove power to the motor while the stick bounces
  myMotor->run(RELEASE);
}

void recoverStick() {
  myMotor->run(BACKWARD);// bring the stick back to the starting position
  myMotor->setSpeed(strikeSpeed); // Currently same speed as the strike
  delay(travelTime); // and the same time to do the movement
  myMotor->run(RELEASE); // remove power
}
