pleasedontcode

Motor Controller rev_02

Aug 13th, 2025
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Motor Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-13 05:54:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Ich brauche ein Code der wie eine normale Rolladen */
  21.     /* Steuerung funktioniert die ich mit alexa steuern */
  22.     /* kann also ich brauche hoch Mitte und runter also */
  23.     /* die Positionen die der Rolladen durch 3 Taster */
  24.     /* weiß wo er gerade ist und stoppen muss */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Er soll nur die Positionen erfragen wo er gerade */
  27.     /* hin fahren soll und wo er sich gerade befindet */
  28.     /* damit er weiß ob er hoch oder runter fahren muss */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /* START CODE */
  32. /****** DEFINITION OF LIBRARIES *****/
  33. // Für einfache Taster und Motorsteuerung keine externen Bibliotheken notwendig
  34.  
  35. /****** PIN DEFINITION *****/
  36. const int pinTasterHoch = 12;   // Taster für Hoch
  37. const int pinTasterMitte = 14;  // Taster für Mitte
  38. const int pinTasterRunter = 27;  // Taster für Runter
  39. const int pinMotorHoch = 25;     // Motor Hoch Steuerung
  40. const int pinMotorRunter = 26;   // Motor Runter Steuerung
  41.  
  42. /****** SYSTEM VARIABLES *****/
  43. int position = 0; // Aktuelle Position in Prozent (0-100)
  44. int targetPosition = -1; // Zielposition, -1 bedeutet keine Bewegung
  45. bool istBewegung = false; // Bewegung läuft gerade
  46.  
  47. // Debounce Variablen
  48. unsigned long lastDebounceTimeHoch = 0;
  49. unsigned long lastDebounceTimeMitte = 0;
  50. unsigned long lastDebounceTimeRunter = 0;
  51. const unsigned long debounceDelay = 50; // ms
  52.  
  53. // Taster Zustände
  54. int lastTasterHochState = LOW;
  55. int lastTasterMitteState = LOW;
  56. int lastTasterRunterState = LOW;
  57.  
  58. /****** FUNCTION PROTOTYPES *****/
  59. void fahreHoch();
  60. void fahreRunter();
  61. void stopMotor();
  62. void updatePosition();
  63. void checkButtons();
  64. void moveToPosition(int pos);
  65. void handleMovement();
  66.  
  67. void setup(void)
  68. {
  69.   // Pin Modus setzen
  70.   pinMode(pinTasterHoch, INPUT_PULLUP);
  71.   pinMode(pinTasterMitte, INPUT_PULLUP);
  72.   pinMode(pinTasterRunter, INPUT_PULLUP);
  73.   pinMode(pinMotorHoch, OUTPUT);
  74.   pinMode(pinMotorRunter, OUTPUT);
  75.  
  76.   // Motor Ausgänge initial auf LOW
  77.   digitalWrite(pinMotorHoch, LOW);
  78.   digitalWrite(pinMotorRunter, LOW);
  79.  
  80.   Serial.begin(115200);
  81.   // Initiale Position (z.B. unten)
  82.   position = 0;
  83.   targetPosition = -1;
  84. }
  85.  
  86. void loop(void)
  87. {
  88.   checkButtons();
  89.   handleMovement();
  90.   updatePosition();
  91.   // Hier könnte man noch eine Kommunikation mit Alexa integrieren
  92.   // z.B. via HTTP Server oder MQTT, ist hier aber nicht implementiert
  93. }
  94.  
  95. /****** Funktionen *****/
  96.  
  97. // Motor hoch fahren
  98. void fahreHoch()
  99. {
  100.   digitalWrite(pinMotorHoch, HIGH);
  101.   digitalWrite(pinMotorRunter, LOW);
  102. }
  103.  
  104. // Motor runter fahren
  105. void fahreRunter()
  106. {
  107.   digitalWrite(pinMotorHoch, LOW);
  108.   digitalWrite(pinMotorRunter, HIGH);
  109. }
  110.  
  111. // Motor stoppen
  112. void stopMotor()
  113. {
  114.   digitalWrite(pinMotorHoch, LOW);
  115.   digitalWrite(pinMotorRunter, LOW);
  116. }
  117.  
  118. // Position aktualisieren (simuliert, in realer Anwendung Sensoren verwenden)
  119. void updatePosition()
  120. {
  121.   if (istBewegung)
  122.   {
  123.     if (targetPosition > position)
  124.     {
  125.       // Bewege nach oben
  126.       if (position < targetPosition)
  127.       {
  128.         position++;
  129.       }
  130.       else
  131.       {
  132.         stopMotor();
  133.         istBewegung = false;
  134.         targetPosition = -1;
  135.       }
  136.     }
  137.     else if (targetPosition < position)
  138.     {
  139.       // Bewege nach unten
  140.       if (position > targetPosition)
  141.       {
  142.         position--;
  143.       }
  144.       else
  145.       {
  146.         stopMotor();
  147.         istBewegung = false;
  148.         targetPosition = -1;
  149.       }
  150.     }
  151.     else
  152.     {
  153.       // Ziel erreicht
  154.       stopMotor();
  155.       istBewegung = false;
  156.       targetPosition = -1;
  157.     }
  158.   }
  159. }
  160.  
  161. // Taster prüfen und Aktionen auslösen
  162. void checkButtons()
  163. {
  164.   int readingHoch = digitalRead(pinTasterHoch);
  165.   int readingMitte = digitalRead(pinTasterMitte);
  166.   int readingRunter = digitalRead(pinTasterRunter);
  167.  
  168.   unsigned long currentTime = millis();
  169.  
  170.   // Hoch Taster
  171.   if (readingHoch != lastTasterHochState)
  172.   {
  173.     lastDebounceTimeHoch = currentTime;
  174.   }
  175.   if ((currentTime - lastDebounceTimeHoch) > debounceDelay)
  176.   {
  177.     if (readingHoch == LOW) // Taster gedrückt (bei INPUT_PULLUP LOW)
  178.     {
  179.       // Ziel: oben (100)
  180.       moveToPosition(100);
  181.     }
  182.   }
  183.   lastTasterHochState = readingHoch;
  184.  
  185.   // Mitte Taster
  186.   if (readingMitte != lastTasterMitteState)
  187.   {
  188.     lastDebounceTimeMitte = currentTime;
  189.   }
  190.   if ((currentTime - lastDebounceTimeMitte) > debounceDelay)
  191.   {
  192.     if (readingMitte == LOW)
  193.     {
  194.       // Ziel: Mitte (50)
  195.       moveToPosition(50);
  196.     }
  197.   }
  198.   lastTasterMitteState = readingMitte;
  199.  
  200.   // Runter Taster
  201.   if (readingRunter != lastTasterRunterState)
  202.   {
  203.     lastDebounceTimeRunter = currentTime;
  204.   }
  205.   if ((currentTime - lastDebounceTimeRunter) > debounceDelay)
  206.   {
  207.     if (readingRunter == LOW)
  208.     {
  209.       // Ziel: unten (0)
  210.       moveToPosition(0);
  211.     }
  212.   }
  213.   lastTasterRunterState = readingRunter;
  214. }
  215.  
  216. // Bewegung zum Ziel
  217. void moveToPosition(int pos)
  218. {
  219.   targetPosition = pos;
  220.   if (position < targetPosition)
  221.   {
  222.     fahreHoch();
  223.     istBewegung = true;
  224.   }
  225.   else if (position > targetPosition)
  226.   {
  227.     fahreRunter();
  228.     istBewegung = true;
  229.   }
  230.   else
  231.   {
  232.     // Bereits am Ziel
  233.     stopMotor();
  234.     istBewegung = false;
  235.   }
  236. }
  237.  
  238. // Steuerung der Bewegung
  239. void handleMovement()
  240. {
  241.   if (istBewegung)
  242.   {
  243.     // Position wird in updatePosition() simuliert
  244.     // Hier könnten Sensoren eingebunden werden
  245.   }
  246. }
  247.  
Advertisement
Add Comment
Please, Sign In to add comment