Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /*
  2. ReadAnalogVoltage
  3. Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  4. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
  5.  
  6. This example code is in the public domain.
  7. */
  8. const int buttonPin = 2;
  9. int buttonState = 0;
  10. int ammo_count = 100;
  11. unsigned long startTime = -1;
  12.  
  13. // the setup routine runs once when you press reset:
  14. void setup() {
  15. // initialize serial communication at 9600 bits per second:
  16. Serial.begin(9600);
  17. Serial.println(ammo_count);
  18. ammo_count --;
  19. pinMode(buttonPin, INPUT);
  20. }
  21.  
  22.  
  23. // the loop routine runs over and over again forever:
  24. void loop() {
  25.  
  26. checkIfSelectorSwitchPressed();
  27.  
  28. float a0Voltage;
  29. boolean bbDetectedA0 = false;
  30.  
  31. while(!bbDetectedA0) {
  32. a0Voltage = getSensorVoltage(A0);
  33. bbDetectedA0 = bbDetected(a0Voltage);
  34. }
  35.  
  36. startTime = micros();
  37. Serial.println(ammo_count);
  38. ammo_count--;
  39.  
  40. float a1Voltage;
  41. boolean bbDetectedA1 = false;
  42.  
  43. while(!bbDetectedA1 && (micros() - startTime) < 250000) {
  44. a1Voltage = getSensorVoltage(A1);
  45. bbDetectedA1 = bbDetected(a1Voltage);
  46. }
  47.  
  48. if(bbDetectedA1){
  49. unsigned long duration = micros() - startTime;
  50. Serial.print("bb traveling: ");
  51. float seconds = duration / 1000000.0;
  52. float inchesPerSecond = 2 / seconds;
  53. Serial.println(inchesPerSecond / 12);
  54. startTime = -1;
  55. } else {
  56. Serial.print("Did not detect BB at A1 sensor!");
  57. }
  58. }
  59.  
  60. void checkIfSelectorSwitchPressed() {
  61. int curButtonState = digitalRead(buttonPin);
  62. if(curButtonState == 1 && curButtonState != buttonState){
  63. Serial.println("toggled");
  64. buttonState = curButtonState;
  65. } else if(curButtonState == 0){
  66. buttonState = 0;
  67. }
  68. }
  69.  
  70. float getSensorVoltage(int pin) {
  71. int sensorValue = analogRead(pin);
  72. // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  73. return sensorValue * (5.0 / 1023.0);
  74. }
  75.  
  76. boolean bbDetected(float sensorVoltage) {
  77. if(sensorVoltage >= 2.25){
  78. return true;
  79. }
  80. return false;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement