Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Motor Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-08-13 05:54:25
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Ich brauche ein Code der wie eine normale Rolladen */
- /* Steuerung funktioniert die ich mit alexa steuern */
- /* kann also ich brauche hoch Mitte und runter also */
- /* die Positionen die der Rolladen durch 3 Taster */
- /* weiß wo er gerade ist und stoppen muss */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Er soll nur die Positionen erfragen wo er gerade */
- /* hin fahren soll und wo er sich gerade befindet */
- /* damit er weiß ob er hoch oder runter fahren muss */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // Für einfache Taster und Motorsteuerung keine externen Bibliotheken notwendig
- /****** PIN DEFINITION *****/
- const int pinTasterHoch = 12; // Taster für Hoch
- const int pinTasterMitte = 14; // Taster für Mitte
- const int pinTasterRunter = 27; // Taster für Runter
- const int pinMotorHoch = 25; // Motor Hoch Steuerung
- const int pinMotorRunter = 26; // Motor Runter Steuerung
- /****** SYSTEM VARIABLES *****/
- int position = 0; // Aktuelle Position in Prozent (0-100)
- int targetPosition = -1; // Zielposition, -1 bedeutet keine Bewegung
- bool istBewegung = false; // Bewegung läuft gerade
- // Debounce Variablen
- unsigned long lastDebounceTimeHoch = 0;
- unsigned long lastDebounceTimeMitte = 0;
- unsigned long lastDebounceTimeRunter = 0;
- const unsigned long debounceDelay = 50; // ms
- // Taster Zustände
- int lastTasterHochState = LOW;
- int lastTasterMitteState = LOW;
- int lastTasterRunterState = LOW;
- /****** FUNCTION PROTOTYPES *****/
- void fahreHoch();
- void fahreRunter();
- void stopMotor();
- void updatePosition();
- void checkButtons();
- void moveToPosition(int pos);
- void handleMovement();
- void setup(void)
- {
- // Pin Modus setzen
- pinMode(pinTasterHoch, INPUT_PULLUP);
- pinMode(pinTasterMitte, INPUT_PULLUP);
- pinMode(pinTasterRunter, INPUT_PULLUP);
- pinMode(pinMotorHoch, OUTPUT);
- pinMode(pinMotorRunter, OUTPUT);
- // Motor Ausgänge initial auf LOW
- digitalWrite(pinMotorHoch, LOW);
- digitalWrite(pinMotorRunter, LOW);
- Serial.begin(115200);
- // Initiale Position (z.B. unten)
- position = 0;
- targetPosition = -1;
- }
- void loop(void)
- {
- checkButtons();
- handleMovement();
- updatePosition();
- // Hier könnte man noch eine Kommunikation mit Alexa integrieren
- // z.B. via HTTP Server oder MQTT, ist hier aber nicht implementiert
- }
- /****** Funktionen *****/
- // Motor hoch fahren
- void fahreHoch()
- {
- digitalWrite(pinMotorHoch, HIGH);
- digitalWrite(pinMotorRunter, LOW);
- }
- // Motor runter fahren
- void fahreRunter()
- {
- digitalWrite(pinMotorHoch, LOW);
- digitalWrite(pinMotorRunter, HIGH);
- }
- // Motor stoppen
- void stopMotor()
- {
- digitalWrite(pinMotorHoch, LOW);
- digitalWrite(pinMotorRunter, LOW);
- }
- // Position aktualisieren (simuliert, in realer Anwendung Sensoren verwenden)
- void updatePosition()
- {
- if (istBewegung)
- {
- if (targetPosition > position)
- {
- // Bewege nach oben
- if (position < targetPosition)
- {
- position++;
- }
- else
- {
- stopMotor();
- istBewegung = false;
- targetPosition = -1;
- }
- }
- else if (targetPosition < position)
- {
- // Bewege nach unten
- if (position > targetPosition)
- {
- position--;
- }
- else
- {
- stopMotor();
- istBewegung = false;
- targetPosition = -1;
- }
- }
- else
- {
- // Ziel erreicht
- stopMotor();
- istBewegung = false;
- targetPosition = -1;
- }
- }
- }
- // Taster prüfen und Aktionen auslösen
- void checkButtons()
- {
- int readingHoch = digitalRead(pinTasterHoch);
- int readingMitte = digitalRead(pinTasterMitte);
- int readingRunter = digitalRead(pinTasterRunter);
- unsigned long currentTime = millis();
- // Hoch Taster
- if (readingHoch != lastTasterHochState)
- {
- lastDebounceTimeHoch = currentTime;
- }
- if ((currentTime - lastDebounceTimeHoch) > debounceDelay)
- {
- if (readingHoch == LOW) // Taster gedrückt (bei INPUT_PULLUP LOW)
- {
- // Ziel: oben (100)
- moveToPosition(100);
- }
- }
- lastTasterHochState = readingHoch;
- // Mitte Taster
- if (readingMitte != lastTasterMitteState)
- {
- lastDebounceTimeMitte = currentTime;
- }
- if ((currentTime - lastDebounceTimeMitte) > debounceDelay)
- {
- if (readingMitte == LOW)
- {
- // Ziel: Mitte (50)
- moveToPosition(50);
- }
- }
- lastTasterMitteState = readingMitte;
- // Runter Taster
- if (readingRunter != lastTasterRunterState)
- {
- lastDebounceTimeRunter = currentTime;
- }
- if ((currentTime - lastDebounceTimeRunter) > debounceDelay)
- {
- if (readingRunter == LOW)
- {
- // Ziel: unten (0)
- moveToPosition(0);
- }
- }
- lastTasterRunterState = readingRunter;
- }
- // Bewegung zum Ziel
- void moveToPosition(int pos)
- {
- targetPosition = pos;
- if (position < targetPosition)
- {
- fahreHoch();
- istBewegung = true;
- }
- else if (position > targetPosition)
- {
- fahreRunter();
- istBewegung = true;
- }
- else
- {
- // Bereits am Ziel
- stopMotor();
- istBewegung = false;
- }
- }
- // Steuerung der Bewegung
- void handleMovement()
- {
- if (istBewegung)
- {
- // Position wird in updatePosition() simuliert
- // Hier könnten Sensoren eingebunden werden
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment