Step #8-2 : motors & L298 board tests

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

Step #8-2 : motors & L298 board tests

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

Motors & L298 board tests : this is an updated version of the code.

Code: Select all

// ---------------------------------------------------------------------------------------------------------------
 // this sketch is an updated version.
 // This sketch allows to check the L298 motors board and motors, the robot run 1 meter forward and goes back
 // 1 meter backward
 //
 // 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 = 1;
 int CurrentLoop = 1;
 ////////////////////////////////////////////////////////////
 void forward(int val) // Forward
 {
 digitalWrite(pinRB,HIGH); // The motor ( right rear ) Action
 digitalWrite(pinRF,LOW);
 digitalWrite(pinLB,HIGH); // The motor ( rear left ) Action
 digitalWrite(pinLF,LOW);
 delay( val * 100);
 Car_state = 1;
 show_state();
 }

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

void turnR(int val) // Turn right
 {
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,LOW); // The motor ( front right ) action
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = 3;
 show_state();
 }

void stopp(int val=0) // Stop
 {
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = 5;
 show_state();
 }

void backward(int val) // Backward
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 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(22) ;

backward(10) ;

turnR(8) ;

backward(10) ;

forward(10) ;

turnR(8) ;

forward(10) ;

stopp();
 CurrentLoop++;
 }

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

Who is online

Users browsing this forum: No registered users and 1 guest