0% found this document useful (0 votes)
7 views

code for robotics

The document contains code for controlling a robot's motors using infrared sensors to detect black lines. It defines pin configurations, motor speed settings, and logic for motor rotation based on sensor input. The setup includes adjusting PWM frequency for better motor control and implementing conditions for turning or stopping the robot based on sensor readings.

Uploaded by

gwynethredondo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

code for robotics

The document contains code for controlling a robot's motors using infrared sensors to detect black lines. It defines pin configurations, motor speed settings, and logic for motor rotation based on sensor input. The setup includes adjusting PWM frequency for better motor control and implementing conditions for turning or stopping the robot based on sensor readings.

Uploaded by

gwynethredondo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#define IR_SENSOR_RIGHT 12 pinMode(leftMotorPin2, OUTPUT);

#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));
} }

void rotateMotor(int rightMotorSpeed, int


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);
\

You might also like