Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ピンアサイン
- //#define EN 19
- #define LPWM 22
- #define RPWM 23
- int PWMMAX = 1023;
- void setup() {
- //PWMタイマ設定
- ledcSetup(0, 20000, 10); //ch0,20kHz,10bit
- ledcSetup(1, 20000, 10); //ch1,20kHz,10bit
- //PWMタイマに出力PIN関連付け
- ledcAttachPin(LPWM, 0);
- ledcAttachPin(RPWM, 1);
- //ENピン初期化
- //pinMode(EN, OUTPUT);
- //Serial初期化
- Serial.begin(115200);
- }
- void loop() {
- if (Serial.available() > 0) { // check serial
- char in = Serial.read();
- if (in == 'k') {
- //digitalWrite(EN, LOW);
- ledcWrite(0, 0);
- ledcWrite(1, 0);
- Serial.println("Stop Motor");
- } else if (in == 'p') {
- int input = Serial.parseInt();
- Serial.println(input);
- Serial.println("PWM control");
- setMotor(input);
- } else if (in == 'f') {
- int input = Serial.parseInt();
- Serial.println(input);
- Serial.println("freqency");
- ledcSetup(0, input, 10); //ch0,20kHz,10bit
- ledcSetup(1, input, 10); //ch1,20kHz,10bit
- } else if (in == 'h') {
- PWMMAX = 1023;
- Serial.println("Slow-Decay");
- } else if (in == 'l') {
- PWMMAX = 0;
- Serial.println("Fast-Decay");
- }
- }
- }
- void setMotor(int pwm) {
- if (pwm == 0) { //ブレーキ
- //digitalWrite(EN, HIGH);
- ledcWrite(0, 1023);
- ledcWrite(1, 1023);
- } else if (pwm > 0 && pwm <= 1023) {
- //digitalWrite(EN, HIGH);
- ledcWrite(0, abs(PWMMAX - pwm));
- ledcWrite(1, PWMMAX);
- } else if (pwm < 0 && pwm >= -1023) {
- //digitalWrite(EN, HIGH);
- ledcWrite(0, PWMMAX);
- ledcWrite(1, abs(pwm + PWMMAX));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement