//--------------MigaOne Cycling------------------------------ 
//Cycles the MigaOne, using analog position readings from pot 
//Supply desired voltage (suggested 12V) 
//Outputs number of cycles, time to reach end of stroke  
//Initial variable declarations 

int motorPin = 8;         
//motor control 
int potPin = 2;           
//analog input (wiper) 
int val = 0;              //serial reading 
int potBuffer = 10;       //pot reading buffer 
int reps = 0;             //cycles completed 
long startTime = 0;       //starting time value 
long time = 0;            //current time 
int dist = 150;           //desired travel distance (calibrated) 
int initPot = 0;          //initial pot value 
int thresh = 0;           //pot value threshold  

void setup() {   
  //Pin setup   
pinMode(motorPin,OUTPUT);   
pinMode(potPin,INPUT);  
  //Begin serial communication   
Serial.begin(9600); 
}  
void loop() 
{  
  //Initialization   
  initPot = analogRead(potPin);  
  thresh = initPot + dist;  
  startTime = millis();  
  
  //Power actuator   
  digitalWrite(motorPin,HIGH);  
  //Wait until threshold is reached   
    while (val < thresh) 
    {     
      val = analogRead(potPin);     
      time = millis() - startTime;   
    }  
  //Stop acuation   
  digitalWrite(motorPin,LOW);  
  //Wait until actuator reaches initial position (+ buffer)   

    while (val > (initPot + potBuffer)) 
    {     
      val = analogRead(potPin);   
    }  
  //Increment cycles       
  reps = reps + 1;    
  //Print reps completed and actuation time   
  Serial.print(reps);
  Serial.print(" ");
  Serial.println(time);  
} 


