Wattsup1234

Darkroom timer

Nov 13th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1.  
  2. // Rewritten for the TM1638 without rotary encoder by Gavin Lyons.
  3. // Based on Monkito darkroom f-stop timer software written and © by Elia Cottier//
  4. #include <TM1638plus.h>
  5. #include <math.h> //Math library for "round" function
  6. #include <EEPROM.h> //EEPROM library to save set-up values
  7.  
  8. // GPIO I/O pins on the Arduino or ESP 12 Relay
  9. //pick on any I/O you want.
  10. #if defined(__AVR__)
  11. // Arduino
  12. #define STROBE_TM 10
  13. #define CLOCK_TM 9
  14. #define DIO_TM 11
  15. #define TONE_PIN 12 //buzzer pin
  16. #elif defined(ESP8266)
  17. // ESP8266 specific code here
  18. #define STROBE_TM 14
  19. #define CLOCK_TM 13
  20. #define DIO_TM 12
  21. #define TONE_PIN 15 //buzzer pin
  22. #endif
  23.  
  24. bool high_freq = false; //default false Arduino Uno,, If using a high freq CPU > ~100 MHZ set to true, i.e ESP32
  25. bool focusLight=false;
  26. bool stripTestMode=false;
  27. bool baseExposure=false;
  28.  
  29. #define RELAY_PIN 5 //relay board pin
  30. #define FOCUS_LED_PIN 0 //Focus button led pin
  31. #define START_LED_PIN 7 //Start button led pin
  32. #define CORR_LED_PIN 6 //Start button led pin
  33.  
  34. //TM1638 Buttons
  35. #define BRIGHTNESS_BUTTON 8
  36. #define STRIPTEST_BUTTON 4
  37. #define STRIPTEST_MODE_BUTTON 5
  38. #define INCREMENT_BUTTON 16
  39. #define SHIFT_BACK_BUTTON 1
  40. #define MINUS_BUTTON 32
  41. #define PLUS_BUTTON 64
  42. #define SHIFT_MINUS_BUTTON 33
  43. #define SHIFT_PLUS_BUTTON 65
  44.  
  45. //EPROM default valuse storage
  46. const byte eeBrightness = 0; //eeprom brightness value address
  47. const byte eeIncrement = 1; //eeprom f-stop selector increment value address
  48. const byte eeLastFStopValue = 3; //eeprom step program input mode value address
  49. const byte eeStepIdx = 6; //eeprom step program input mode value address
  50.  
  51. byte brightnessValue;
  52. uint8_t tmButtons;
  53. const long intervalButton = 300; // interval to read button (milliseconds)
  54. byte debounce(10); //general debounce delay, ok for buttons and encoder
  55. byte uiMode; //mode for main loop mode management
  56. int displayRefreshTracker; //track encoder change used by all selection functions
  57. byte bip(100); //bip duration
  58. int shortTone (500); //short tone duration
  59. int longTone (1000); //long tone duration
  60. int toneLow(880); //lower frequency tone (A 4th)
  61. int toneHigh(1780); //higher frequency tone (A 5th)
  62. byte shortPause(150); //short tone pause
  63. int readingDelay = 1000;//1 sec. human reading delay
  64. char tempString[9]; //TM1638 Display digits with two decimals.
  65.  
  66. volatile unsigned int buttonPlusMinusValue; //+/- exposure functions
  67. int increment;
  68. int plusminus; // plusminus button value
  69. bool loadDefault;
  70.  
  71. //Timer
  72. byte timerIncrement []= {8,16,33,50,100}; //Preset f-stop fractions increments in hundreds
  73. byte timerIncrementSize = 5;
  74. byte timerInc; //fstop increment value
  75.  
  76. unsigned int FStop; //F-stop value
  77. unsigned int tensSeconds; //time value in 1/10th of seconds
  78. unsigned int resumeTime; //Resume time value
  79. double deltaFStop; //scaling f-stop delta value
  80. double lengthRatio = 0;
  81. unsigned long timeMillis;//time in millis
  82. unsigned long startMillis;//start time
  83. unsigned long elapsedMillis;//elapsed time
  84. unsigned long millisInterval = 100;//10 Hz timer display update frequency
  85. unsigned long time_passed;//elapsed button time
  86. int stepIdx;
  87.  
  88. //Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU)
  89. TM1638plus tm(STROBE_TM, CLOCK_TM , DIO_TM, high_freq);
  90. void setup()
  91. {
  92. Serialinit();
  93. tm.displayBegin();
  94. //EEPROM
  95. #if defined(ESP8266)
  96. EEPROM.begin(6);// Allocate The Memory Size Needed
  97. #endif
  98. timerInc = EEPROM.read(eeIncrement);//f-stop buttonPlueMinus increment
  99. stepIdx = EEPROM.read(eeStepIdx);
  100. if (stepIdx==0) stepIdx=3;
  101. if (timerInc==0) timerInc=timerIncrement[2];
  102. tm.setLED(stepIdx, 1);
  103. brightnessValue = EEPROM.read(eeBrightness);
  104. //Values
  105. FStop = 0;
  106. deltaFStop = 0;
  107. timeMillis = 0;
  108. elapsedMillis = 0;
  109. buttonPlusMinusValue = 0;
  110. plusminus=0;
  111. increment = 0;
  112. displayRefreshTracker = 10000; //value for displays refresh
  113. time_passed = millis();
  114. loadDefault=true;
  115. //Pin modes
  116. pinMode(TONE_PIN, OUTPUT);
  117. pinMode(RELAY_PIN, OUTPUT);
  118. //Display
  119. brightnessInit();
  120. bipHigh();//set-up end signal
  121. }
  122.  
  123. void loop()
  124. {
  125. uiModes();
  126. }
  127.  
  128. void uiModes() //timer mode and related functions
  129. {
  130. tmButtons = buttonsRead(); //check buttons events
  131. if (tmButtons != 0) displayRefreshTracker = 10000;//Value to get any display update
  132.  
  133. if (uiMode==0)
  134. {
  135. plusminus=0;
  136. switch(tmButtons)
  137. {
  138. case 0x16:
  139. uiMode = 1; //Clear buttonPlueMinus before timer mode (uiMode 0)
  140. break;
  141. case 0x02:
  142. uiMode = 4; //Focus light on/off
  143. break;
  144. case STRIPTEST_BUTTON:
  145. uiMode = 2; //Strip Test Mode
  146. break;
  147. case BRIGHTNESS_BUTTON:
  148. uiMode = 99; //Brightness set-up
  149. break;
  150. case INCREMENT_BUTTON://F-stop increment set-up
  151. uiMode = 14;
  152. break;
  153. case STRIPTEST_MODE_BUTTON:
  154. uiMode=16;
  155. break;
  156. case 65:
  157. uiMode = 18; //Correction set-up
  158. break;
  159. case 129:
  160. uiMode = 19; //Correction reset
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. if(millis()-time_passed > 5000) {
  167. time_passed = millis();
  168. uiMode=0;
  169. }
  170. }
  171.  
  172. switch (uiMode)
  173. {
  174. case 1: //
  175. buttonPlusMinusValue = 0;
  176. uiMode = 0; //default mode
  177. break;
  178. case 2:
  179. stripTest();
  180. break;
  181. case 3:
  182. uiMode = 22;
  183. break;
  184. case 4:
  185. focusOnOff(); // Focus Lamp on/off
  186. break;
  187. case 12:
  188. focusOnOff();
  189. break;
  190. case 14:
  191. fstopIncrementSetUp();
  192. break;
  193. case 18:
  194. scaleCalculator();
  195. break;
  196. case 19:
  197. clearCorrection();
  198. break;
  199. case 99:
  200. brightnessSelector();
  201. break;
  202. default:
  203. fstopSelector(); //default mode
  204. }
  205. }
  206. // Display Text with decimal points
  207. void displayText(const char *text, int p, int p2) {
  208. char c, pos=0;
  209. while ((c = (*text++)) && pos < TM_DISPLAY_SIZE) {
  210. if ((p==pos ||p2==pos) && p>0) {
  211. tm.displayASCIIwDot(pos++, c);
  212. } else {
  213. tm.displayASCII(pos++, c);
  214. }
  215. }
  216. }
  217. // Reset LED on the TM1638
  218. void clearStripLEDs() {
  219. for (uint8_t LEDposition = 1; LEDposition < 7; LEDposition++) {
  220. tm.setLED(LEDposition, 0);
  221. }
  222. }
  223.  
  224. // Read and debounce the buttons from TM1638 every interval
  225. uint8_t buttonsRead(void)
  226. {
  227. uint8_t buttons = 0;
  228. static unsigned long previousMillisButton = 0; // executed once
  229. unsigned long currentMillis = millis();
  230. if (currentMillis - previousMillisButton >= intervalButton) {
  231. previousMillisButton = currentMillis;
  232. buttons = tm.readButtons();
  233. }
  234. return buttons;
  235. }
  236.  
  237. //Function to setup serial called from setup FOR debug
  238. void Serialinit()
  239. {
  240. Serial.begin(9600);
  241. Serial.println("F-STOP Timer");
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment