gustavohw

CameraSliderV3

Jul 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.76 KB | None | 0 0
  1. //EEPROM Stuff
  2. #include <EEPROMex.h>
  3. #include <EEPROMvar.h>
  4. const int maxAllowedWrites = 20;
  5. const int memBase          = 100;
  6.  
  7. //Timer
  8. #include "TimerOne.h"
  9.  
  10.  
  11. //------------------------ \/ Encoder \/ ------------------------//
  12. #include <Encoder.h>
  13. #define BUTTON 2
  14. #define ENC0 3
  15. #define ENC1 4
  16. Encoder myEnc(ENC0, ENC1);
  17. long oldPosition  = 0;
  18. boolean button = false;
  19. boolean up = false;
  20. boolean down = false;
  21. boolean middle = false;
  22. int16_t last, value;
  23. //------------------------ /\ Encoder /\ ------------------------//
  24.  
  25. //Random
  26. #include <stdint.h>
  27. bool debug = true;
  28. bool skipCalibration = true;
  29.  
  30.  
  31. //------------------------ \/ LCD Stuff \/ ------------------------//
  32. #include <Adafruit_GFX.h>
  33. #include <Adafruit_PCD8544.h>
  34. #include <gfxfont.h>
  35. Adafruit_PCD8544 display = Adafruit_PCD8544 (8, 9, 10, 11, 12);
  36. //------------------------ /\ LCD Stuff /\ ------------------------//
  37.  
  38.  
  39.  
  40. //Runtime Vars
  41. long day = 86400000; // 86400000 milliseconds in a day
  42. long hour = 3600000; // 3600000 milliseconds in an hour
  43. long minute = 60000; // 60000 milliseconds in a minute
  44. long second =  1000; // 1000 milliseconds in a second
  45. volatile long numruns = 0;
  46.  
  47. unsigned long totalLength = 143000;
  48.  
  49. volatile long period = 10000;
  50. long runtime = 0;
  51. #define MINRUNTIME 10000
  52.  
  53.  
  54. //Stepper Motor
  55. #define DIR 6
  56. #define STEP 7
  57. #define STEPSMM 60
  58. #define MS1 A0
  59. #define MS2 A1
  60. #define MS3 A2
  61. #define StepperEnable A3
  62. boolean sdir = 0;
  63. boolean oldsdir = !sdir;
  64. boolean timerSet = false;
  65. #define DirLeft 0
  66. #define DirRight 1
  67. #define DirHome 0
  68.  
  69. //Endstop
  70. int endStop = 5;
  71.  
  72. #define DEBOUNCE 300
  73. volatile long lastTriggered = 0;
  74.  
  75. long offset = 0;
  76. long sspeed = 0;
  77. long decimals = 0;
  78.  
  79. byte oldDays = 0;
  80. byte oldHours = 0;
  81. byte oldMinutes = 0;
  82. byte oldSeconds = 0;
  83.  
  84. boolean srunning = false;
  85.  
  86. //------------------------\/ Machine State Driver \/------------------------//
  87. typedef enum State {
  88.   Boot,
  89.   Calibration,
  90.   Menu,
  91.   Settings,
  92.   Running,
  93.   Pause,
  94.   Finished,
  95.   Low_Battery
  96. };
  97. State currentState;
  98.  
  99. typedef enum Mode {
  100.   LeftStart,
  101.   RightStart,
  102.   Ping_Pong
  103. };
  104. Mode currentMode;
  105.  
  106. //------------------------/\ Machine State Driver /\------------------------//
  107.  
  108. void catchButton() {
  109.   if (lastTriggered + DEBOUNCE < millis()) {
  110.     button = true;
  111.     middle = true;
  112.     lastTriggered = millis();
  113.     Serial.print("BUTTON TRIG, DIGITAL STATE: ");
  114.     Serial.println(digitalRead(BUTTON));
  115.   }
  116. }
  117.  
  118. void setup() {
  119.   if (debug == true){
  120.     Serial.begin(115200);
  121.     Debug();
  122.   }
  123.  
  124.   StepperInit();
  125.   EncoderInit();
  126.   TimerInit();
  127.  
  128. }
  129.  
  130.  
  131. void TimerInit (){
  132.   Timer1.initialize(period);         // initialize timer1, and set a 1/2 second period
  133.   Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
  134.   Timer1.stop();
  135. }
  136.  
  137. void EncoderInit (){
  138.   digitalWrite(BUTTON, HIGH);
  139.   attachInterrupt(digitalPinToInterrupt(BUTTON), catchButton, FALLING);
  140. }
  141.  
  142. void StepperInit (){
  143.   pinMode(MS1, OUTPUT);
  144.   pinMode(MS2, OUTPUT);
  145.   pinMode(MS3, OUTPUT);
  146.   digitalWrite(MS1, HIGH);
  147.   digitalWrite(MS2, HIGH);
  148.   digitalWrite(MS3, HIGH);
  149.  
  150.   pinMode(STEP, OUTPUT);
  151.   pinMode(DIR, OUTPUT);
  152.   digitalWrite(DIR, sdir);
  153.  
  154.   pinMode(StepperEnable, OUTPUT);
  155.  
  156.   //EndStop
  157.   pinMode(endStop, INPUT_PULLUP);
  158. }
  159.  
  160.  
  161. void callback(){
  162.  
  163.   numruns++;
  164.   digitalWrite(STEP, digitalRead(STEP) ^ 1);
  165.   if (currentState != Calibration){
  166.  
  167.     if (numruns >= totalLength){
  168.      
  169.       if (sdir == DirLeft){
  170.         SetDirection(DirRight);
  171.       }
  172.       else if (sdir == DirRight){
  173.         SetDirection(DirLeft);
  174.       }
  175.       digitalWrite(DIR, sdir);
  176.       numruns = 0;
  177.       srunning = false;
  178.       Timer1.stop();
  179.     }
  180.   }
  181.  
  182. }
  183.  
  184. void loop() {
  185.   EncoderLoop();
  186. }
  187.  
  188.  
  189. void EncoderLoop(){
  190.  
  191.   long newPosition = myEnc.read();
  192.   //Serial.println(newPosition);
  193.   if ((newPosition < 0) && (newPosition < offset)) {
  194.     offset = newPosition;
  195.   }
  196.   newPosition = newPosition - offset;
  197.  
  198.   if (newPosition != oldPosition) {
  199.     oldPosition = newPosition;
  200.     updateRuntime();
  201.     setPeriod();
  202.   }
  203.  
  204.   if (button) {
  205.     if ((sspeed == 0) && (decimals == 0)) {
  206.       srunning = !srunning;
  207.       setPeriod();
  208.     }
  209.     else {
  210.       srunning = !srunning;
  211.       setPeriod();
  212.     }
  213.     button = false;
  214.   }
  215. }
  216.  
  217.  
  218. void updateRuntime() {
  219.   if (oldPosition != 0) {
  220.     runtime = 1000 * oldPosition * oldPosition / 4;
  221.     //runtime = runtime/3;
  222.     int days = runtime / day ;                                //number of days
  223.     int hours = (runtime % day) / hour;                       //the remainder from days division (in milliseconds) divided by hours, this gives the full hours
  224.     int minutes = ((runtime % day) % hour) / minute ;         //and so on...
  225.     int seconds = (((runtime % day) % hour) % minute) / second;
  226.    
  227.     Serial.print("Days: ");
  228.     Serial.print(days);
  229.     Serial.print(" / Hours: ");
  230.     Serial.print(hours);
  231.     Serial.print(" / Minutes: ");
  232.     Serial.print(minutes);
  233.     Serial.print(" / Seconds: ");
  234.     Serial.println(seconds);
  235.   }
  236. }
  237.  
  238.  
  239. void setPeriod() {
  240.   Serial.print("Runtime: ");
  241.   Serial.println(runtime);
  242. //  Serial.print(" / sRunning: ");
  243. //  Serial.println(srunning);
  244. //  Serial.print(" / Number of Runs: ");
  245. //  Serial.println(numruns);
  246.   if ((runtime < MINRUNTIME) | ! srunning) {
  247.     Timer1.stop();
  248.   }
  249.   else {
  250.     float ssspeed = 1000000 / ((float) runtime);
  251.     float sps = ssspeed * STEPSMM;
  252.     float pperiod = 500000 / sps;
  253.     Timer1.setPeriod(pperiod);
  254.     //Serial.println(pperiod);
  255.   }
  256. }
  257. //----------------------------------------State Change----------------------------------------//
  258. void ChangeState (State state){
  259.   Serial.print("Changing State from: ");
  260.   Serial.print(currentState);
  261.   Serial.print(" to: ");
  262.   Serial.println(state);
  263.   currentState = state;
  264. }
  265.  
  266.  
  267. void ChangeMode (Mode mode){
  268.   Serial.print("Changing Mode from: ");
  269.   Serial.print(currentMode);
  270.   Serial.print(" to: ");
  271.   Serial.println(mode);
  272.   currentMode = mode;
  273. }
  274.  
  275. void CalibrationStateChange (CalibrationState state){
  276.   Serial.print("Changing Calibration State from: ");
  277.   Serial.print(calibrationState);
  278.   Serial.print(" to: ");
  279.   Serial.println(state);
  280.   calibrationState = state;
  281. }
  282.  
  283. //----------------------------------------State Change----------------------------------------//
  284.  
  285. //----------------------------------------\/ Stepper Helpers \/----------------------------------------//
  286. void StopStepper(){
  287.   Timer1.stop();
  288.   srunning = false;
  289.   Serial.println("Stopping Timer");
  290. }
  291.  
  292. void ZeroPosition (){
  293.   //distanceTravelled = 0;  
  294.   numruns = 0;
  295.   Serial.println("Zeroing Position");
  296. }
  297.  
  298. void SetPosition (long value){
  299.   distanceTravelled = value;
  300. }
  301.  
  302. void SetDirection (int value){
  303.   sdir = value;
  304.   digitalWrite(DIR, value);
  305.   Serial.print("Direction set to: ");
  306.   Serial.println(value);
  307. }
  308. //----------------------------------------/\ Stepper Helpers /\----------------------------------------//
  309.  
  310. //----------------------------------------\/ Debug Stuff \/----------------------------------------//
  311. void Debug(){
  312.     Serial.println("----------------------------------------------");    
  313.     Serial.println("Starting Camera Slider Serial Debug");    
  314.     Serial.println("----------------------------------------------");  
  315.  
  316.     Serial.println("----------------------------------------------");    
  317.     Serial.print("Checking if we have any remaining distance: ");  
  318. //    if (remainingDistance > 0){
  319. //      Serial.println(remainingDistance);  
  320. //    }
  321. //    else{
  322. //      Serial.println("No remaining distance found. Looks like we are good to go!");
  323. //    }
  324.     Serial.println("----------------------------------------------");  
  325.    
  326. }
  327. //----------------------------------------/\ Debug Stuff /\----------------------------------------//
Add Comment
Please, Sign In to add comment