Having problem at dc motor control with buttons at arduino

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Having problem at dc motor control with buttons at arduino

Post by MEKACI »

Friends, I have another problem now at my project. I have built a miniature testing robot to make easier to observe my coding results. I was writing some part of codes for movement and rotation with button control ;however I am living with some problems.

This is the miniature testing robot :

Image
Image
Image

The problems I am living is that :
" When I click the button it takes time to respond to move motors and light the leds too ,while I continue to push the button, it stops moving then the motors moves again then stops again ". I don't know how to fix this problem. " Debounce " might help to fix this problem but I don't know how to apply successfully. When you are newbie and when you don2t know what to do ( which is right or not ) It is really hard to solve the problem ;and it is happening to me right now. You friends might help me please ? thanks.

This is a small video that shows my problem visually :
http://www.youtube.com/watch?v=HQpqamVd ... e=youtu.be

This the code I have written ( this is only the movement version ) :

Code: Select all

#include <AFMotor.h>
    
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ);   // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ);   // create motor #1, 64KHz pwm


const int buttonPin1 = 18;     // the number of the pushbutton pin for tank movement  backward
const int buttonPin2 = 19;     // the number of the pushbutton pin for tank movement  forward
const int buttonPin3 = 20;     // the number of the pushbutton pin for tank body rotation  turn around 
// const int buttonPin4 = 21;     // the number of the pushbutton pin for tank firing mechanism motor  at BB firing


const int ledPin1 =  14;      // the number of the LED pin to check if the button works properly for tank movement  backward
const int ledPin2 =  15;      // the number of the LED pin to check if the button works properly for tank movement  forward
const int ledPin3 =  16;      // the number of the LED pin to check if the button works properly for tank body rotation  turn around 
// const int ledPin4 =  17;      // the number of the LED pin to check if the button works properly for tank firing mechanism motor  at BB firing

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status for tank body movement  backward
int buttonState2 = 0;         // variable for reading the pushbutton status for tank body movement  forward 
int buttonState3 = 0;         // variable for reading the pushbutton status for tank body rotation  turn around 
// int buttonState4 = 0;         // variable for reading the pushbutton status for tank firing mechanism motor  at BB firing

void setup() {
  
  Serial.begin(9600);           // set up Serial library at 9600 bps
     
     
     motor1.setSpeed(255);     // set the speed to 200/255
          motor2.setSpeed(255);     // set the speed to 200/255
          motor3.setSpeed(255);
          motor4.setSpeed(255);
           
           pinMode(ledPin1, OUTPUT);        // initialize the LED1 pin as an output 
           pinMode(buttonPin1, INPUT);      // initialize the pushbutton pin as an input
           
           pinMode(ledPin2, OUTPUT);        // initialize the LED2 pin as an output:    
           pinMode(buttonPin2, INPUT);      // initialize the pushbutton pin as an input:
  
           pinMode(ledPin3, OUTPUT);        // initialize the LED3 pin as an output:    
           pinMode(buttonPin3, INPUT);      // initialize the pushbutton pin as an input:
           
/*  
  pinMode(ledPin4, OUTPUT);        // initialize the LED4 pin as an output:    
  pinMode(buttonPin4, INPUT);      // initialize the pushbutton pin as an input:
 */
}


     void loop() {
     
       // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
       if (buttonState1 == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin1, LOW);  
    
     motor1.run(RELEASE);      // stopped
     motor2.run(RELEASE);      // stopped
     motor3.run(RELEASE);      // stopped
     motor4.run(RELEASE);      // stopped
     delay(1000);
     
  } 
  else {
     digitalWrite(ledPin1, HIGH);
    
     motor1.run(BACKWARD);      // turn it on going backward
     motor1.setSpeed(255);
     motor2.run(BACKWARD);      // turn it on going backward
     motor2.setSpeed(255);
     motor3.run(BACKWARD);      // turn it on going backward
     motor3.setSpeed(225);
     motor4.run(BACKWARD);      // turn it on going backward
     motor4.setSpeed(255);
                               
     delay(1000);
    
    
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);
       if (buttonState2 == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin2, LOW);  
    
     motor1.run(RELEASE);      // stopped
     motor2.run(RELEASE);      // stopped
     motor3.run(RELEASE);      // stopped
     motor4.run(RELEASE);      // stopped
     delay(1000);
     
  } 
  else {
     digitalWrite(ledPin2, HIGH);
    
     motor1.run(FORWARD);      // turn it on going backward
     motor1.setSpeed(255);
     motor2.run(FORWARD);      // turn it on going backward
     motor2.setSpeed(255);
     motor3.run(FORWARD);      // turn it on going backward
     motor3.setSpeed(225);
     motor4.run(FORWARD);      // turn it on going backward
     motor4.setSpeed(255);
                               
     delay(1000);
    
    
  }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  // read the state of the pushbutton value:
  buttonState3 = digitalRead(buttonPin3);
       if (buttonState3 == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin3, LOW);  
    
     motor1.run(RELEASE);      // stopped
     motor2.run(RELEASE);      // stopped
     motor3.run(RELEASE);      // stopped
     motor4.run(RELEASE);      // stopped
     delay(1000);
     
  } 
  else {
     digitalWrite(ledPin3, HIGH);
    
     motor1.run(FORWARD);      // turn it on going backward
     motor1.setSpeed(255);
     motor2.run(FORWARD);      // turn it on going backward
     motor2.setSpeed(255);
     motor3.run(BACKWARD);      // turn it on going backward
     motor3.setSpeed(225);
     motor4.run(BACKWARD);      // turn it on going backward
     motor4.setSpeed(255);
                               
     delay(1000);

  }
}  

