Skip to content

Instantly share code, notes, and snippets.

@MelissaBruno
Created August 17, 2016 15:01
Show Gist options
  • Save MelissaBruno/d8e300a92307a4a0e0a2a06647b096ec to your computer and use it in GitHub Desktop.
Save MelissaBruno/d8e300a92307a4a0e0a2a06647b096ec to your computer and use it in GitHub Desktop.
/* Measures the speed of a ping pong ball using the distance
between two photoresistors and millis() */
int sensor1Value;
int sensor2Value;
double startTime;
double endTime;
double time;
double timeInSeconds;
double distance;
double speed;
boolean endFlag;
void setup() {
Serial.begin(9600);
distance = 6;
endFlag = false;
}
void loop() {
sensor1Value = analogRead(A0);
sensor2Value = analogRead(A1);
//Serial.println(sensor1Value); // Used for determining photoresistor thresholds
//Serial.println(sensor2Value);
if(sensor1Value > 800){ // If first photoresistor passes threshold, save startTime.
endFlag = false;
startTime = millis();
}
if(sensor2Value < 830 && endFlag == false){ // If second photoresistor passes threshold, save endTime.
endTime = millis();
time = endTime - startTime;
timeInSeconds = time/1000;
speed = distance/timeInSeconds;
Serial.print("Speed: ");
Serial.print(speed);
Serial.print(" cm/s");
Serial.println(" ");
endFlag = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment