Go Down

Topic: noob motor control (Read 500 times) previous topic - next topic

paul259

hi all

moved from lego nxt to arduino a couple of weeks ago, so plodding along on a steep learning curve  XD.

built a 4 motor tracked chassis and have it working using the following code:-

// Through serial monitor, type 'a'= left,'s'= stop, 'w'= foward,'d'= right,'x'= reverse to control the motor
// connect apc220 after sketch upload to control wirelessly through serial monitor


int EN1 = 9;  //speed control
int EN2 = 11; //speed control
int EN3 = 6;  //speed control
int EN4 = 10; //speed control

int IN1 = 3;  //direction
int IN2 = 5;  //direction
int IN3 = 7;  //direction
int IN4 = 2;  //direction



void Motor1(int pwm, boolean reverse)
        {
          analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         {
          digitalWrite(IN1,HIGH); 
         }
        else
        {
          digitalWrite(IN1,LOW);   
         }
        } 
       
void Motor2(int pwm, boolean reverse)
        {
          analogWrite(EN2,pwm);
         if(reverse)
         {
          digitalWrite(IN2,HIGH);   
         }
        else
        {
          digitalWrite(IN2,LOW);   
         }
        } 
void Motor3(int pwm, boolean reverse)
        {
          analogWrite(EN3,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         {
          digitalWrite(IN3,HIGH);   
         }
        else
        {
          digitalWrite(IN3,LOW);   
         }
        }
void Motor4(int pwm, boolean reverse)
        {
          analogWrite(EN4,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         {
          digitalWrite(IN4,HIGH);   
         }
        else
        {
          digitalWrite(IN4,LOW);   
         }
        }   
       
void setup()
{
    int i;
   
    for(i=2;i<=11;i++)   //Motor Shield pin settings
    pinMode(i, OUTPUT);  //set pin 2,3,5,6,7,9,10,11 to output mode

    Serial.begin(9600);   
}


void loop()
{
  int x,delay_en;
  char val;
  while(1)
  {
    val = Serial.read();
    if(val!=-1)
       {
          switch(val)
           {
             case 'w'://Move ahead
                        Motor1(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                         break;
             case 'x'://move back
                        Motor1(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                         break;
             case 'a'://turn left
                        Motor1(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                         break;       
             case 'd'://turn right
                        Motor1(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        break;   
               case 's'://stop
                        Motor1(0,false);
                        Motor2(0,false);
                        Motor3(0,false);
                        Motor4(0,false);
                         break;
                                 
           }     
       
       }
           
  }                           
}
the board on the chassis is arduino mega 1280 i also have a arduino duemilanove and a dfrobot input shield http://www.droboticsonline.com/index.php/arduino/arduino-shields/arduino-input-shield.html anybody help to make these a remote for the chassis using the apc220 for wireless control.

ps i can already remote control the chassis with apc220 using my laptop

any help would be gratefuly recieved
only happy when tinkering

Msquare

Welcome. Code looks alright a couple of minor suggestions. (And see, my code is in code boxes (which stops the smileys appear instead of some seperators) which you do by using the #-button above, whne the code text is selected. (or you learnt to type the [-bracket syntax direct after a while)

The comment turning the pins on is missing the 8 :)

The way to do serial is
Code: [Select]
if (Serial.available() > 0 ) {
  val = Serial.read() ;
  :
  }


I think the motor1 motor2 etc code could be made neater with only one routine and an array, but that is just esthetics - your code works for you.

But what exactly do you want help with? Just a general direction of what to do or the tricky bit about sampling sensor input from the joystick and controlling the motors?

paul259

thank you very much for the quick reply.

i will take on board your comments and learn from them so i can tidy up my code.

as for the remote joystick i realy have no clue how to even get started i had the idea of the joystick copying the w,x,a,d,s direction ie if i push forward on the stick it would send the w command to the chassis but i had no luck at all trying to do this and i guess its a crappy way anyway :smiley-red: i am realy at a loss on writting code to achieve my aim.
only happy when tinkering

PaulS

What kind of motor controller board are you using? Typically, a motor controller can do more than turn the motor off or on. It can also vary the speed of the motors.

Since the joystick outputs varying values depending on how far it is pushed or pulled, you can use the analog value read to control the speed of the motors.

First, learn how to connect the joystick, and read the various values as you move the stick around. When you can do that, use the map function to map the joystick values from their range of values to the range of values that the PWM pins that the motor controller are connected to expects. Then, send the map() output to the PWM pin, with the enable pin set appropriately (to go forward or backwards at different speeds).

Go Up