Robot 1

/*Robot 1

This is the first program for running the simple robot.
We just want to make sure the motors spin the right and left correctly
and the robot moves forward .

Note the use of the ena, enb. In the old version of L298, there were 6 inputs. ena and enb
are set to HIGH. these can be set by hardware and hence are commented out.

Depending on your driver, you can compile these commands or not. Use find
and replace to remove the comment out directive if you wish.
*/


#define trigPin 13
#define echoPin 12

// int ena = 4;
int int1 = 5;
int int2 = 6;

// int enb = 8;
int int3 =9;
int int4 = 10;

int timex = 3000; //global variable runs for 3 seconds
boolean run;

void setup() {
//pinMode (ena,OUTPUT);
pinMode (int1,OUTPUT);
pinMode (int2,OUTPUT);

//pinMode (enb,OUTPUT);
pinMode (int3,OUTPUT);
pinMode (int4,OUTPUT);


Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}


void loop() {
// parameter passed as a value
forward(100); // sets the robot forward. 
pinger();
}

void pinger() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance);
if (distance < 10){
halt(100);
reverse(1000);
halt(100);
right(500);
halt(100); 
}
}

void reverse(int wait){ // reverse sub procedure
//digitalWrite(ena,HIGH);
digitalWrite(int1,LOW);
digitalWrite(int2,HIGH);

//digitalWrite(enb, HIGH);
digitalWrite(int3,LOW);
digitalWrite(int4,HIGH);

delay(wait);
}

void forward(int wait){ // forward sub procedure
//digitalWrite(ena,HIGH);
digitalWrite(int1,HIGH);
digitalWrite(int2,LOW);

//digitalWrite(enb, HIGH);
digitalWrite(int3,HIGH);
digitalWrite(int4,LOW);

delay(wait);

}
void halt(int wait) // halt sub proceedure
{
//digitalWrite(ena,HIGH);
digitalWrite(int1,LOW);
digitalWrite(int2,LOW);

//digitalWrite(enb,HIGH);
digitalWrite(int3,LOW);
digitalWrite(int4,LOW);
delay(wait);
}


void right(int wait){
//digitalWrite(ena, HIGH);
digitalWrite(int1,LOW);
digitalWrite(int2,HIGH);

//digitalWrite(enb, HIGH);
digitalWrite(int3,HIGH);
digitalWrite(int4,LOW);

delay(wait);
}

void left(int wait){
//digitalWrite(ena, HIGH);
digitalWrite(int1,HIGH);
digitalWrite(int2,LOW);

//digitalWrite(enb, HIGH);
digitalWrite(int3,LOW);
digitalWrite(int4,HIGH);

delay(wait);
}

Last modified: Tuesday, 31 May 2016, 9:35 PM