Embedded Programming‎ > ‎Examples‎ > ‎

Terminal Velocity

Unhook the O-ring between the Turn Table and the pot before running this.


// Boolean values
#define TRUE 1
#define FALSE 0

// Hardware Pin Information
#define PHOTO_PIN 1
#define BUTTON 5
#define M1_PWM 3
#define M1_DIR 12

// Variables
#define PHOTO_THRESHOLD 100
boolean foundCargo, atCargo, startCheck;
unsigned long cargoCounter;

// Timer Variables
unsigned long startTime, stopTime;
double runTime;

void setup(){
  // Initialization and Setup
  Serial.begin(115200);
 
  pinMode (M1_PWM,OUTPUT); digitalWrite (M1_PWM,LOW);
  pinMode (M1_DIR,OUTPUT); digitalWrite (M1_DIR,LOW);
 
  analogWrite (M1_PWM, 0);
 
 
  // Start of Code
  Serial.println("Please press the General Button to start accelerating the Turn Table");
  Serial.println();
 
  // Pause until Button Press
  while(analogRead(BUTTON) != 0) {delay (10);}
 
  Serial.println("Please wait a while until the Turn Table has visually reached Terminal Velocity!");
  Serial.println();
 
  // Ramp up the Turn Table speed a bit
  RampMotor();
 
}

void loop() {
  // Initialize the variables
  foundCargo = FALSE;
  atCargo = FALSE;
  startCheck = FALSE;
  cargoCounter = 0;
 
 
  Serial.println("When ready, please press the General Button to start recording data, the test will run for 15 seconds.");
 
 
  // Pause until Button Press
  while (analogRead(BUTTON) != 0) {delay (10);}
 
  // Run until we see the first cargo and pass it
  while (!startCheck) {
    // Check Photo-sensor
    foundCargo = PhotoCheck();
   
    // Condition Check
    if (foundCargo && !atCargo) {
      atCargo = TRUE;
    }
    if (!foundCargo && atCargo) {
      atCargo = FALSE;
      startCheck = TRUE;
    }
  }
 
  //   Start recording data
  startTime = millis();
  stopTime = startTime; 
 
  while ( (stopTime-startTime) < 15000 ) {
    // Check Photo-sensor
    foundCargo = PhotoCheck();
   
    // Condition Check
    if (foundCargo && !atCargo) {
      atCargo = TRUE;
      cargoCounter++;
    }
    if (!foundCargo && atCargo) {
      atCargo = FALSE;
    }

    stopTime = millis();       
  }
 
  runTime = stopTime-startTime;
  runTime = runTime/1000;
 
  Serial.println("Test Done");
  Serial.print("Time (sec): ");
  Serial.println(runTime,3);
  Serial.print("Counter: ");
  Serial.println(cargoCounter,DEC);
  Serial.print("RPM: ");
  Serial.println(60 * cargoCounter / runTime , 3);
}

// Check Photo-sensor
boolean PhotoCheck() {
  if (analogRead(PHOTO_PIN) > PHOTO_THRESHOLD) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

// Ramp up the motor
void RampMotor() {
  digitalWrite(M1_DIR,HIGH);
  analogWrite(M1_PWM,abs(100));
  delay(1000);

  analogWrite(M1_PWM,abs(200));
  delay(1000);
 
  analogWrite(M1_PWM,abs(255));
}

Comments