Advertisement
Guest User

Sketch

a guest
Aug 23rd, 2020
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.79 KB | None | 0 0
  1. #include <FastIO.h>
  2. #include <I2CIO.h>
  3. #include <LCD.h>
  4. #include <LiquidCrystal.h>
  5. #include <LiquidCrystal_I2C.h>
  6. #include <LiquidCrystal_I2C_ByVac.h>
  7. #include <LiquidCrystal_SI2C.h>
  8. #include <LiquidCrystal_SR.h>
  9. #include <LiquidCrystal_SR1W.h>
  10. #include <LiquidCrystal_SR2W.h>
  11. #include <LiquidCrystal_SR3W.h>
  12. #include <SI2CIO.h>
  13. #include <SoftI2CMaster.h>
  14.  
  15.  
  16. //Display:
  17.  
  18. #define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
  19. #define BACKLIGHT_PIN     3
  20. #define En_pin  2
  21. #define Rw_pin  1
  22. #define Rs_pin  0
  23. #define D4_pin  4
  24. #define D5_pin  5
  25. #define D6_pin  6
  26. #define D7_pin  7
  27.  
  28.  
  29.  
  30. LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
  31. String myTxt;
  32.  
  33. /*
  34.  * 0 = Hauptmenü
  35.  * 1 = Setup
  36.  * 2 = Manuell
  37.  */
  38. int state = 0;                // Aktueller Programmteil
  39. int stateAlt = -1;            // Alter Status
  40.  
  41. // Drehknopf
  42. #define DIR_CCW 0x10
  43. #define DIR_CW 0x20
  44. int Rotary1 = 10;              // Rotary Drehknopf
  45. int Rotary2 = 11;              // Rotary Drehknopf
  46. int RotaryButton = 12;         // Rotary Button
  47. int rAlt = 0;                 // Rotary PressStatus Alt
  48. int rWert = 0;                // Rotary DrehWert
  49.  
  50. // Poti
  51. int potPin = 2;
  52. int stepSize = 0;
  53.  
  54.  
  55. const int resolution = 16;
  56. const long int maxSteps = 52600; // von Links nach Rechts 17 ganze Schritt a 200
  57. int speed = 800;
  58. long int steps = 0;
  59. long int maxPics = 0;
  60. long int maxDelay = 0;
  61. int pictureCounter = 0;
  62.  
  63. short int shutterPin = 2;  // Auslösen der Kamera
  64. short int motorEnable = 6; // HIGH = Motor deaktiviert | LOW = Motor aktiviert
  65. short int motorStep = 5; // Step
  66. short int motorDir = 4; // Dir | LOW hin zum Motor | High = Weg vom Motor
  67. boolean motorDirection = HIGH; //LOW hin zum Motor | High = Weg vom Motor
  68.  
  69. short int motorM1 = 7; //
  70. short int motorM2 = 8; // -> Bestimmen die Revolution | M1,M2,M3 High = 1/16, M1,M2,M3 auf LOW = 1
  71. short int motorM3 = 9; //
  72.  
  73. /* Pfeil */
  74. byte arrow[8] = {
  75.   B00000,
  76.   B11000,
  77.   B01100,
  78.   B00110,
  79.   B01100,
  80.   B11000,
  81.   B00000,
  82. };
  83.  
  84.  
  85. void setup() {
  86.   lcd.begin(20,4);
  87.   lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  88.   lcd.setBacklight(HIGH);
  89.   lcd.home();
  90.   lcd.print("Camera Slider v1");
  91.   lcd.setCursor(0,1);
  92.   lcd.print("Initialisiere...");
  93.   lcd.setCursor(0,3);
  94.   lcd.print("[24.11.16] by Hansi");
  95.   Serial.begin(9600);
  96.  
  97.   // Rotary Encoder:
  98.   pinMode(Rotary1, INPUT);  // Drehknopf initialisieren
  99.   pinMode(Rotary2, INPUT);  // Drehknopf initialisieren
  100.   digitalWrite(Rotary1, HIGH);
  101.   digitalWrite(Rotary2, HIGH);
  102.   pinMode(RotaryButton, INPUT);      // Button initialisieren
  103.   digitalWrite(RotaryButton, HIGH);  // Button aktivieren
  104.  
  105.   // Motor
  106.   pinMode(motorEnable,OUTPUT); // Enable
  107.   digitalWrite(motorEnable,HIGH); // Motor deaktivieren
  108.   pinMode(motorStep,OUTPUT);
  109.   pinMode(motorDir,OUTPUT);
  110.   pinMode(motorM1,OUTPUT);
  111.   pinMode(motorM2,OUTPUT);
  112.   pinMode(motorM3,OUTPUT);
  113.   setRes(16);
  114.  
  115.   // Foto
  116.   pinMode (shutterPin, OUTPUT);
  117.   delay(1000);
  118. }
  119.  
  120.  
  121. int menuAkt = 0;
  122. int menu[] = {0,1,2};
  123. int menuSize = sizeof(menu) / sizeof(int);
  124. void loop() {
  125.   // Weg vom Motor HIGH
  126.   // Hin zum Motor LOW
  127.   if(state != stateAlt){
  128.     stateAlt = state;
  129.     switch(state){
  130.       case 1: state = loadSetup(); break;
  131.       case 2: state = loadManuell(); break;
  132.       default: state = loadMenu(); break;
  133.     }
  134.   }
  135.   delay(1000);
  136.   /*
  137.   Serial.println(maxSteps);
  138.   speed = 800;
  139.   while(steps < maxSteps){
  140.     Move(200);
  141.     delay(500);
  142.     shutter();
  143.     delay(500);
  144.     steps += 200;
  145.     Serial.print("Schritt ");
  146.     Serial.print(steps);
  147.     Serial.print(" von ");
  148.     Serial.print(maxSteps);
  149.     Serial.print(" | #Fotos: ");
  150.     Serial.println(pictureCounter);
  151.   }
  152.   goBack();
  153.   exit;
  154.   */
  155. }
  156.  
  157.  
  158. /*
  159.  *  Setup:
  160.  */
  161.  
  162. int setupStep = 0;
  163. boolean loadSetup(){
  164.   boolean doSetup = true;
  165.   while(doSetup){
  166.     switch(setupStep){
  167.       case 0:
  168.         showBilder(); // Max. 10520
  169.         setupStep++;
  170.         break;
  171.       case 1:
  172.         showDelay();
  173.         setupStep++;
  174.         break;
  175.       case 2:
  176.         showDirection();
  177.         setupStep++;
  178.         break;
  179.       case 3:
  180.         showUndlos();
  181.         setupStep++;
  182.         break;
  183.       case 4:
  184.         doTimelapse();
  185.         doSetup = false;
  186.         setupStep = 0;
  187.         break;
  188.     }
  189.   }
  190.   return 0;
  191. }
  192.  
  193. /*
  194.  *  Manueller Modus
  195.  */
  196. int rotary = 0;
  197. void showBilder(){
  198.   boolean dome = true;
  199.   lcd.clear();
  200.   lcd.print("Wie viele Bilder?");
  201.   int addPics = analogRead(potPin);
  202.  
  203.   while(dome){
  204.     rotary = getRotary();
  205.     Serial.println(analogRead(potPin));
  206.     if(rotary){
  207.       addPics = analogRead(potPin);
  208.       if(addPics < 1)
  209.         addPics = 1;
  210.       if(rotary > 0){
  211.         maxPics += addPics;
  212.         if(maxPics > maxSteps)
  213.           maxPics = maxSteps;
  214.       }
  215.       if(rotary < 0){
  216.         maxPics -= addPics;
  217.         if(maxPics < 0)
  218.           maxPics = 0;
  219.       }
  220.       if(maxPics > 10520)
  221.         maxPics = 10520;
  222.       clearLine(2);
  223.       myTxt = maxPics + String(" / ") + maxSteps / 5;
  224.       lcd.setCursor(0,2); lcd.print(myTxt);
  225.       myTxt = String("Video: ") + String((float) maxPics / 30) + String("s   ");
  226.       lcd.setCursor(0,3); lcd.print(myTxt);
  227.     }
  228.     if(pushed())
  229.       dome = false;
  230.   }
  231. }
  232.  
  233. void showDelay(){
  234.   boolean dome = true;
  235.   lcd.clear();
  236.   lcd.print("Wie viel Zeit");
  237.   lcd.setCursor(0,1); lcd.print("zw. den Bildern?");
  238.   while(dome){
  239.     rotary = getRotary();
  240.     if(rotary){
  241.       if(rotary > 0)
  242.         maxDelay++;
  243.       if(rotary < 0)
  244.         maxDelay--;
  245.       clearLine(2);
  246.       myTxt = String("Abstand: ") + maxDelay + ("s   ");
  247.       lcd.setCursor(0,2); lcd.print(myTxt);
  248.     }
  249.     if(pushed())
  250.       dome = false;
  251.   }
  252. }
  253.  
  254. void showDirection(){
  255.   boolean dome = true;
  256.   lcd.clear();
  257.   lcd.print("Welche Richtung?");
  258.   lcd.setCursor(0,2); lcd.print("Bewegung");
  259.   lcd.setCursor(0,3); lcd.print("Wohin soll er gehn?");
  260.   while(dome){
  261.     rotary = getRotary();
  262.     if(rotary){
  263.       if(rotary > 0){
  264.         lcd.setCursor(0,3); lcd.print("weg vom Motor");
  265.         motorDirection = HIGH;
  266.       }
  267.       if(rotary < 0){
  268.         lcd.setCursor(0,3); lcd.print("hin zum Motor");
  269.         motorDirection = LOW;
  270.       }
  271.     }
  272.     if(pushed())
  273.       dome = false;
  274.   }
  275. }
  276.  
  277. void showUndlos(){
  278.    boolean dome = true;
  279.   lcd.clear();
  280.   lcd.print("Kann es losgehen?");
  281.   while(dome){
  282.     if(pushed())
  283.       dome = false;
  284.   }
  285. }
  286.  
  287. void doTimelapse(){
  288.   steps = 0;
  289.   pictureCounter = 0;
  290.   setRes(16);
  291.   lcd.clear();
  292.   lcd.print("Timelapse...");
  293.   digitalWrite(motorEnable,LOW);
  294.   digitalWrite(motorDir,motorDirection);
  295.   speed = 800;
  296.   long int maxStepsNow = maxPics;
  297.   int nowMove = (maxSteps / maxStepsNow);
  298.   Serial.println(nowMove);
  299.   while(steps < maxSteps){
  300.     lcd.clear();
  301.     shutter();
  302.     lcd.setCursor(0,0); lcd.print("Timelapse...");
  303.     myTxt = String("Bilder: ") + pictureCounter + (" / ") + (maxPics +  1);
  304.     lcd.setCursor(0,1); lcd.print(myTxt);
  305.     float prozent = ((float) steps / float(maxSteps)) * 100;
  306.     myTxt = String("Fortschritt: ") + prozent + String("%");
  307.     lcd.setCursor(0,2); lcd.print(myTxt);
  308.     myTxt = String("Restdauer: ~") + ((maxPics - pictureCounter) * (maxDelay + 1) + ((maxSteps - steps) / 1000 ) * 1.63) + String("s");
  309.     lcd.setCursor(0,3); lcd.print(myTxt);
  310.     delay(maxDelay * 1000);
  311.     Move(nowMove);
  312.     delay(750);
  313.     steps += nowMove;
  314.   }
  315.   lcd.clear();
  316.   shutter();
  317.   lcd.setCursor(0,0); lcd.print("Timelapse...");
  318.   myTxt = String("Bilder: ") + pictureCounter + (" / ") + (maxPics +  1);
  319.   lcd.setCursor(0,1); lcd.print(myTxt);
  320.   float prozent = ((float) steps / float(maxSteps)) * 100;
  321.   myTxt = String("Fortschritt: ") + prozent + String("%");
  322.   lcd.setCursor(0,2); lcd.print(myTxt);
  323.   myTxt = String("Restdauer: ~") + ((maxPics - pictureCounter) * (maxDelay + 1) + ((maxSteps - steps) / 1000 ) * 1.63) + String("s  ");
  324.   lcd.setCursor(0,3); lcd.print(myTxt);
  325.   delay(maxDelay * 1000);
  326.   digitalWrite(motorEnable,HIGH);
  327.   if(motorDirection == HIGH)
  328.     motorDirection = LOW;
  329.   else
  330.     motorDirection = HIGH;
  331.   goBack();
  332. }
  333.  
  334. boolean loadManuell(){
  335.   setRes(16);
  336.   digitalWrite(motorEnable,LOW);
  337.   lcd.clear();
  338.   myTxt = String("Manueller Modus");
  339.   lcd.print(myTxt);
  340.   int stepSize = 10;
  341.   while(1){
  342.     rotary = getRotary();
  343.     stepSize = analogRead(potPin);
  344.     if(stepSize < 5)
  345.       stepSize = 5;
  346.     if(rotary){
  347.       if(rotary > 0){
  348.         digitalWrite(motorDir,HIGH);
  349.         steps += stepSize;
  350.       }else{
  351.         digitalWrite(motorDir,LOW);
  352.         steps -= stepSize;
  353.       }
  354.       Move(stepSize);
  355.       float prozent = ((float) steps / float(maxSteps)) * 100;
  356.       //prozent = prozent / 100;
  357.       myTxt = String("Distanz: ") + prozent + String("%   ");
  358.       lcd.setCursor(0,1); lcd.print(myTxt);
  359.     }
  360.     if(pushed()){
  361.       Serial.println("Bild!");
  362.       delay(500);
  363.       shutter();
  364.       delay(500);
  365.       myTxt = String("# Fotos: ") + pictureCounter;
  366.       lcd.setCursor(0,2); lcd.print(myTxt);
  367.     }
  368.   }
  369.   digitalWrite(motorEnable,HIGH);
  370.   return 0;
  371. }
  372.  
  373. /*
  374.  * Hauptmenü
  375.  */
  376. boolean menuTrigger = false;
  377. int loadMenu(){
  378.   // 0: Setup starten
  379.   // 1: Manueller Modus
  380.   int menuAlt = 0;
  381.   changeMenu(0);
  382.   while(1){
  383.     int rotary = getRotary();
  384.     if(rotary){
  385.       changeMenu(rotary);
  386.     }
  387.  
  388.     if(pushed() && menuTrigger){
  389.       menuAkt++;
  390.       return menuAkt;
  391.     }
  392.     menuTrigger = true;
  393.   }
  394.   return 0;
  395. }
  396.  
  397. void changeMenu(int s){
  398.   if(!s)
  399.     showMenu(0);
  400.   else{
  401.     if(s < 0)
  402.       menuAkt--;
  403.     else
  404.       menuAkt++;
  405.    
  406.     if(menuAkt > menuSize)
  407.       menuAkt = 0;
  408.     else if(menuAkt < 0)
  409.       menuAkt = menuSize;
  410.     showMenu(menuAkt);
  411.   }
  412. }
  413.  
  414. void showMenu(int s){
  415.   lcd.createChar(0, arrow);
  416.   lcd.clear();
  417.   lcd.setCursor(2,0); lcd.print("Setup starten");
  418.   lcd.setCursor(2,1); lcd.print("Manueller Modus");
  419.  
  420.   switch(s){
  421.     case 1: lcd.setCursor(0,1); lcd.write(byte(0)); break;
  422.     default: lcd.setCursor(0,0); lcd.write(byte(0)); break;
  423.   }
  424.   menuAkt = s;
  425. }
  426.  
  427. void Move(int steps){
  428.   for(int i = 0;i<steps;i++){
  429.     digitalWrite(motorStep,HIGH); // Output high
  430.     delayMicroseconds(speed); // Wait 1/2 a ms
  431.     digitalWrite(motorStep,LOW); // Output low
  432.     delayMicroseconds(speed); // Wait 1/2 a ms
  433.   }
  434. }
  435.  
  436. void shutter(){
  437.   digitalWrite(shutterPin, HIGH);  
  438.   delay(50);
  439.   digitalWrite(shutterPin, LOW);  
  440.   pictureCounter++;
  441. }
  442.  
  443. void goBack(){
  444.   digitalWrite(motorEnable,LOW);
  445.   digitalWrite(motorDir,motorDirection);
  446.   setRes(0);
  447.   speed = 1000;
  448.   if(steps > maxSteps)
  449.     steps = maxSteps;
  450.   while(steps > 0){
  451.     Move(100);
  452.     steps -= 100 * 16;
  453.     Serial.println(steps);
  454.   }
  455.   digitalWrite(motorEnable,HIGH);
  456.   Serial.println("Fertig!");
  457. }
  458.  
  459. void setRes(int i){
  460.   if(i == 16){
  461.     digitalWrite(motorM1,HIGH);
  462.     digitalWrite(motorM2,HIGH);
  463.     digitalWrite(motorM3,HIGH);
  464.   }else{
  465.     digitalWrite(motorM1,LOW);
  466.     digitalWrite(motorM2,LOW);
  467.     digitalWrite(motorM3,LOW);
  468.   }
  469. }
  470.  
  471. /*
  472.  * Knopfdruck abfragen
  473.  */
  474. boolean pushed(){
  475.   int rStatus = digitalRead(RotaryButton);
  476.   if (rStatus != rAlt) {
  477.     rAlt = rStatus;
  478.     if(rStatus == HIGH)
  479.       return true;
  480.   }
  481.   return false;
  482. }
  483.  
  484. /*
  485.  * Drehimpuls abfragen
  486.  */
  487.  
  488. const unsigned char ttable[7][4] = {
  489.   {0x0, 0x2, 0x4,  0x0}, {0x3, 0x0, 0x1, 0x10},
  490.   {0x3, 0x2, 0x0,  0x0}, {0x3, 0x2, 0x1,  0x0},
  491.   {0x6, 0x0, 0x4,  0x0}, {0x6, 0x5, 0x0, 0x20},
  492.   {0x6, 0x5, 0x4,  0x0},
  493. };
  494.  
  495. volatile unsigned char stateR = 0;
  496.  
  497. int getRotary() {
  498.   unsigned char pinstate = (digitalRead(Rotary2) << 1) | digitalRead(Rotary1);
  499.   stateR = ttable[stateR & 0xf][pinstate];
  500.   unsigned char result = (stateR & 0x30);
  501.  
  502.   if(result)
  503.     return (result == DIR_CCW ? -1 : 1);
  504.   return false;
  505. }
  506.  
  507. void clearLine(int i){
  508.   lcd.setCursor(0,i);
  509.   lcd.print("                    ");
  510.   return;
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement