Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit headlight changer --- 18 September 2019
- A button that toggles between high and low beams
- a light sensor of some kind that should automatically turn the lights off.
- 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.
- */
- // I changed to some more-descriptive variable names.
- int headlight_toggle_button_pin = 8;
- int led_1_normal_light_pin = 12; // This is the I/O pin to turn on the 'normal' lights.
- int led_2_high_light_pin = 13; // This is the I/O pin to turn on the 'high' lights.
- //int status = false; // ------------ "status" appears to be a reserved word (it already has a defined meaning somewhere in the IDE language reference)
- // so you can't use that as a variable name. That is why it appeared orange in the Arduino IDE.
- // I don't know what portion uses it, I can't find any info about it online--but it is orange for some (?) reason.
- // I am using the variable below instead.
- int headlight_power = 0; // zero = off and 1 = on. This variable only represents if the lights are on or off.
- int current_light_mode = 1; // 1 = normal, 2 = high. This variable only represents what level the lights should be, if they are on.
- // You need a separate variable for this setting in order to remember the previous mode after the lights are turned back on.
- int light_sensor_pin = A0;
- int current_sensorValue = 0; // This is for storing the curent value of the sensor.
- int sensorValue_darkness_threshold = 100; // --------- This value is just a guess (it is a low value, since the max value is 1023).
- // 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.
- void setup() {
- Serial.begin(9600);
- pinMode(headlight_toggle_button_pin, INPUT_PULLUP);
- pinMode(led_1_normal_light_pin, OUTPUT);
- pinMode(led_2_high_light_pin, OUTPUT);
- Serial.println("Exiting setup()");
- }
- void loop() {
- check_light_sensor();
- check_light_selector_button();
- change_light_modes();
- delay(1000);
- // Just for your information: LDR's react very slowly, compared to photodiodes or phototransistors.
- // Many photodiodes and phototransistors (even cheaper ones) can easily detect hundreds of thousands of pulses per second.
- // LDRs are generally much, much slower than that.
- // 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.
- // And they get slower as they get colder--at freezing temperatures, they may take over a half-second to change.
- }
- void check_light_sensor() {
- // This function checks the light sensor, and then sets the value of headlight_power.
- current_sensorValue = analogRead(light_sensor_pin);
- if (current_sensorValue <= sensorValue_darkness_threshold) { // If the sensor is at or below the darkness threshold,
- headlight_power = 1; // ------------------------------------- Then turn the headlights power on.
- }
- else {
- headlight_power = 0; // ------------------------------------- Otherwise, turn the headlights power off.
- }
- }
- void check_light_selector_button() {
- // This function checks to see if the button is pressed, but only if headlight_power is set to 1.
- // (I assumed here that pressing the button when the lights are off shouldn't do anything)
- if (headlight_power == 1) { // This part only works if the lights are currently on.
- 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.
- if (current_light_mode == 1) {
- current_light_mode = 2; // 2 = high light beamz
- }
- else {
- current_light_mode = 1; // 1 = normal light beamz
- }
- }
- }
- }
- void change_light_modes() {
- // This function changes the light pins depending on the values of headlight_power and current_light_mode.
- if (headlight_power == 1) { // If the lights are supposed to be on:
- if (current_light_mode == 1) { // 1 = normal light beamz
- // This part means to turn the normal lights on:
- digitalWrite(led_1_normal_light_pin, HIGH);
- digitalWrite(led_2_high_light_pin, LOW);
- }
- else { // if current_light_mode = 2 (high light beamz)
- // This part means to turn the high lights on:
- digitalWrite(led_1_normal_light_pin, LOW);
- digitalWrite(led_2_high_light_pin, HIGH);
- }
- }
- else { // if (headlight_power == 0)
- // Getting to this part means that the lights are supposed to be turned off.
- // So this part turns off both lights:
- digitalWrite(led_1_normal_light_pin, LOW);
- digitalWrite(led_2_high_light_pin, LOW);
- }
- }
- // ~~~~~~~ the end ~~~~~~~~~
Add Comment
Please, Sign In to add comment