skizziks_53

Reddit dual-sensor timer v1.0

May 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.93 KB | None | 0 0
  1. /*
  2.    Reddit speed tracker - V 1.0
  3.    This is a sketch that uses two photodetectors to act as a timer for something (?)
  4.  
  5.    Board: not specified
  6.    Hardware: two photo sensors of some kind, (???) not LDRs I would hope....
  7.  
  8.    Just so you know: you need to use a phototransistor or a photodiode for optical timing sensors, such as  BPW34.
  9.    LDRs (Light-Dependent Resistors) tend to have a relatively long activation time that can be as long as 1/10th of a second. They react too slow for many timing uses.
  10.    Phototransistors and photodiodes can turn on and off much faster; many are capable of doing so up into the megahertz range.
  11.    (-a regular LED can be used as a photodetector too, if that's all you have or you are cheap. When used this way, they turn on and off very fast also-)
  12. */
  13.  
  14.  
  15. //int lightPin = 0; //pin for Photoresistor ---------------------- you used the digital pins (plain numbers) and you need to use the analog pins.
  16. //int lightPin1 = 1; //pin for photoresistor #2
  17. // ,,,,,,,, Also if you use Serial.print() statements, then you cannot use digital pins zero and 1 for anything, because those pins are the same as the USB plug data pins.
  18.  
  19. int light_sensor_pin_A0 = A0; //pin for Photoresistor // ---------------- note the different pin number --> begins with A
  20. int light_sensor_pin_A1 = A1; //pin for photoresistor #2 // ---------------- note the different pin number --> begins with A
  21.  
  22. // int TranslateAnalog = 0; --------------------- these are declared okay but the names are ambiguous.
  23. // int TranslateAnalog1 = 1;
  24. int analog_pin_value_A0 = 0; // -------------------- these pin numbers tell you what they represent.
  25. int analog_pin_value_A1 = 0;
  26.  
  27.  
  28. // There is two places where the light sensor value is checked to be above a minimum value.
  29. // The two variables below just provide named values for each.
  30. int light_sensor_A0_threshold_value = 650;
  31. int light_sensor_A1_threshold_value = 650;
  32.  
  33. // unsigned long time; ---------------------------- this is declared correctly but the name is ambiguous
  34. // int time1 = time; ------------------------------ these are not declared properly; they should also be unsigned longs since you are trying to store the millis() time value in them.
  35. // int time2 = time;
  36.  
  37. // Below is two variables declared for storing both the beginning and ending times in.
  38. unsigned long sensor_begin_time = 0;
  39. unsigned long sensor_end_time = 0;
  40.  
  41. // int formula = 1.5 / ((time2 - time1) / 1000); ------------- There is no reason to put a formula here and use it to assign a value to [formula] because it has no values to use.
  42. float speed_formula_result = 0; // -------- I am declaring this as a float since the end result isn't likely to be a round number...
  43.  
  44. int led_output_pin = 13; // ------------------------------ I declared your output pin as #13, because it has a LED on it already. You can change the pin number as you wish, if you have some other output to use.
  45. // It is good practice to ALWAYS name your pins with descriptive names and then use the pin names, not the numbers.
  46.  
  47.  
  48. // Below is an added variable.
  49. // You need some way to tell within the main loop how to do these multiple steps in order, een though they may not occur in the same pass through the loop.
  50. // There are multiple steps to have the program work correctly, and so the program needs a way to "remember" what step it is on.
  51. int timer_state = 0; // This is a variable to tell what state the timer is in.
  52. // Zero = neither sensor triggered. -------- The first sensor is being checked in case it is above the minimum set level light_sensor_A0_threshold_value, and the second sensor is being ignored.
  53. // 1 = sensor A0 has been triggered. ------- Now sensor A0 will be ignored, and the second sensor will be checked until it is above the minimum set level in light_sensor_A1_threshold_value.
  54. // 2 = sensor A1 has been triggered. ------- Both sensors have been triggered, and both are being ignored. The end message is shown.
  55. // 3 = the end display is showing. --------- This is a time delay at the end for the whole thing to reset.
  56. // 4 = reset back to the normal zero state after the end display timer has run out.
  57.  
  58. int end_display_delay = 10; // This is an end display delay, with the delay value stated in seconds. After the second photo sensor is triggered, (with this set to 10) there will be a ten-second delay for the timer to reset.
  59. int end_display_seconds_counter = 0; // This will be used to count up to the value in end_display_delay.
  60. // The two variables below are for a one-second coded timer, to help with providing the end delay.
  61. unsigned long one_second_timer_begin_time = 0;
  62. unsigned long one_second_timer_current_time = 0;
  63.  
  64.  
  65.  
  66. void setup() {
  67.  
  68.   Serial.begin(9600); //Start serial communication
  69.   // pinMode (7, OUTPUT); ------------------------------------ not needed now
  70.   pinMode (led_output_pin, OUTPUT);
  71.   digitalWrite(led_output_pin, LOW);
  72.   Serial.println("Exiting setup()"); // ------------------------ This just prints a serial message that says when it is exiting the setup() function.
  73.  
  74. }
  75.  
  76.  
  77. void loop() {
  78.  
  79.   switch (timer_state) {
  80.  
  81.     case 0:
  82.       // Zero = neither sensor triggered.
  83.       perform_state_0();
  84.       break;
  85.     case 1:
  86.       // 1 = sensor A0 has been triggered.
  87.       perform_state_1();
  88.       break;
  89.     case 2:
  90.       // 2 = sensor A1 has been triggered.
  91.       perform_state_2();
  92.       break;
  93.     case 3:
  94.       // 3 = the end display is showing.
  95.       perform_state_3();
  96.       break;
  97.     case 4:
  98.       // 4 = reset back to the normal zero state after the end display timer has run out.
  99.       perform_state_4();
  100.       break;
  101.   }
  102.  
  103. } // end of main loop()
  104.  
  105.  
  106. void perform_state_0() {
  107.   analog_pin_value_A0 =  analogRead(light_sensor_pin_A0);
  108.   if (analog_pin_value_A0 > 650) {
  109.     digitalWrite(led_output_pin, HIGH);
  110.     sensor_begin_time = millis();
  111.     Serial.println("Time1 captured"); // --------------- You may want to comment this out for actual use.
  112.     // ------------------------------ It is not a good idea to place Serial.print() statements within timing code that must run fast, because the Serial.print() statement takes some time of its own to be executed.
  113.     //                                The set baud rate is bits per second, and each char that you print is 8 bits...
  114.     timer_state = 1; // Advance the timer_state to the next step.
  115.   }
  116. }
  117.  
  118. void perform_state_1() {
  119.   analog_pin_value_A1 =  analogRead(light_sensor_pin_A1);
  120.   if (analog_pin_value_A1 > 650) {
  121.     digitalWrite(led_output_pin, LOW);
  122.     sensor_end_time = millis();
  123.     Serial.println("Time2 captured"); // --------------- You may want to comment this out for actual use.
  124.     timer_state = 2; // Advance the timer_state to the next step.
  125.   }
  126. }
  127.  
  128. void perform_state_2() {
  129.   Serial.print("Sensor A0 value: ");
  130.   Serial.println(analog_pin_value_A0);
  131.   Serial.print("Time1: ");
  132.   Serial.println(sensor_begin_time);
  133.  
  134.   Serial.print("Sensor A1 value: ");
  135.   Serial.println(analog_pin_value_A1);
  136.   Serial.print("Time2: ");
  137.   Serial.println(sensor_end_time);
  138.  
  139.   Serial.print("Speed formula result = ");
  140.   // speed_formula_result = 1.5 / ((sensor_end_time - sensor_begin_time) / 1000); ------------- this formula is yielding an inf result
  141.   speed_formula_result = 1.5 * (sensor_end_time - sensor_begin_time); // this formula is for testing purposes only...
  142.   Serial.println(speed_formula_result);
  143.  
  144.   Serial.println("Starting end delay timer,,, (wait for reset)");
  145.   start_end_delay_timer();
  146.   timer_state = 3;
  147. }
  148.  
  149. void perform_state_3() {
  150.   check_end_delay_timer();
  151.   // The above function changes timer_state to 4 when necessary.
  152. }
  153.  
  154. void perform_state_4() {
  155.   // This state is just provided to show when the end display timer was resetting.
  156.   Serial.println("End-delay timer has reset.");
  157.   digitalWrite(led_output_pin, LOW); // Turn this LED off again.
  158.   timer_state = 0;
  159. }
  160.  
  161.  
  162.  
  163. void start_end_delay_timer() {
  164.   // This function is called to start the end-delay timer running.
  165.   end_display_seconds_counter = 0;
  166.   one_second_timer_begin_time = millis();
  167. }
  168.  
  169.  
  170. void check_end_delay_timer() {
  171.   // If the end-delay timer is running, this is called on every pass through the loop to check if enough time has passed to move on to the next stage.
  172.   one_second_timer_current_time = millis();
  173.   if (one_second_timer_current_time >= one_second_timer_begin_time) {
  174.     if (one_second_timer_current_time >= (one_second_timer_begin_time + 1000)) {
  175.       end_display_seconds_counter += 1;
  176.       if (end_display_seconds_counter == end_display_delay) {
  177.         timer_state = 4;
  178.       }
  179.       else {
  180.         one_second_timer_begin_time = millis();
  181.       }
  182.     }
  183.     // The code below blinks the pin #13 LED in short pulses once per second.
  184.     if ((one_second_timer_current_time - one_second_timer_begin_time) < 51) {
  185.       digitalWrite(led_output_pin, HIGH);
  186.     }
  187.     else {
  188.       digitalWrite(led_output_pin, LOW);
  189.     }
  190.   }
  191.   else {
  192.     one_second_timer_begin_time = millis(); // This is for the millis() rollover condition.
  193.   }
  194. }
Add Comment
Please, Sign In to add comment