Hi! I’m new in arduino and things involved so please be patient
I have to do a project that consists in building a machine capable to follow a line, to detect obstacles and avoid them. I buyed this stuff some time ago
http://www.ebay.com/itm/Remote-Control-Trace-Obstacle-Avoidance-Intelligent-Car-for-Arduino-/130948382242?pt=LH_DefaultDomain_0&hash=item1e7d21be22
After many hours of reading topics and tutorials, I wrote the following code:
#include <Servo.h>
// define motor pins
int MD1 = 6; // right motor 1
int MD2 = 2; // right motor 2
int MS1 = 3; // left motor 1
int MS2 = 4; // left motor 2
int ENA = 10; // enable MD1 & MD2
int ENB = 11; // enable MS1 & MS2
// define sensor pins
const int SenzorStanga=7; // SENZOR left
const int SenzorDreapta=8; // SENZOR right
const int SenzorFata=12; // SENZOR front
int SSt; // SENZOR state left
int SDr; // SENZOR state right
int SF; // SENZOR state front
//define ultrasonic sensor and servo
int trig = A1;
int echo = A0;
int DF = 0; // front distance
int DDr = 0; // front distance
int DSt = 0; // front distance
Servo servodetect;
void setup()
{
Serial.begin(9600);
pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);
pinMode (MD1, OUTPUT);
pinMode (MD2, OUTPUT);
pinMode (MS1, OUTPUT);
pinMode (MS2, OUTPUT);
pinMode (SS, INPUT);
pinMode (SDr, INPUT);
pinMode (SF, INPUT);
pinMode (trig, INPUT);
pinMode (echo, OUTPUT);
servodetect.attach(9);
}
void loop()
{
mfata(1);
mspate(1);
mdreapta(1);
mstanga(1);
}
and the move code. mdreapta=move right, mstanga=move left
// define moves
void mfata (int a)
{
analogWrite (ENA, 150);
analogWrite (ENB, 150);
digitalWrite (MD1, LOW);
digitalWrite (MD2, HIGH);
digitalWrite (MS1, HIGH);
digitalWrite (MS2, LOW);
delay (a * 1000);
}
void mspate(int b)
{
analogWrite (ENA, 150);
analogWrite (ENB, 150);
digitalWrite (MD1, HIGH);
digitalWrite (MD2, LOW);
digitalWrite (MS1, LOW);
digitalWrite (MS2, HIGH);
delay (b * 1000);
}
void mdreapta(int c)
{
analogWrite (ENA, 250);
digitalWrite (MD1, LOW);
digitalWrite (MD2, HIGH);
analogWrite (ENB, 250);
digitalWrite (MS1, LOW);
digitalWrite (MS2, HIGH);
delay (c * 1000);
}
void mstanga(int d)
{
analogWrite (ENA, 250);
digitalWrite (MD1, HIGH);
digitalWrite (MD2, LOW);
analogWrite (ENB, 250);
digitalWrite (MS1, HIGH);
digitalWrite (MS2, LOW);
delay (d * 1000);
}
also tryed this for left and right
void mdreapta(int c)
{
analogWrite (ENA, 255);
analogWrite (ENB, 255);
digitalWrite (MD1, LOW);
digitalWrite (MD2, HIGH);
digitalWrite (MS1, LOW);
digitalWrite (MS2, LOW);
delay (c*1000);
digitalWrite (MD1, LOW);
digitalWrite (MD2, LOW);
digitalWrite (MS1, LOW);
digitalWrite (MS2, HIGH);
//delay (c * 2000);
delay (c*1000);
}
void mstanga(int d)
{
analogWrite (ENA, 255);
analogWrite (ENB, 255);
digitalWrite (MD1, HIGH);
digitalWrite (MD2, LOW);
digitalWrite (MS1, LOW);
digitalWrite (MS2, LOW);
delay (d*1000);
digitalWrite (MD1, LOW);
digitalWrite (MD2, LOW);
digitalWrite (MS1, HIGH);
digitalWrite (MS2, LOW);
//delay (d * 2000);
delay (d*1000);
}
but still don’t work
As you can see in the code, i didn’t used any sensor or servo till now, i just defined them cuz i inted to use them later.
My problem occurs when the car have to execute the cornering lines. I do not know if the 4 engines do not get enough power or is the code wrong, but when the left or right command runs, the car barely moves.
For power i use 6 x AA 1.5V duracel. I also tried to put arduino board to the 9V battery and the motor shield to the 6xAA, with the same result.
here’s avideo with the car in “action”:
What i’m doing wrong and how to resolve this?
PS: please excuse my grammar mistakes