Our next step is to code our test chassis to drive around & detects objects on its way and the smart enough to drive around them. For this purpose we have bough 4 reflex detectors to have around our chassis. More updates on the way ... Click on read more for the source code in Semi C++ Arduino programing language.
int pin_7 = 7;
int sens_r = 0;
int sens_l = 1;
int velocityPin = 5;
int velocity;
boolean motorEnable = false;
//MOTOR#2 -> LEFT & RIGHT
//MOTOR#1 -> FORWARD & BACKWARD
//OUTPUT PINS
byte IN1M1 = 2;//IN1 Motor1
byte IN2M1 = 4;//IN2 Motor1
byte IN1M2 = 11;//IN1 Motor2
byte IN2M2 = 9;//IN2 Motor2
byte ENM1 = 5;//enable signal Motor1
byte ENM2 = 10;//enable signal Motor2
byte PWMM1 = 3; //PWM Motor1
byte PWMM2 = 8; //PWM Motor2
byte ledPin = 13; // LED connected to digital pin 13
int rightSensor, leftSensor;
int order = 0;
void setup(){
pinMode(pin_7, INPUT);
pinMode(sens_r, INPUT);
pinMode(sens_l, INPUT);
pinMode(velocityPin, INPUT);
pinMode(IN1M1, OUTPUT); // MOTOR1 IN1
pinMode(IN2M1, OUTPUT); // MOTOR1 IN2
pinMode(IN1M2, OUTPUT); // MOTOR2 IN1
pinMode(IN2M2, OUTPUT); // MOTOR2 IN2
pinMode(ENM1, OUTPUT); // MOTOR1 Enable
pinMode(ENM2, OUTPUT); // MOTOR2 Enable
pinMode(PWMM1, OUTPUT); // MOTOR 1 PWM
pinMode(PWMM2, OUTPUT); // MOTOR 2 PWM
pinMode(ledPin, OUTPUT); // LED
digitalWrite(ENM1, HIGH);
Serial.begin(115200);
order = 0;
}
void loop(){
//Value_ = digitalRead(pin_7);
velocity = analogRead(velocityPin)/4;
rightSensor = analogRead(sens_r);
leftSensor = analogRead(sens_l);
digitalWrite(IN1M1, LOW);
digitalWrite(IN2M1, HIGH);
analogWrite(PWMM1, velocity);
digitalWrite(ledPin, HIGH);
/*Serial.print("Right sensor is :");
Serial.println(rightSensor);
Serial.print("Left sensor is :");
Serial.println(leftSensor);*/
// Serial.print("Velocity is :");
// Serial.println(velocity);
//delay(200);
if(rightSensor >220 && leftSensor > 220){
analogWrite(PWMM1, velocity+70);
order = 3;
}
else if ((rightSensor < 60) && (leftSensor < 60)){
makeItStraight();
// Serial.println("*** BOTH ON BLACK ***");
}
else if ((rightSensor < 60) && (leftSensor > 60)){//RIGHT BLACK
// Serial.print("Left sensor is ON WHITE");
// Serial.println(leftSensor);
order = 2;
}
else if ((rightSensor > 60) && (leftSensor < 60)){//LEFT BLACK
// Serial.print("Right sensor is ON WHITE");
// Serial.println(rightSensor);
order = 3;
}
else if ((rightSensor > 60) && (leftSensor > 60)){//ALL WHITE
makeItStraight();
// Serial.println("*** BOTH ON WHITE ***");
/*Serial.println("Both sensors are highter than 200 -> the values are :");
Serial.print("Left sensor is :");
Serial.println(rightSensor);
Serial.print("Right sensor is : ");
Serial.println(leftSensor);
//order = 4;*/
}
comm();
order = 0;
// delay(50);
}
//***********************************
void comm()
{
switch(order)
{
case 4://Forward, F pressed
// Serial.println( "F pressed" );
digitalWrite(IN1M1, LOW);
digitalWrite(IN2M1, HIGH);
digitalWrite(ENM1, HIGH);
//digitalWrite(PWMM1,HIGH);
analogWrite(PWMM1, velocity);
digitalWrite(ledPin, HIGH);
break;
case 1://Back, B pressed
// Serial.println( "B pressed" );
digitalWrite(IN1M1, HIGH);
digitalWrite(IN2M1, LOW);
digitalWrite(ENM1, HIGH);
analogWrite(PWMM1, velocity);
//digitalWrite(PWMM1,HIGH);
digitalWrite(ledPin, HIGH);
break;
case 2://Right, R pressed
// Serial.println( "R pressed" );
digitalWrite(IN1M2, HIGH);
digitalWrite(IN2M2, LOW);
digitalWrite(ENM2, HIGH);
analogWrite(PWMM2, 200);
digitalWrite(ledPin, HIGH);
break;
case 3://Left, L pressed
// Serial.println( "L pressed" );
digitalWrite(IN1M2, LOW);
digitalWrite(IN2M2, HIGH);
digitalWrite(ENM2, HIGH);
analogWrite(PWMM2, 200);
digitalWrite(ledPin, HIGH);
break;
}
}
//***************** COMM *****************************
//********************************************************
//***************** STOPCAR ******************************
void stopCar(){
digitalWrite(13, LOW);
digitalWrite(ENM1, LOW);
digitalWrite(ENM2, LOW);
order = 0 ;
Serial.println( "Reseting the system");
//delay(500);
}//end of stop
//************* STOPCAR ******************************
//****************************************************
//************* makeItStraight ***********************
void makeItStraight(){
digitalWrite(ENM2, HIGH);
digitalWrite(IN1M2, LOW);
digitalWrite(IN2M2, LOW);
digitalWrite(ENM2, LOW);
Serial.println( "Straighting the wheels");
//delay(500);
}//end of stop
No comments:
Post a Comment