Bài N P Arduino - Part 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

BÀI NỘP ARDUINO - PART 2

Bài 3.4:
Bài code:
// Enable 3,4: 9
// Input 3: 11, Input 4: 8;
// Enable 1,2: 10
// Input 1: 12, Input 2: 13;

void setup(){
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void left_motor(int speed){


if(speed>0){
analogWrite(9 ,speed);
digitalWrite(11, HIGH);
digitalWrite(8, LOW);
}
else{
speed=0-speed;
analogWrite(9, speed);
digitalWrite(11, LOW);
digitalWrite(8, HIGH);

}
}

void right_motor(int speed){


if(speed>0){
analogWrite(10 ,speed);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
}
else{
speed=0-speed;
analogWrite(10 ,speed);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}
}

void forward(int speed){


left_motor(speed);
right_motor(speed);
delay(2000);
left_motor(0);
right_motor(0);
delay(2000);
}

void backward(int speed){


left_motor(0-speed);
right_motor(0-speed);
delay(2000);
left_motor(0);
right_motor(0);
delay(2000);
}

void turn_right(int speed){


left_motor(speed);
delay(2000);
left_motor(0);
delay(2000);
}

void turn_left(int speed){


right_motor(speed);
delay(2000);
right_motor(0);
delay(2000);
}
Bài 3.5:
Link Google Drive

You might also like