Basically right now i'm working on a project where i need to build a robot crane with a claw attached to it that has 2 function. The first one is, the car have a line tracking function which i use IR sensors. The second function is a manual control function in which it will be controlled via bluetooth. For the claw/arm i have used servo motors. i have already done with the sketches for both function. is there a way where i can combine the two sketches and switch between them to function one at a time.
this is the first sketch ,the line tracking
int IRsensor1 = A0; //LEFT SENSOR
int IRsensor2 = A1; // CENTER SENSOR
int IRsensor3 = A2; // RIGHT SENSOR
int Sensorvalue1 = 0;
int Sensorvalue2 = 0;
int Sensorvalue3 = 0;
int MotorA = 12;
int EA = 10;
int MotorB = 13;
int EB = 11;
void setup() {
pinMode(IRsensor1,INPUT);
pinMode(IRsensor2,INPUT);
pinMode(IRsensor3,INPUT);
pinMode(MotorA, OUTPUT);
pinMode(MotorB, OUTPUT);
Serial.begin(9600);
Serial.println("Program Starting");
delay(100);
}
void loop() {
Sensorvalue1= analogRead(IRsensor1);
Sensorvalue2= analogRead(IRsensor2);
Sensorvalue3= analogRead(IRsensor3);
Serial.print("Sensorvalue1:");
Serial.println(Sensorvalue1);
Serial.print("Sensorvalue2:");
Serial.println(Sensorvalue2);
Serial.print("Sensorvalue3:");
Serial.println(Sensorvalue3);
delay(100);
if ((Sensorvalue3 <= 250) & (Sensorvalue2 >= 250) & (Sensorvalue1 <= 250))
{
Serial.println("FORWARD");
digitalWrite(MotorA, HIGH);
analogWrite(EA, 130);
digitalWrite(MotorB, HIGH);
analogWrite(EB, 130);
}
else if ((Sensorvalue1 >= 250) & (Sensorvalue2 >= 250) & (Sensorvalue3 >= 250))
{
Serial.println("STOP");
digitalWrite(MotorA, LOW);
analogWrite(EA, 0);
digitalWrite(MotorB, LOW);
analogWrite(EB, 0);
}
else if ((Sensorvalue3 >= 250 ) & (Sensorvalue2 >= 250) & (Sensorvalue1 <= 250))
{
Serial.println("SHARP LEFT");
digitalWrite(MotorA, HIGH);
analogWrite(EA, 220);
digitalWrite(MotorB, LOW);
analogWrite(EB, 175);
}
else if ((Sensorvalue3 >= 250 ) & (Sensorvalue2 <= 250) & (Sensorvalue1 <= 250))
{
Serial.println("LEFT");
digitalWrite(MotorA, HIGH);
analogWrite(EA, 160);
digitalWrite(MotorB, LOW);
analogWrite(EB, 150);
}
else if ((Sensorvalue3 <= 250) & (Sensorvalue2 >= 250) & (Sensorvalue1 >= 250))
{
Serial.println("SHARP RIGHT");
digitalWrite(MotorA, LOW);
analogWrite(EA, 175);
digitalWrite(MotorB, HIGH);
analogWrite(EB, 220);
}
else if ((Sensorvalue3 <= 250) & (Sensorvalue2 <= 250) & (Sensorvalue1 >= 250))
{
Serial.println("RIGHT");
digitalWrite(MotorA, LOW);
analogWrite(EA, 150);
digitalWrite(MotorB, HIGH);
analogWrite(EB, 160);
}}
the second codes, manual control
#include <Servo.h>
Servo gripper;
Servo arm;
Servo arm2;
int MotorA = 12;
int EA = 10;
int MotorB = 13;
int EB = 11;
char incoming_byte = 0;
int value = 0;
int pos = 0;
void setup() {
// put your setup code here, to run once:
gripper.attach(A3);
arm.attach(A4);
arm2.attach(A5);
Serial.begin(9600);
}
void loop() {
{
if (Serial.available() > 0)
{
incoming_byte = Serial.read();
if (incoming_byte == '9')
{
arm.write(80);
arm2.write(10);
}
if (incoming_byte == '7')
{
arm.write(130);
arm2.write(130);
}
if (incoming_byte == 'E')
{
gripper.write(150);
}
else if (incoming_byte == 'R')
{
gripper.write(0);
}
if (incoming_byte == 'F')
{
arm.write(5);
}
else if (incoming_byte == 'G')
{
arm.write(12);
}
else if (incoming_byte == 'H')
{
arm.write(17);
}
else if (incoming_byte == 'J')
{
arm.write(25);
}
else if (incoming_byte == 'K')
{
arm.write(30);
}
if (incoming_byte == 'C')
{
arm2.write(0);
}
else if (incoming_byte == 'V')
{
arm2.write(10);
}
else if (incoming_byte == 'B')
{
arm2.write(20);
}
else if (incoming_byte == 'N')
{
arm2.write(30);
}
else if (incoming_byte == 'M')
{
arm2.write(40);
}
else if (incoming_byte == '1')
{
arm2.write(90);
}
else if (incoming_byte == '8')
{
arm2.write(120);
}
////////////Forward////////////
if (incoming_byte == 'W')
{
digitalWrite(MotorA, HIGH);
digitalWrite(MotorB, HIGH);
analogWrite(EA, 255);
analogWrite(EB, 255);
}
////////////Slow Forward////////////
if (incoming_byte == 'Q')
{
digitalWrite(MotorA, HIGH);
digitalWrite(MotorB, HIGH);
analogWrite(EA, 100);
analogWrite(EB, 100);
}
////////////Reverse////////////
if (incoming_byte == 'S')
{
digitalWrite(MotorA, LOW);
digitalWrite(MotorB, LOW);
analogWrite(EA, 255);
analogWrite(EB, 255);
}
////////////Left////////////
if (incoming_byte == 'A')
{
digitalWrite(MotorA, HIGH);
digitalWrite(MotorB, LOW);
analogWrite(EA, 255);
analogWrite(EB, 255);
}
////////////Right////////////
if (incoming_byte == 'D')
{
digitalWrite(MotorA, LOW);
digitalWrite(MotorB, HIGH);
analogWrite(EA, 255);
analogWrite(EB, 200);
}
////////////STOP////////////
if (incoming_byte == 'X')
{
digitalWrite(MotorA, LOW);
digitalWrite(MotorB, LOW);
analogWrite(EA, 0);
analogWrite(EB, 0);
}}}}