Go Down

Topic: Buttons that control motor occasionally not working  (Read 607 times) previous topic - next topic

Nikos_N

Hi everybody,

Utter newbie here, so I would really appreciate any feedback.

I am using two buttons and an H-bridge to make a small hobby motor spin from both directions. Initially, I was using a potentiometer that was making it spin clockwise or counter-clockwise depending on the latter's value.
I can achieve to make the motor spin with the buttons, but mysteriously enough I need to include a line on the code, that reads the potentiometer's value, although the potentiometer is not connected anymore. Otherwise, the code is not working.
The code looks as follows:

Code: [Select]



//Hbridge Motor Control
const int EN=9;   //Half Bridge 1 Enable
const int MC1=3;  //Motor Control 1
const int MC2=2;  //Motor Control 2
const int POT=0;  //POT on Analog Pin 0
const int BUTTON1=7;      //Button1 is connected to pin 7
const int BUTTON2=6;      //Button2 is connected to pin 6

int val = 0;      //for storing the reading from the POT

void setup()
{
    pinMode(EN, OUTPUT);
    pinMode(MC1, OUTPUT);
    pinMode(MC2, OUTPUT);
    pinMode(BUTTON1, INPUT);
    pinMode(BUTTON2, INPUT);
    brake(); //Initialize with motor stopped
}

void loop()
{
    val = analogRead(POT);
    if (digitalRead(BUTTON1) == HIGH) forward(150);
    else if (digitalRead(BUTTON2) == HIGH) reverse(150);

    else brake();
 
}

//Motor goes forward
void forward (int rate)
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, HIGH);
    digitalWrite(MC2, LOW);
    analogWrite(EN, rate);
}

//Motor goes backwards
void reverse (int rate)
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, LOW);
    digitalWrite(MC2, HIGH);
    analogWrite(EN, rate);
}

//Stops motor
void brake ()
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, LOW);
    digitalWrite(MC2, LOW);
    digitalWrite(EN, HIGH);
}



Any ideas?
Thanks :)

TommiP

#1
Dec 01, 2017, 09:41 am Last Edit: Dec 01, 2017, 09:50 am by TommiP Reason: Some additional info
Hi everybody,

Utter newbie here, so I would really appreciate any feedback.

I am using two buttons and an H-bridge to make a small hobby motor spin from both directions. Initially, I was using a potentiometer that was making it spin clockwise or counter-clockwise depending on the latter's value.
I can achieve to make the motor spin with the buttons, but mysteriously enough I need to include a line on the code, that reads the potentiometer's value, although the potentiometer is not connected anymore. Otherwise, the code is not working.
The code looks as follows:

Code: [Select]



//Hbridge Motor Control
const int EN=9;   //Half Bridge 1 Enable
const int MC1=3;  //Motor Control 1
const int MC2=2;  //Motor Control 2
const int POT=0;  //POT on Analog Pin 0
const int BUTTON1=7;      //Button1 is connected to pin 7
const int BUTTON2=6;      //Button2 is connected to pin 6

int val = 0;      //for storing the reading from the POT

void setup()
{
    pinMode(EN, OUTPUT);
    pinMode(MC1, OUTPUT);
    pinMode(MC2, OUTPUT);
    pinMode(BUTTON1, INPUT);
    pinMode(BUTTON2, INPUT);
    brake(); //Initialize with motor stopped
}

void loop()
{
    val = analogRead(POT);
    if (digitalRead(BUTTON1) == HIGH) forward(150);
    else if (digitalRead(BUTTON2) == HIGH) reverse(150);

    else brake();
 
}

//Motor goes forward
void forward (int rate)
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, HIGH);
    digitalWrite(MC2, LOW);
    analogWrite(EN, rate);
}

//Motor goes backwards
void reverse (int rate)
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, LOW);
    digitalWrite(MC2, HIGH);
    analogWrite(EN, rate);
}

//Stops motor
void brake ()
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, LOW);
    digitalWrite(MC2, LOW);
    digitalWrite(EN, HIGH);
}



Any ideas?
Thanks :)
Hi.

So, after you've removed line

Code: [Select]

val = analogRead(POT);


it doesn't work anymore, right ?

Second thing, are you using any pullups in your pins for buttons ?

Third thing, a hint: Learn how to use brackets if you're fresh in C-language. It saves your nerves in the future :D

Instead of this..

Code: [Select]

if (digitalRead(BUTTON1) == HIGH) forward(150);
    else if (digitalRead(BUTTON2) == HIGH) reverse(150);
    else brake();


..use this kind of code:

Code: [Select]

if (digitalRead(BUTTON1) == HIGH)
{
     forward(150);
}
else if (digitalRead(BUTTON2) == HIGH)
{
     reverse(150);
}
else
{
     brake();
}



For this case something more, it'd be wise to use both inputs for controlling to avoid conflicts while using both buttons:
Code: [Select]

if (digitalRead(BUTTON1) == HIGH && digitalRead(BUTTON2) == LOW)
{
     forward(150);
}
else if (digitalRead(BUTTON1) == LOW && digitalRead(BUTTON2) == HIGH)
{
     reverse(150);
}
else
{
     brake();
}






TommiP




Nikos_N

Hi TommiP!!
Thank you for your help.
Yes, when I remove the line where the potentiometer value is read, the code is not working as intended. And yes, I am using pull-ups in both buttons. Also, thanks for the hint. I was avoiding curly braces because they reminded me of javascript, but you are definitely right. They increase readability :D. I also modified the if-else clauses as you recommended, but the same problem pertains :/

cattledog

Quote
And yes, I am using pull-ups in both buttons.
Can you please provide a sketch of how you have your buttons connected.

If you were using external (or the internal) pullups, the pins would read HIGH when open and LOW when closed and connected to ground.

With pulldowns, they would be LOW when not pressed and HIGH when pressed if they are connected to 5V through the closed switch.

Nikos_N

Hi cattledog and thanks for your answer.
The thing is that the circuit is not responding only when commenting out the line that reads the potentiometer value. When it is there, the button values are interpreted correctly.

MarkT

Post the latest code indicating the line you mean please.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Nikos_N

Hi MarkT and thanks for your post.
Here is the latest code:

Code: [Select]

//Hbridge Motor Control
const int EN=9;   //Half Bridge 1 Enable
const int MC1=3;  //Motor Control 1
const int MC2=2;  //Motor Control 2
const int POT=0;  //POT on Analog Pin 0
const int BUTTON1=7;      //Button1 is connected to pin 7
const int BUTTON2=6;      //Button2 is connected to pin 6

int val = 0;      //for storing the reading from the POT

void setup()
{
    pinMode(EN, OUTPUT);
    pinMode(MC1, OUTPUT);
    pinMode(MC2, OUTPUT);
    pinMode(BUTTON1, INPUT);
    pinMode(BUTTON2, INPUT);
    brake(); //Initialize with motor stopped
}

void loop()
{
    val = analogRead(POT);
    if (digitalRead(BUTTON1) == HIGH && digitalRead(BUTTON2) == LOW)
    {
      forward(150);
    }
    else if (digitalRead(BUTTON1) == LOW && digitalRead(BUTTON2) == HIGH) {
      reverse(150);
    }

    else brake();
 
}

//Motor goes forward
void forward (int rate)
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, HIGH);
    digitalWrite(MC2, LOW);
    analogWrite(EN, rate);
}

//Motor goes backwards
void reverse (int rate)
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, LOW);
    digitalWrite(MC2, HIGH);
    analogWrite(EN, rate);
}

//Stops motor
void brake ()
{
    digitalWrite(EN, LOW);
    digitalWrite(MC1, LOW);
    digitalWrite(MC2, LOW);
    digitalWrite(EN, HIGH);
}



and the line, which if commented out, brings trouble is:

Code: [Select]

val = analogRead(POT);

Robin2

I think that line is just acting a short delay of about 100 microsecs. It may be having the effect of skipping some switch bounce. As a test, try replacing that line with delay(10); which is a much longer interval.

If that works then for a longer term solution look at how millis() is used to manage timing without blocking in Several things at a time - particularly how it is used to create an interval between successive button reads.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up