Friends please help me to solve this issue at coding too. I really need your help, I stucked at my project now :-(. Please check my code above ;and help me please.

I recorded this problem to video too : http://www.youtube.com/watch?v=HQpqamVd ... e=youtu.be
Last edited by adafruit_support_bill on Thu Apr 17, 2014 11:12 am, edited 1 time in total.
Reason: please use the code button when submitting code. press [Code], then paste your code between the [code] [/code] tags.

MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Re: Having problem at dc motor control with buttons at ardui

Post by MEKACI »

Friends, I am trying debounce but failing all the time...

if I write ,for example, only the forward command it works the way I want ;but when I add other 2 commands the robot becomes crazy :(. Who can show me how to craft my above code with debounce successfully ? thanks. I really need help , who can help me ? thanks.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Having problem at dc motor control with buttons at ardui

Post by adafruit_support_mike »

There are several ways to debounce buttons, but I like this method:

Code: Select all


byte button1 = 0;

void loop () {
    button1 = ( 2 * button1 ) + digitalRead( pin1 );
    
    if ( 0x7f == button1 ) {
        Serial.println( "Button 1 was just pressed" );
    }
    if ( 0xff == button1 ) {
        Serial.println( "Button 1 is HIGH" );
    }
    if ( 0x80 == button1 ) {
        Serial.println( "Button 1 was just released" );
    }
    if ( 0x00 == button1 ) {
        Serial.println( "Button 1 is LOW" );
    }
    delay( 10 );
}
The variable 'button1' acts as a shift register. Every pass through the loop, the current value is shifted one place to the left and the next button reading is added onto the end. That means the bits are time-based samples of the button state over a certain interval.. in this case, 80 milliseconds.

Each pattern can only occur if the button behaves a certain way over time. The 'just pressed' and 'just released' values only appear after any switch bounce has died away. The 'HIGH' and 'LOW' values only appear when the signal from the button is constant.

The method scales to any number of buttons, you just keep a shift register for each one.

MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Re: Having problem at dc motor control with buttons at ardui

Post by MEKACI »

Where should I put my motor related codes at your debounce buttons method ( because I want to apply your method with using 3 buttons ) ? I explained at " NOT " part below, the problems I have encountered yesterday ( I didn't try your debounce buttons method but I want to learn please help me, I am newbie so I am seeking help ;and this issue is not solving with reading the documents about debouncing with button which it will be work with my motor codes successfully, I need a sample example to learn this multi button coding with debouncing, please help me, thanks ).

Would you teach me how to apply my motor codes to your debounce buttons method without any problem ? I will look forward your reply to me, thanks.




NOT :
A not which is about what kind of problems I have encountered at my code:

I tried my code without delays of motors ;but It is still the same without delays . And now only last button works the way I want ;but others doesn't work ( okay leds are on and off when I press button ;but motors don't move at other 2 buttons and while pressing other 2 buttons they noise " tick tick ;or a strange noise ". How to fix it ? thanks.

I tried without debouncing ;but it gives only noise and " tick tick " or strange behaviours happening ( start to move after a while then stop then move again while holding the button; thus I was started to use debouncing but new problems occured now that I mentioned at my first paragraph above ). Friends Please help me to fix this issue, I really need your help, thanks again.

Would you make some adjustments on my codes please to solve and fix this issue ? thanks.
( first button is for tank movement forward; second button is for tank movement backward; third button is for tank body rotation to turn its around with using debouncing )

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Having problem at dc motor control with buttons at ardui

Post by adafruit_support_bill »

Would you make some adjustments on my codes please to solve and fix this issue ?
This request is incompatible with your stated desire to learn. It is also not our job to write custom code for people. And, as I mentioned in one of your other threads, we do provide sell or support DK-Electronics motor shields.

For what you are trying to do, you do not need debouncing. The problem with the code you have written is that you test all the buttons separately, but use each test to control all 4 motors. That is why some buttons interfere with the operation of others. What you need to do is:

Remove all the delays from your code. They do not help you.
Each time throug the loop:
* Read all 3 buttons and decide on a single action (forward/reverse/left/right)
* Set the motor states to implement that single action.

MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Re: Having problem at dc motor control with buttons at ardui

Post by MEKACI »

Sorry for writing this " Would you make some adjustments on my codes please to solve and fix this issue ? " ; I tried to code with debouncing the 3 buttons at the same time with my motor codes for 3 days , it exhausted me to see for my every try a new problem has occurred :(. I am newbie so I didn't want to learn a wrong time with with error and trials;so I was asked that question ( I didn't mean it in a different meaning )Anyway, I will work more harder and try more myself now.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”