skizziks_53

Reddit headlight changer v 1.0

Sep 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. /*
  2.   Reddit headlight changer --- 18 September 2019
  3.   A button that toggles between high and low beams
  4.   a light sensor of some kind that should automatically turn the lights off.
  5.  
  6.   I did not actually test-run this sketch because the LDR circuit would need to be defined in order to set a proper darkness threshold value for sensorValue_darkness_threshold.
  7. */
  8.  
  9. // I changed to some more-descriptive variable names.
  10. int headlight_toggle_button_pin = 8;
  11. int led_1_normal_light_pin = 12; // This is the I/O pin to turn on the 'normal' lights.
  12. int led_2_high_light_pin = 13; // This is the I/O pin to turn on the 'high' lights.
  13.  
  14. //int status = false; // ------------ "status" appears to be a reserved word (it already has a defined meaning somewhere in the IDE language reference)
  15. //                                    so you can't use that as a variable name. That is why it appeared orange in the Arduino IDE.
  16. //                                    I don't know what portion uses it, I can't find any info about it online--but it is orange for some (?) reason.
  17. // I am using the variable below instead.
  18. int headlight_power = 0; // zero = off and 1 = on. This variable only represents if the lights are on or off.
  19.  
  20. int current_light_mode = 1; // 1 = normal, 2 = high. This variable only represents what level the lights should be, if they are on.
  21. // You need a separate variable for this setting in order to remember the previous mode after the lights are turned back on.
  22.  
  23. int light_sensor_pin = A0;
  24. int current_sensorValue = 0; // This is for storing the curent value of the sensor.
  25. int sensorValue_darkness_threshold = 100; // --------- This value is just a guess (it is a low value, since the max value is 1023).
  26. // For this sketch, I am assuming that the light sensor is half of a voltage divider that would have a low voltage in the dark and a high voltage in the light.
  27.  
  28.  
  29. void setup() {
  30.   Serial.begin(9600);
  31.   pinMode(headlight_toggle_button_pin, INPUT_PULLUP);
  32.   pinMode(led_1_normal_light_pin, OUTPUT);
  33.   pinMode(led_2_high_light_pin, OUTPUT);
  34.   Serial.println("Exiting setup()");
  35. }
  36.  
  37.  
  38. void loop() {
  39.   check_light_sensor();
  40.   check_light_selector_button();
  41.   change_light_modes();
  42.  
  43.   delay(1000);
  44.   // Just for your information: LDR's react very slowly, compared to photodiodes or phototransistors.
  45.   // Many photodiodes and phototransistors (even cheaper ones) can easily detect hundreds of thousands of pulses per second.
  46.   // LDRs are generally much, much slower than that.
  47.   // At normal room temperatures (70F / 21C) many LDRs take at least 1/20th of a second to change their value, and some can take more than 1/10th of a second to change.
  48.   // And they get slower as they get colder--at freezing temperatures, they may take over a half-second to change.
  49. }
  50.  
  51.  
  52. void check_light_sensor() {
  53.   // This function checks the light sensor, and then sets the value of headlight_power.
  54.   current_sensorValue = analogRead(light_sensor_pin);
  55.   if (current_sensorValue <= sensorValue_darkness_threshold) { // If the sensor is at or below the darkness threshold,
  56.     headlight_power = 1; // ------------------------------------- Then turn the headlights power on.
  57.   }
  58.   else {
  59.     headlight_power = 0; // ------------------------------------- Otherwise, turn the headlights power off.
  60.   }
  61. }
  62.  
  63.  
  64. void check_light_selector_button() {
  65.   // This function checks to see if the button is pressed, but only if headlight_power is set to 1.
  66.   // (I assumed here that pressing the button when the lights are off shouldn't do anything)
  67.   if (headlight_power == 1) { // This part only works if the lights are currently on.
  68.     if (digitalRead(headlight_toggle_button_pin) == LOW) { // Since you declared the button pin as input_pullup, you need to check for the LOW state to see when it is pressed.
  69.       if (current_light_mode == 1) {
  70.         current_light_mode = 2; // 2 = high light beamz
  71.       }
  72.       else {
  73.         current_light_mode = 1; // 1 = normal light beamz
  74.       }
  75.     }
  76.   }
  77. }
  78.  
  79.  
  80. void change_light_modes() {
  81.   // This function changes the light pins depending on the values of headlight_power and current_light_mode.
  82.   if (headlight_power == 1) { // If the lights are supposed to be on:
  83.     if (current_light_mode == 1) { // 1 = normal light beamz
  84.       // This part means to turn the normal lights on:
  85.       digitalWrite(led_1_normal_light_pin, HIGH);
  86.       digitalWrite(led_2_high_light_pin, LOW);
  87.     }
  88.     else { // if current_light_mode = 2 (high light beamz)
  89.       // This part means to turn the high lights on:
  90.       digitalWrite(led_1_normal_light_pin, LOW);
  91.       digitalWrite(led_2_high_light_pin, HIGH);
  92.     }
  93.   }
  94.   else { // if (headlight_power == 0)
  95.     // Getting to this part means that the lights are supposed to be turned off.
  96.     // So this part turns off both lights:
  97.     digitalWrite(led_1_normal_light_pin, LOW);
  98.     digitalWrite(led_2_high_light_pin, LOW);
  99.   }
  100. }
  101.  
  102.  
  103.  
  104. // ~~~~~~~ the end ~~~~~~~~~
Add Comment
Please, Sign In to add comment