I've been messing around with an obstacle avoidance robot. I picked up a lil'kit of Ebay for the 2WD version,It came with motors an a motor shield(L298N). I have a UNO, sensor shieldV4.0 , a couple ultrasound sensors(4pin type)
Playing with various bits of code ,I found nothing to fit.
So.I started to mess around on my own.
I guess the first thing a robot needs to do is move...of course...So I established the various motion patterns.I put a "halt" pattern in ,because or house is multilevel and I don't need this bot driving over the stairs.
//establish relative motion and turn routine
int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
#define leftturn
#define rightturn
#define forward
#define reverse
#define halt
void setup()
{
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop()
{
analogWrite(ENA,255);//start driving motorA
analogWrite(ENB,255);//start driving motorB
rightturn;
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);//setting motorB's directon
delay (3000);
leftturn;
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
delay (3000);
forward;
digitalWrite(ENA,LOW);
digitalWrite(ENA,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
delay(3000);
reverse;
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
delay (3000);
halt;
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
delay (3000);
}
My guess is that this bit of code will not be in the loop but in setup with out delays when it comes time to get this thing looking for walls and drop off's