Switch case dc motor

Hello everbody, i'm trying to control my dc motor with the "switch case" because how we know it, it's imposible to control the motor position w/out an enconder. how i'll not use it i tried to use the switch case how y'll see bellow:
void setup() {
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
Serial.begin(9600);

}

void loop() {
if (Serial.available() > 0) {
int comando = Serial.read();

switch (comando) {
  
case'1':

Serial.println("Abrir");
digitalWrite(motor1Pin1, HIGH);
delay(1000);
digitalWrite(motor1Pin2, LOW);
delay(1000);
break;

case'2':

Serial.println("Fechar");
digitalWrite(motor1Pin1, LOW);
delay(1000);
digitalWrite(motor1Pin2, HIGH);
delay(1000);
break;

but even if i use this, the dc motor dont stop when i want...after a time that i decided "(1000)".

like this /

the dc motor start and stop, in a time that i want

// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;

const int freq = 1500;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

char a;
char b;

void Setup() {

Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);

}

void loop() {

char comando=Serial.read();
Serial.println(comando);
delay(2000);

switch (comando) {
case 'a':
digitalWrite(motor1Pin1, HIGH);
delay(1000);
break;
case 'b':
digitalWrite(motor1Pin2, HIGH);
delay(1000);
break;
}

}

Hi, @ykinifi

To add code please click this link;

Can you please post a schematic of your project?
Can you please tell us what model Arduino are you using?
What motor you are controlling?
What are you using as a motor driver?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem :wink:

1 Like

Try changing the variable you read into to a char.

  char comando = Serial.read();

hello tom!!! i hope you're going well.

this project is for a window, a mini window...that's why i'm using the dc motor, because the micro servo motor 9g can just reach 180º...how this window is a little big i decided to use a dc motor...

my microcontroller is an esp wrover and i'm using a ln289 driver...the h-brigde.

my all code is bellow /

// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;

const int freq = 1500;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

char a;
char b;

void Setup() {

Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);

}

void loop() {

char comando=Serial.read();
Serial.println(comando);
delay(2000);

switch (comando) {
case 'a':
digitalWrite(motor1Pin1, HIGH);
delay(1000);
break;
case 'b':
digitalWrite(motor1Pin2, HIGH);
delay(1000);
break;
}

}

tks bro, i'm joing in this world yet LOL.

i'm sorry xD

"Setup" needs to be "setup" for code to compile

looks like you have one motor. pins 1&2 control direction and enable speed.

don't see pin 14 being a PWM pin on any Arduino (see analogWrite())

consider that uses 'f' for forward, 'r' for reverse and anything else stop

// Pin1/2 control direction, enablePin controls speed
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enablePin = 14;

void setup ()
{
    Serial.begin (9600);

    pinMode (motor1Pin1, OUTPUT);
    pinMode (motor1Pin2, OUTPUT);
    pinMode (enablePin,  OUTPUT);
}

void loop ()
{
    if (Serial.available ())  {
        char comando=Serial.read ();
        Serial.println (comando);

        switch (comando) {
        case 'r':       // reverse
            digitalWrite (motor1Pin1, HIGH);
            digitalWrite (motor1Pin2, LOW);
            digitalWrite (enablePin, HIGH);
            break;

        case 'f':       // forward
            digitalWrite (motor1Pin1, LOW);
            digitalWrite (motor1Pin2, HIGH);
            digitalWrite (enablePin, HIGH);
            break;

        default:        // stop
            digitalWrite (enablePin, LOW);
            break;
        }
    }
}

tks for helping bro!

but the problem has been the dc motor dont breaking after the time that i want, this is why i thought that using delay the dc motor would start after my command, and break after a time...maybe 1 sec after

In case you missed it.

why couldn't i use this one?

The ESP32 has an internal module called the MCWPM,
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html
that can interface with the ln289.

1 Like
#include "driver/mcpwm.h"

voiding setup()
{
  //
  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1A, GPIO_NUM_4 ); // Azimuth
  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_NUM_12 ); // Altitude servo
  //
  mcpwm_config_t pwm_config = {};
  pwm_config.frequency = 50;    //frequency = 50Hz, i.e. for every servo motor time period should be 20ms
  pwm_config.cmpr_a = 0;    //duty cycle of PWMxA = 0
  pwm_config.cmpr_b = 0;    //duty cycle of PWMxb = 0
  pwm_config.counter_mode = MCPWM_UP_COUNTER;
  pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
  log_i( "MCPWM complete" );
  //
  //
  mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);    //Configure PWM0A timer 0 with above settings
  mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config);    //Configure PWM0A timer 1 with above settings
  //  ////
  fMoveAltitude( 1525 );
  fMoveAzimuth( 1500 );

}

void fMoveAltitude( int MoveTo )
{
  mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, MoveTo);
  vTaskDelay( 12 );
}
//////
void fMoveAzimuth( int MoveTo )
{
  mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_OPR_A, MoveTo);
  vTaskDelay( 12 );
}

A code snippet of using the MCPWM under with the Arduino IDE.

Good luck.

i need to be honest, i'm not undertandig why i would use this, because my code worked, the only problem is that i cant make my dc motor break when i want...even just when i use other switch case in te monitor. got it?

i'm sorry, i didnt got what u say

The return type of Serial.read() is int, int16_t.

A char will not hold all the possible return values.

Assigning it to a char will not make a difference in comparisons to character literals like '1'. No difference in the switch/case.

'1' is 49 or 0x31, the same number Serial.read() returns when the next character is '1'.

a7

Yep agreed... fair point.

No worries.

Now please edit your post #5, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software will display it correctly.

you set a pin HIGH but never attempt to undo that after a second by setting the pin LOW

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.