0% found this document useful (0 votes)
30 views2 pages

Arduino Motor Control Code Example

The document contains Arduino code for controlling a robot's movement, including functions to set speeds for left and right motors, and to move forward, backward, turn left, and turn right. The setup function initializes the motor control pins, while the loop function executes a sequence of movements with delays. Each movement function adjusts motor speeds based on the input speed parameter.

Uploaded by

duc.bui2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Arduino Motor Control Code Example

The document contains Arduino code for controlling a robot's movement, including functions to set speeds for left and right motors, and to move forward, backward, turn left, and turn right. The setup function initializes the motor control pins, while the loop function executes a sequence of movements with delays. Each movement function adjusts motor speeds based on the input speed parameter.

Uploaded by

duc.bui2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

void setup () {

for(int i=8;i<14;i++)
{pinMode (i, OUTPUT);}
}
void left_speed ( int speed ) {
// TODO : Implement your code here
if( speed > 0)
{
digitalWrite (8,HIGH);
digitalWrite (11,LOW);
analogWrite (9 , speed) ;
}
else
{
speed = 0 - speed ;
digitalWrite (8 , LOW ) ;
digitalWrite (11 , HIGH ) ;
analogWrite (9 , speed) ;
}
}
void right_speed ( int speed ) {
// TODO : Implement your code here
if( speed > 0)
{
digitalWrite (13,HIGH);
digitalWrite (12,LOW);
analogWrite (10 , speed) ;
}
else
{
speed = 0 - speed ;
digitalWrite (13 , LOW ) ;
digitalWrite (12 , HIGH ) ;
analogWrite (10 , speed) ;
}
}

void forward (int speed ) {


// TODO
right_speed(speed);
left_speed(speed);
}

void backward (int speed ) {


// TODO
if(speed>0){speed=-speed;}
right_speed(speed);
left_speed(speed);
}

void turnleft (int speed ) {


// TODO
right_speed(speed);
left_speed(0);
}

void turnright (int speed ) {


// TODO
right_speed(0);
left_speed(speed);
}

void loop ()
{
forward(100);
delay(2000);
backward(100);
delay(2000);
turnleft(100);
delay(2000);
turnright(100);
delay(2000);
}

You might also like