Advertisement
circuitdh

100w Flashlight v1.0

May 25th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. /*
  2.   Daniel Hoefer 5/25/2019
  3.   ---------------------
  4.   This is the code for the 100w flashlight Daniel built.
  5.   It uses two potentiometers to add a strobe mode to the light.
  6.   There are two modes in this version, as specified below
  7.   The potentiometers are wired to analog pins A6 and A7 (off and on)
  8.   The flaslight's transistor switch base is connected to digital pin 12.
  9.   There is a 200 ohm resistor linking these two terminals
  10.   The transistor's collector and emmitter are connected in series with
  11.     the flashlights dimmer circuit.
  12.   Questions? email circuitdh@gmail.com
  13.  
  14.   MODES:
  15.     1) Steady On
  16.     2) Flash
  17.   STATES:
  18.     0) On delay
  19.     1) Off delay
  20. */
  21.  
  22. //we define all the pin numbers as constant variables for easy access
  23. #define controlPin 12
  24. #define indicatorPin 13
  25. #define onPin 7
  26. #define offPin 6
  27. long onVal, offVal; //we define the variables that store the delay times (on and off)
  28. int onValRaw, offValRaw; //we define the variables that store the raw potentiometer values
  29. byte mode; //variable that holds the mode of the light
  30. boolean state = 0; //variable that holds the strobe state, either on or off (0 or 1)
  31. byte OFF = 1, ON = 0; //variables that hold the true values of the switch pin. see below
  32. //when the control pin is off, the flashlight is on and vise versa.
  33. //Ttese variables make this easier to understand
  34.  
  35. //this part of the code runs once
  36. void setup() {
  37.     //declares the main light and indicator light pins as an outputs
  38.     pinMode(controlPin, OUTPUT);
  39.     pinMode(indicatorPin, OUTPUT);
  40.     //declares the two potentiometer pins as inputs
  41.     pinMode(onPin, INPUT);
  42.     pinMode(offPin, INPUT);
  43.    
  44.     Serial.begin(9600);
  45. }
  46.  
  47. //this part of the code runs repeatedly
  48. void loop() {
  49.     //we read the values from the potentiometers and assign that to the raw data variables
  50.     onValRaw = analogRead(onPin);
  51.     offValRaw = analogRead(offPin);
  52.     //we convert each variable to a value between about 1 and 5200
  53.     //we are using exponents to make the turn of the knobs feel more logarithmic
  54.     onVal = pow(2, ((float)onValRaw / 82));
  55.     offVal = pow(2, ((float)offValRaw / 82));
  56.     //we see if the on time potentiometer is at the high extent of its turn
  57.     //if it is, we put the flashlight in mode 1, which is always on
  58.     //otherwise, we set it to mode 1, which is stobe mode
  59.     if(onVal > 5000) { mode = 1; } else { mode = 2; }
  60.     Serial.println(onVal);
  61.     Serial.println(mode);
  62.     //we make sure the value for off time is capped at 5 seconds (5000 ms)
  63.     if(offVal > 5000) { offVal = 5000; }
  64.    
  65.     //we enter the main part of the loop
  66.     switch(mode) { //we check what mode the light is in
  67.       case 1: //mode 1 is just on
  68.         digitalWrite(controlPin, ON); //we turn the light on
  69.         break; //finish this case
  70.        
  71.       case 2:  //mode 2 is strobe
  72.         if(state == 0) { //we check if the light is in the 0 state (last time it would be off)
  73.           digitalWrite(controlPin, ON); //if it is, we turn the light on
  74.           digitalWrite(indicatorPin, ON); //we also turn the indicator light off for an inverse effect
  75.           delay(onVal); //we delay the length of ontime calculated earlier (in milliseconds)
  76.           state = !state; //flip the state for the next stobe
  77.           return; //ends the loop to check if there was a mode change before moving on
  78.         }
  79.         if(state == 1) { //we check if the light is in the 1 state (last time it would be on)
  80.           digitalWrite(controlPin, OFF); //if it is, we turn the light off
  81.           digitalWrite(indicatorPin, OFF); //we also turn the indicator light on for an inverse effect
  82.           delay(offVal); //we delay the length of off time calculated earlier (in milliseconds)
  83.           state = !state; //flip the state for the next stobe
  84.           return; //ends the loop to check if there was a mode change before moving on
  85.         }
  86.         break; //finish this case
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement