Created
October 2, 2023 13:50
-
-
Save ulikoehler/44aef619f96f612422bacd446323c7bb to your computer and use it in GitHub Desktop.
Motor pulse contorller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
constexpr int Pin_R_Enable = 14; // TODO | |
constexpr int Pin_L_Enable = 15; // TODO | |
constexpr int Pin_R_PWM = 16; // TODO | |
constexpr int Pin_L_PWM = 17; // TODO | |
constexpr int LEDC_CHANNEL_LEFT = LEDC_CHANNEL_0; | |
constexpr int LEDC_CHANNEL_RIGHT = LEDC_CHANNEL_1; | |
void SetDirection(bool left) { | |
if(left) { | |
digitalWrite(Pin_L_Enable, HIGH); | |
digitalWrite(Pin_R_Enable, LOW); | |
} else { | |
digitalWrite(Pin_L_Enable, LOW); | |
digitalWrite(Pin_R_Enable, HIGH); | |
} | |
} | |
void SetSpeed(bool left, uint16_t value=4096) { | |
if(left) { | |
ledcWrite(LEDC_CHANNEL_LEFT, value); | |
} else { | |
ledcWrite(LEDC_CHANNEL_RIGHT, value); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(Pin_R_Enable, OUTPUT); | |
digitalWrite(Pin_R_Enable, LOW); | |
pinMode(Pin_L_Enable, OUTPUT); | |
digitalWrite(Pin_L_Enable, LOW); | |
pinMode(Pin_R_PWM, OUTPUT); | |
pinMode(Pin_L_PWM, OUTPUT); | |
// L PWM | |
ledcSetup(LEDC_CHANNEL_LEFT, 10000 /* Hz */, 12); | |
ledcAttachPin(GPIO_NUM_16 /* TODO*/, LEDC_CHANNEL_LEFT); | |
ledcWrite(LEDC_CHANNEL_LEFT, 2048); // 50% | |
// R PWM | |
ledcSetup(LEDC_CHANNEL_RIGHT, 10000 /* Hz */, 12); | |
ledcAttachPin(GPIO_NUM_17 /* TODO */, LEDC_CHANNEL_RIGHT); | |
ledcWrite(LEDC_CHANNEL_RIGHT, 2048); // 50% | |
} | |
void loop() { | |
// Left | |
SetDirection(/*left=*/true); | |
SetSpeed(/*left=*/true, /*value=*/2048); | |
delayMicroseconds(2000); // millisekunden left | |
// Right | |
SetDirection(/*left=*/false); | |
SetSpeed(/*left=*/false, /*value=*/2048); | |
delayMicroseconds(2000); // millisekunden left | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment