Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Use this code to test your motor with the Arduino board:
- // if you need PWM, just use the PWM outputs on the Arduino
- // and instead of digitalWrite, you should use the analogWrite command
- // --------------------------------------------------------------------------- Motors
- int motor_left[] = {2, 3};
- int motor_right[] = {7, 8};
- int enable_pin_left = 3;
- int enable_pin_right = 5;
- // --------------------------------------------------------------------------- Setup
- void setup() {
- Serial.begin(9600);
- // Setup motors
- int i;
- for(i = 0; i < 2; i++){
- pinMode(motor_left[i], OUTPUT);
- pinMode(motor_right[i], OUTPUT);
- pinMode(enable_pin_left, OUTPUT);
- pinMode(enable_pin_right, OUTPUT);
- }
- }
- // --------------------------------------------------------------------------- Loop
- void loop() {
- analogWrite(enable_pin_left, 80);
- analogWrite(enable_pin_right, 80);
- drive_forward();
- delay(5000);
- drive_backward();
- delay(5000);
- }
- // --------------------------------------------------------------------------- Drive
- void motor_stop(){
- digitalWrite(motor_left[0], LOW);
- digitalWrite(motor_left[1], LOW);
- digitalWrite(motor_right[0], LOW);
- digitalWrite(motor_right[1], LOW);
- delay(25);
- }
- void drive_forward(){
- digitalWrite(motor_left[0], HIGH);
- digitalWrite(motor_left[1], LOW);
- digitalWrite(motor_right[0], HIGH);
- digitalWrite(motor_right[1], LOW);
- }
- void drive_backward(){
- digitalWrite(motor_left[0], LOW);
- digitalWrite(motor_left[1], HIGH);
- digitalWrite(motor_right[0], LOW);
- digitalWrite(motor_right[1], HIGH);
- }
- void turn_left(){
- digitalWrite(motor_left[0], LOW);
- digitalWrite(motor_left[1], HIGH);
- digitalWrite(motor_right[0], HIGH);
- digitalWrite(motor_right[1], LOW);
- }
- void turn_right(){
- digitalWrite(motor_left[0], HIGH);
- digitalWrite(motor_left[1], LOW);
- digitalWrite(motor_right[0], LOW);
- digitalWrite(motor_right[1], HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement