Combining Sketches

Hello.....I build a voice controlled robot by copying codes and wanted to build voice controlled line follower but i tried to combine the codes but it didnt worked..I have only little knowledge of arduino programmming can anyone help??I need it for my school project

Line Follower .txt (1.71 KB)

combined.txt (2.67 KB)

Sounds like you need to move beyond just copying other people's work and actually take some time to learn the basics of writing code for an Arduino. I can't help you because you attached the code instead of posting it so you've basically told anyone on a mobile device that you don't want heir help.

Essentially you need to understand what all the little pieces of code do and arrange them into one code so that they make sense.

I am just 14 years old ,I found arduino intresting and begin making some projects I made many projects and learnt many things and i tried to merge code also but it didnt worked

combined.txt (2.68 KB)

Again, you have stopped me from being able to help you by attaching rather than posting the code.

String voice;
int LS=10;
int RS=9;
int LED=13;
char getstr;
int in1=3;
int in2=4;
int in3=5;
int in4=6;
int ENA=2;
int ENB=7;
long duration, distance;
int trigPin=A0;
int echoPin=A1;
int buzzer=8;

void forward()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);

}

void back()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);

}
void left()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);

}
void right()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);

}
void stop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);

}
void on()
{
digitalWrite(LED,HIGH);
}
void off()
{
digitalWrite(LED,LOW);
}
void line(){

if(digitalRead(LS) && digitalRead(RS)) // Stop
{
stop();
}

if(!(digitalRead(LS)) && digitalRead(RS)) // right
{
right();
}

if(digitalRead(LS) && !(digitalRead(RS))) // Turn left
{
left();
}

if(!(digitalRead(LS)) && !(digitalRead(RS))) // Forward
{
forward();
}
}
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop()
{
while (Serial.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
if (c == '#') {break;} //Exit the loop when the # is detected after the word
voice += c; //Shorthand for voice = voice + c
}
if (voice.length() > 0){
if(voice == "*go ahead"){
forward();
}
else if(voice == "*go back"){
back();
}
else if(voice == "*turn right") {
right();
}
else if(voice == "*turn left") {
left();
}
else if(voice == "*light on") {
on();
}
else if(voice == "*light off") {
off();
}

else if(voice == "*stop") {
stop();
}
else if(voice == "*line follow") {
line();

}

voice=""; //Reset the variable after initiating
}
}

Please use code tags when posting code. Read the "How to use this forum -please read" thread.

Right now you only call line from inside that while loop that is checking for serial. So it gets called once and then the String gets cleared and it doesn't get called again. Instead you need to think like a state machine. Let all of those if statements in looo just set some variable that tells what you want to do and then later in loop you call whatever function needs to be called. That way even if there is no serial data coming in your line() function still gets called and you can still follow a line.

Thank you for your help but iam completely unknown what you are saying....I may understand if you dont mind make some changes in my code if you want....That would be great

What part don't you understand? Can you be a little more specific? No, I'm not going to write your code for you unless you pay me. I am very very very expensive.

The line follow part ...It is not working ...When i say line follow it only goes forward

Bibek_Poudel:
The line follow part ...It is not working ...When i say line follow it only goes forward

Yes, and in #5 I explained to you why. What part of it didn't you understand?

Try formatting your code and see if that helps you to see it. In the IDE press Control-T. That will line up all the blocks so you can see how your control logic flows. Real programmers don't just format their code that way because it is pretty.

I dont understand why is it not working as i have limited knowledge ...I would try to get help from my seniors....Thank you for your help :slight_smile: :slight_smile: :slight_smile:

Bibek_Poudel:
I dont understand why is it not working as i have limited knowledge ...I would try to get help from my seniors....Thank you for your help :slight_smile: :slight_smile: :slight_smile:

Yes you have limited knowledge. But you also have an explanation. So that's not a very good excuse anymore.

Have you tried formatting the code so you can see where the blocks line up? Or are you just going to keep going on about how you have limited knowledge? If that's your plan then give up, that never got anyone anywhere.

I think when I say line follow then the line follow code is only executed once.......so i should make it run continuously until i say stop

OK, if you are just going to ignore what I write then I'm done here. Best of luck to you!

I am not able to understand you .....thats why