Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. /********************************************************
  2. * Autor: Martin Maskaľ
  3. * Názov programu: Merač reakčného času
  4. * Dátum: 15.11.2018
  5. * Verzia: 2.4
  6. *--------------------------------------------------------
  7. * Popis programu: Program na meranie reakčného času
  8. *********************************************************/
  9.  
  10. /**************************************
  11. * Konštanty
  12. **************************************/
  13. #define btnSTART 2 //pin pre tlacidlo Start
  14. #define btnHIT 3 //pin pre hracie tlacidlo
  15. #define ledTrigger 4 //pin pre LEDku
  16.  
  17. /**************************************
  18. * Premenne
  19. **************************************/
  20. // Inicializacia premennych do ktorych sa uklada cas reakcie a celkovy cas reakcii
  21. float timeReaction; // reakcny cas jedneho pokusu
  22. float timeTotal = 0; // celkovy rekacny cas vsetkych pokusov
  23.  
  24. /**************************************
  25. * Podprogramy
  26. **************************************/
  27. void Round()
  28. {
  29. // Zacatie testovacieho kola
  30. for (int i = 0; i < 5; i++)
  31. {
  32. delay(random(700, 5000)); // pocka vygenerovany nahodny cas od 0,7 do 5 sekund
  33. timeReaction = millis(); // do premennej timeReaction sa ulozi cas millis()
  34.  
  35. digitalWrite(ledTrigger, LOW); // zapne sa LEDka
  36.  
  37. // Pocka kym sa nestlaci hracie tlacidlo
  38. while (digitalRead(btnHIT)) {}
  39.  
  40. timeReaction = millis() - timeReaction; // od millis sa odpocita timeReaction cim sa vypocita cas reakcie
  41. timeTotal += timeReaction; // k celkovemu casu sa pripocita reakcny cas jedneho pokusu
  42. delay(10);
  43.  
  44. while (!digitalRead(btnHIT)) {} // pocka kym sa neuvolni hracie tlacidlo
  45. digitalWrite(ledTrigger, HIGH); // LEDka zhasne
  46. Serial.print(i + 1); // "i" ide od nuly ale pokusy zacinaju od 1
  47. Serial.print(": ");
  48. Serial.print(timeReaction);
  49. Serial.println(" msec");
  50. delay(1000);
  51. }
  52. }
  53.  
  54. void ReadyForStartRound()
  55. {
  56. // zapnutie LEDky
  57. digitalWrite(ledTrigger, LOW);
  58.  
  59. timeTotal = 0; // vynulovanie celkoveho casu na zaciatku programu
  60.  
  61. Serial.println("Press START button when you're ready");
  62.  
  63. // Cakanie na stlacenie tlacidla Start
  64. while (digitalRead(btnSTART)) {}
  65.  
  66. // Vypnutie LEDky
  67. digitalWrite(ledTrigger, HIGH);
  68.  
  69. // Cakanie kym sa uvolni tlacidlo Start
  70. delay(10);
  71. while (!digitalRead(btnSTART)) {}
  72. Serial.println();
  73. Serial.println("Prepare yourself");
  74. Serial.println();
  75. delay(3000); // pocka 3 sekundy
  76. Serial.println("GO!");
  77. delay(1000); // pocka sekundu
  78. }
  79.  
  80. void EndOfRound()
  81. {
  82. Serial.println();
  83. Serial.print("Avg = ");
  84. Serial.print(timeTotal / 5); // vypocitanie priemeru reakcnych casov
  85. Serial.println(" msec");
  86. Serial.println();
  87. }
  88.  
  89.  
  90. /**************************************
  91. * Hlavný program
  92. **************************************/
  93. void setup() {
  94. // inicializacia tlacidiel a LEDky
  95. pinMode(btnSTART, INPUT_PULLUP);
  96. pinMode(btnHIT, INPUT_PULLUP);
  97. pinMode(ledTrigger, OUTPUT);
  98.  
  99. // Zasvietenie LEDky
  100. digitalWrite(ledTrigger, LOW);
  101.  
  102. // inicialiazacia Serioveho monitora
  103. Serial.begin(9600);
  104. Serial.println("Arduino Reaction Timer");
  105. Serial.println();
  106. // Generator nahodnych cisiel
  107. randomSeed(analogRead(0));
  108. }
  109.  
  110.  
  111. void loop()
  112. {
  113. ReadyForStartRound();
  114.  
  115. Round();
  116.  
  117. EndOfRound();
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement