// Use this code to test your motor with the Arduino board:
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// --------------------------------------------------------------------------- Motors
int motor_left[] = {3,8};
int motor_right[] = {12, 7};
// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
// --------------------------------------------------------------------------- Loop
void loop() {
drive_forward();
delay(1000);
motor_stop();
Serial.println("1");
drive_backward();
delay(1000);
motor_stop();
Serial.println("2");
turn_left();
delay(1000);
motor_stop();
Serial.println("3");
turn_right();
delay(1000);
motor_stop();
Serial.println("4");
motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
}
// --------------------------------------------------------------------------- Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
------------------------------------------------------------------------------------------
IRRELEVANT
Beside this code u can also take help
// Use this code to test
your motor with the Arduino board:
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// --------------------------------------------------------------------------- Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
// --------------------------------------------------------------------------- Loop
void loop() {
drive_forward();
delay(1000);
motor_stop();
Serial.println("1");
drive_backward();
delay(1000);
motor_stop();
Serial.println("2");
turn_left();
delay(1000);
motor_stop();
Serial.println("3");
turn_right();
delay(1000);
motor_stop();
Serial.println("4");
motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
}
// --------------------------------------------------------------------------- Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// --------------------------------------------------------------------------- Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
// --------------------------------------------------------------------------- Loop
void loop() {
drive_forward();
delay(1000);
motor_stop();
Serial.println("1");
drive_backward();
delay(1000);
motor_stop();
Serial.println("2");
turn_left();
delay(1000);
motor_stop();
Serial.println("3");
turn_right();
delay(1000);
motor_stop();
Serial.println("4");
motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
}
// --------------------------------------------------------------------------- Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
For IR sensor
int analogPin = 3; //
Infrared Sensor (Right lead) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
Program for Forward motion when Infra Red Sensor
Value is smaller than 100
// This is the basic code which runs a loop
that makes the Robot go straight forward when the Infrared value is below 100.
int analogPin = 3; // Infrared Sensor (Right lead) connected to analog pin 3
// Infrared Sensor(middle Lead) connected to +5V
// Infrared Sensor(Left Lead) connected to ground
int val = 0; // variable to store the value read
int LeftMotorForward = 10; // Pin 10 has Left Motor connected on Arduino boards.
int LeftMotorReverse = 9; // Pin 9 has Left Motor connected on Arduino boards.
int RightMotorForward = 12; // Pin 12 has Right Motor connected on Arduino boards.
int RightMotorReverse = 13; // Pin 13 has Right Motor connected on Arduino boards.
//------------------------------------------------------------------------------------------------
void setup()
{
pinMode(LeftMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(LeftMotorReverse, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorReverse, OUTPUT); // initialize the pin as an output.
Serial.begin(9600); // setup serial
}
// The following Loop is to make the Robot go staright when the Infrared value is below 100
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
if (val <100)
// If the read value is < 100 then run the code below which will turn both Left and Right Motor on making the Robot go straight.
{
digitalWrite(RightMotorForward, HIGH); // turn the Right Motor ON
digitalWrite(LeftMotorForward, HIGH); // turn the Left Motor ON
delay(10000); // wait for 10 seconds
digitalWrite(RightMotorForward,LOW); // turn the Right Motor OFF
digitalWrite(LeftMotorForward, LOW); // turn the Left Motor OFF
delay(10000); // wait for 10 seconds
}
}
int analogPin = 3; // Infrared Sensor (Right lead) connected to analog pin 3
// Infrared Sensor(middle Lead) connected to +5V
// Infrared Sensor(Left Lead) connected to ground
int val = 0; // variable to store the value read
int LeftMotorForward = 10; // Pin 10 has Left Motor connected on Arduino boards.
int LeftMotorReverse = 9; // Pin 9 has Left Motor connected on Arduino boards.
int RightMotorForward = 12; // Pin 12 has Right Motor connected on Arduino boards.
int RightMotorReverse = 13; // Pin 13 has Right Motor connected on Arduino boards.
//------------------------------------------------------------------------------------------------
void setup()
{
pinMode(LeftMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(LeftMotorReverse, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorReverse, OUTPUT); // initialize the pin as an output.
Serial.begin(9600); // setup serial
}
// The following Loop is to make the Robot go staright when the Infrared value is below 100
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
if (val <100)
// If the read value is < 100 then run the code below which will turn both Left and Right Motor on making the Robot go straight.
{
digitalWrite(RightMotorForward, HIGH); // turn the Right Motor ON
digitalWrite(LeftMotorForward, HIGH); // turn the Left Motor ON
delay(10000); // wait for 10 seconds
digitalWrite(RightMotorForward,LOW); // turn the Right Motor OFF
digitalWrite(LeftMotorForward, LOW); // turn the Left Motor OFF
delay(10000); // wait for 10 seconds
}
}
program when Infrared Red sensor Value is smaller than 100
When IR Value is Less than 100 then the Motors
are turned on and the Robot drives forward for 10 seconds.
No comments:
Post a Comment