Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Daniel Hoefer 5/25/2019
- ---------------------
- This is the code for the 100w flashlight Daniel built.
- It uses two potentiometers to add a strobe mode to the light.
- There are two modes in this version, as specified below
- The potentiometers are wired to analog pins A6 and A7 (off and on)
- The flaslight's transistor switch base is connected to digital pin 12.
- There is a 200 ohm resistor linking these two terminals
- The transistor's collector and emmitter are connected in series with
- the flashlights dimmer circuit.
- Questions? email circuitdh@gmail.com
- MODES:
- 1) Steady On
- 2) Flash
- STATES:
- 0) On delay
- 1) Off delay
- */
- //we define all the pin numbers as constant variables for easy access
- #define controlPin 12
- #define indicatorPin 13
- #define onPin 7
- #define offPin 6
- long onVal, offVal; //we define the variables that store the delay times (on and off)
- int onValRaw, offValRaw; //we define the variables that store the raw potentiometer values
- byte mode; //variable that holds the mode of the light
- boolean state = 0; //variable that holds the strobe state, either on or off (0 or 1)
- byte OFF = 1, ON = 0; //variables that hold the true values of the switch pin. see below
- //when the control pin is off, the flashlight is on and vise versa.
- //Ttese variables make this easier to understand
- //this part of the code runs once
- void setup() {
- //declares the main light and indicator light pins as an outputs
- pinMode(controlPin, OUTPUT);
- pinMode(indicatorPin, OUTPUT);
- //declares the two potentiometer pins as inputs
- pinMode(onPin, INPUT);
- pinMode(offPin, INPUT);
- Serial.begin(9600);
- }
- //this part of the code runs repeatedly
- void loop() {
- //we read the values from the potentiometers and assign that to the raw data variables
- onValRaw = analogRead(onPin);
- offValRaw = analogRead(offPin);
- //we convert each variable to a value between about 1 and 5200
- //we are using exponents to make the turn of the knobs feel more logarithmic
- onVal = pow(2, ((float)onValRaw / 82));
- offVal = pow(2, ((float)offValRaw / 82));
- //we see if the on time potentiometer is at the high extent of its turn
- //if it is, we put the flashlight in mode 1, which is always on
- //otherwise, we set it to mode 1, which is stobe mode
- if(onVal > 5000) { mode = 1; } else { mode = 2; }
- Serial.println(onVal);
- Serial.println(mode);
- //we make sure the value for off time is capped at 5 seconds (5000 ms)
- if(offVal > 5000) { offVal = 5000; }
- //we enter the main part of the loop
- switch(mode) { //we check what mode the light is in
- case 1: //mode 1 is just on
- digitalWrite(controlPin, ON); //we turn the light on
- break; //finish this case
- case 2: //mode 2 is strobe
- if(state == 0) { //we check if the light is in the 0 state (last time it would be off)
- digitalWrite(controlPin, ON); //if it is, we turn the light on
- digitalWrite(indicatorPin, ON); //we also turn the indicator light off for an inverse effect
- delay(onVal); //we delay the length of ontime calculated earlier (in milliseconds)
- state = !state; //flip the state for the next stobe
- return; //ends the loop to check if there was a mode change before moving on
- }
- if(state == 1) { //we check if the light is in the 1 state (last time it would be on)
- digitalWrite(controlPin, OFF); //if it is, we turn the light off
- digitalWrite(indicatorPin, OFF); //we also turn the indicator light on for an inverse effect
- delay(offVal); //we delay the length of off time calculated earlier (in milliseconds)
- state = !state; //flip the state for the next stobe
- return; //ends the loop to check if there was a mode change before moving on
- }
- break; //finish this case
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement