Go Down

Topic: combination  (Read 108 times) previous topic - next topic

darkboy

can someone help me to combine two code given below

code 1: line follower and android control with Bluetooth
Code: [Select]

//Android Controll
//Line Follower

int en1 = 6; //motor 1 er forward
int  en2 = 9; // motor 1 er backward
int en3 = 10; // motor 2 er forward
int en4 = 11; // motor 2 er backward
int ls = 2; //ir sensor L
int rs = 4; //ir sersor R

void setup() {
  pinMode(ls, INPUT);
  pinMode(rs, INPUT);
  pinMode(en1, OUTPUT);
  pinMode(en2, OUTPUT);
  pinMode(en3, OUTPUT);
  pinMode(en4, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Enter direction:");
}

void loop() {
  if (Serial.available())
  {
    char ch = Serial.read();
    while(ch == 'x'){
      Serial.print("LINE FOLLOWER MODE ACTIVATE ");
  if(digitalRead(ls) && digitalRead(rs))     // Forward
  {
    digitalWrite(en1, LOW);
    digitalWrite(en2, HIGH);
    digitalWrite(en3, LOW);
    digitalWrite(en4, HIGH);
    delay (10);
  }
 
  else if(!(digitalRead(ls)) && digitalRead(rs))     // right
  {
    digitalWrite(en1, LOW);
    digitalWrite(en2, LOW);
    digitalWrite(en3, LOW);
    digitalWrite(en4, HIGH);
    delay (10);
  }
 
 else if(digitalRead(ls) && !(digitalRead(rs)))     //left
  {
    digitalWrite(en1, LOW);
    digitalWrite(en2, HIGH);
    digitalWrite(en3, LOW);
    digitalWrite(en4, LOW);
    delay (10);
  }
 
  else if(!(digitalRead(ls)) && !(digitalRead(rs)))     // stop
  {
    digitalWrite(en1, LOW);
    digitalWrite(en2, LOW);
    digitalWrite(en3, LOW);
    digitalWrite(en4, LOW);
    delay (10);
Serial.println("Enter direction:");
break;
   
  }
 
}
if (ch == 's')
    {
      digitalWrite(en1, HIGH);
      digitalWrite(en2, LOW);
      digitalWrite(en3, HIGH);
      digitalWrite(en4, LOW);

      Serial.print("back ");
    }
    else if (ch == 'w')
    {
      digitalWrite(en1, LOW);
      digitalWrite(en2, HIGH);
      digitalWrite(en3, LOW);
      digitalWrite(en4, HIGH);

      Serial.print("forward ");
    }
    else if (ch == 'a')
    {
      digitalWrite(en1, LOW);
      digitalWrite(en2, HIGH);
      digitalWrite(en3, HIGH);
      digitalWrite(en4, LOW);

      Serial.print("left ");
    }
    else if (ch == 'd')
    {
      digitalWrite(en1, HIGH);
      digitalWrite(en2, LOW);
      digitalWrite(en3, LOW);
      digitalWrite(en4, HIGH);

      Serial.print("right ");
    }
    else if (ch == 'z')
    {
      digitalWrite(en1, LOW);
      digitalWrite(en2, LOW);
      digitalWrite(en3, LOW);
      digitalWrite(en4, LOW);

      Serial.print("STOP ");
    }


    }
         
 
}

 



code 2: 2 servo control with bluetooth
Code: [Select]

#include <SoftwareSerial.h>

#include <Servo.h>
Servo myservo1, myservo2;

int bluetoothTx = 7;
int bluetoothRx = 8;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo1.attach(3);
  myservo2.attach(5);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);
   
    if (realservo >= 1000 && realservo <1180){
    int servo1 = realservo;
    servo1 = map(servo1, 1000,1180,0,180);
    myservo1.write(servo1);
    Serial.println("servo 1 ON");
    delay(10);

    }
   
    if (realservo >=2000 && realservo <2180){
      int servo2 = realservo;
      servo2 = map(servo2,2000,2180,0,180);
      myservo2.write(servo2);
      Serial.println("servo 2 On");
      delay(10);
     
    }
   
  }


}

UKHeliBob

What have you tried ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

darkboy

What have you tried ?

i create a robot which is android controlled and linefollower. now i wanna add a robotic arm so i need to control at least 2 servo with android , i have code for servoes but i cant combine it with my line follower and android control's code . :(

sterretje

What UKHelibob is saying (between the lines): show the code of your attempt.

My view is that you need to understand the code that you want to use. Once you understand how it works, it's usually easy to copy (and modify if needed) the pieces that you need into a new sketch.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

PaulS

Quote
but i cant combine it with my line follower and android control's code .
Why can't you?

Before you make any attempt to combine two independent sketches, you need to have a set of requirements. If you do, you forgot to share them.
The art of getting good answers lies in asking good questions.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy