Step #8 : motors & L298 board tests

arduino4fun
Site Admin
Posts: 37
Joined: Mon Jun 06, 2016 1:16 pm

Step #8 : motors & L298 board tests

Postby arduino4fun » Thu Jun 09, 2016 9:04 am

Motors & L298 board tests : This is a test of the motors & L298 board, it starts, runs for one meter, go back 50cm, turn left, go back some cm, go forward, turn right and advance for 50 cm to return to the origin point but at the other side, this is performed 2 times

video : https://youtu.be/SijllucXybM

Code: Select all

// ---------------------------------------------------------------------------------------------------------------
 // This sketch allows to check the L298 motors board and motors; the robot advance 1 meter forward, than it goes
 // back 50 cm backward, turn right, goes backward for 50 cm, goes forward 50 cm, turns right and goes forward
 // for 50 cm, it returns to the origin but in the other sense
 //
 // L298 ---- Sensor Shield V5
// ENA -- 5 - yellow
 // IN1 -- 2 - grey
 // IN2 -- 4 - green
 // IN3 -- 7 - mauve
 // IN4 -- 8 - orange
 // ENB -- 10 - blue
 // GND black
 // +5V red
 //
 //
 // https://www.facebook.com/arduino4fun
 // http://www.arduino4fun.com
 // ---------------------------------------------------------------------------------------------------------------

////////////////////////////////////////////////////////////
 int pinLB=2; // Steering wheel control ---- motor driver board IN1
 int pinLF=4; // Steering wheel control ---- motor driver board IN2
 int pinRB=7; // Steering wheel control ---- motor driver board IN3
 int pinRF=8; // Steering wheel control ---- motor driver board IN4

///////////Movements//////////////////////
 int Lpwm_pin = 10; // Motor vehicle speed control pin driver board ENB
 int Rpwm_pin = 5; // Motor vehicle speed control pin driver board ENA

////////////////////////////////
 int Car_state=0;
 int NUMBEROFLOOPS = 2;
 int CurrentLoop = 1;
 ////////////////////////////////////////////////////////////
 void forward() // Forward
 {
 digitalWrite(pinRB,HIGH); // The motor ( right rear ) Action
 digitalWrite(pinRF,LOW);
 digitalWrite(pinLB,HIGH); // The motor ( rear left ) Action
 digitalWrite(pinLF,LOW);
 Car_state = 1;
 show_state();
 }

void turnL() // Turn left
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,LOW); // The motor (front left ) Action
 Car_state = 4;
 show_state();
 }

void turnR() // Turn right

{
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,LOW); // The motor ( front right ) action
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 Car_state = 3;
 show_state();
 }

void stopp() // Stop
 {
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,HIGH);
 Car_state = 5;
 show_state();
 }

void backward() // Backward
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 Car_state = 2;
 show_state() ;
 }

void ConfigPins()
 {
 pinMode(pinLB,OUTPUT); // Pin 2
 pinMode(pinLF,OUTPUT); // Pin 4
 pinMode(pinRB,OUTPUT); // Pin 7
 pinMode(pinRF,OUTPUT); // Pin 8
 pinMode(Lpwm_pin,OUTPUT); // Pin 10 (PWM)
 pinMode(Rpwm_pin,OUTPUT); // Pin 5 (PWM)
 }

void Set_Speed(unsigned char Left,unsigned char Right)
 {
 analogWrite(Lpwm_pin,Left);
 analogWrite(Rpwm_pin,Right);
 }

void show_state(void)
 {
 Serial.print("State : ");
 switch(Car_state)
 {
 case 1:
 //lcd.print(" Go ");
 Serial.print("Go");
 break;
 case 2:
 //lcd.print(" Back ");
 Serial.print("Back");
 break;
 case 3:
 //lcd.print(" Left ");
 Serial.print("Left");
 break;
 case 4:
 //lcd.print(" Right");
 Serial.print("Right");
 break;
 case 5:
 //lcd.print(" stop ");
 Serial.print("Stop");
 break;
 default:
 break;
 }
 Serial.println();
 }

void setup()
 {
 Serial.begin(9600);
 ConfigPins();
 delay(500);
 Set_Speed(3000,3000);
 }

void loop()
 {
 if( CurrentLoop>NUMBEROFLOOPS )
 {
 //stopp();
 //Serial.print("Max loops reached");
 //Serial.println();
 return;
 }
 Serial.print("Loop #");
 Serial.print(CurrentLoop);
 Serial.println();

forward() ;
 delay(2200);

backward() ;
 delay(1000);

turnR() ;
 delay(800);

backward() ;
 delay(1000);

forward() ;
 delay(1000);

turnR() ;
 delay(800);

forward() ;
 delay(1000);

stopp();
 //delay(2000);
 CurrentLoop++;
 }


Image
Image
Image
Image
Image
Image

Return to “Multifunction Bluetooth Controlled Robot Smart Car Kits For Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest