Go Down

Topic: Combining Sketches (Read 287 times) previous topic - next topic

Bibek_Poudel

Nov 14, 2017, 03:22 pm Last Edit: Nov 14, 2017, 03:58 pm by Bibek_Poudel
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


Delta_G

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. 
If at first you don't succeed, up - home - sudo - enter.

Bibek_Poudel

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

Delta_G

Again, you have stopped me from being able to help you by attaching rather than posting the code. 
If at first you don't succeed, up - home - sudo - enter.

Bibek_Poudel


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
  }
}

Delta_G

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.
If at first you don't succeed, up - home - sudo - enter.

Bibek_Poudel

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

Delta_G

#7
Nov 14, 2017, 06:50 pm Last Edit: Nov 14, 2017, 06:50 pm by Delta_G
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. 
If at first you don't succeed, up - home - sudo - enter.

Bibek_Poudel

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

Delta_G

#9
Nov 15, 2017, 02:01 am Last Edit: Nov 15, 2017, 02:01 am by Delta_G
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?
If at first you don't succeed, up - home - sudo - enter.

Delta_G

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. 
If at first you don't succeed, up - home - sudo - enter.

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 :)  :)  :)

Delta_G

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 :)  :)  :)

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. 
If at first you don't succeed, up - home - sudo - enter.

Bibek_Poudel

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

Delta_G

#14
Nov 15, 2017, 03:54 am Last Edit: Nov 15, 2017, 03:55 am by Delta_G
OK, if you are just going to ignore what I write then I'm done here.  Best of luck to you!
If at first you don't succeed, up - home - sudo - enter.

Go Up