code for robotics
code for robotics
#define IR_SENSOR_LEFT 13
#define MOTOR_SPEED 180 pinMode(IR_SENSOR_RIGHT, INPUT);
pinMode(IR_SENSOR_LEFT, INPUT);
//Right motor rotateMotor(0,0);
int enableRightMotor=6; }
int rightMotorPin1=7;
int rightMotorPin2=8;
void loop()
//Left motor {
int enableLeftMotor=5;
int leftMotorPin1=9; int rightIRSensorValue =
int leftMotorPin2=10; digitalRead(IR_SENSOR_RIGHT);
int leftIRSensorValue =
void setup() digitalRead(IR_SENSOR_LEFT);
{
//The problem with TT gear motors is Serial.print("IR_SENSOR_RIGHT: ");
that, at very low pwm value it does not Serial.println(IR_SENSOR_RIGHT);
even rotate. Serial.print("IR_SENSOR_LEFT: ");
//If we increase the PWM value then it Serial.println(IR_SENSOR_LEFT);
rotates faster and our robot is not delay(1000);
controlled in that speed and goes out of
line. //If none of the sensors detects black
//For that we need to increase the line, then go straight
frequency of analogWrite. if (rightIRSensorValue == LOW &&
//Below line is important to change the leftIRSensorValue == LOW)
frequency of PWM signal on pin D5 and {
D6 rotateMotor(MOTOR_SPEED,
//Because of this, moto3r runs in MOTOR_SPEED);
controlled manner (lower speed) at high }
PWM value. //If right sensor detects black line, then
//This sets frequecny as 7812.5 hz. turn right
TCCR0B = TCCR0B & B11111000 | else if (rightIRSensorValue == HIGH &&
B00000010 ; leftIRSensorValue == LOW )
{
// put your setup code here, to run once: rotateMotor(-MOTOR_SPEED,
pinMode(enableRightMotor, OUTPUT); MOTOR_SPEED);
pinMode(rightMotorPin1, OUTPUT); }
pinMode(rightMotorPin2, OUTPUT); //If left sensor detects black line, then
turn left
pinMode(enableLeftMotor, OUTPUT); else if (rightIRSensorValue == LOW &&
pinMode(leftMotorPin1, OUTPUT); leftIRSensorValue == HIGH )
{ digitalWrite(leftMotorPin2,LOW);
rotateMotor(MOTOR_SPEED, }
-MOTOR_SPEED); else
} {
//If both the sensors detect black line, digitalWrite(leftMotorPin1,LOW);
then stop digitalWrite(leftMotorPin2,LOW);
else }
{ analogWrite(enableRightMotor,
rotateMotor(0, 0); abs(rightMotorSpeed));
} analogWrite(enableLeftMotor,
abs(leftMotorSpeed));
} }
if (rightMotorSpeed < 0)
{
digitalWrite(rightMotorPin1,LOW);
digitalWrite(rightMotorPin2,HIGH);
}
else if (rightMotorSpeed > 0)
{
digitalWrite(rightMotorPin1,HIGH);
digitalWrite(rightMotorPin2,LOW);
}
else
{
digitalWrite(rightMotorPin1,LOW);
digitalWrite(rightMotorPin2,LOW);
}
if (leftMotorSpeed < 0)
{
digitalWrite(leftMotorPin1,LOW);
digitalWrite(leftMotorPin2,HIGH);
}
else if (leftMotorSpeed > 0)
{
digitalWrite(leftMotorPin1,HIGH);
\