Hello,
I'm having issues when combining 2 Arduino codes.
They work independently, but i can't figure out a way to combine them.
Code 1 : servo-motor control via Android app
Code 2 : Dc motor control, H bridge via Android app (joystick button)
I combined the Android apps (sliders for the servos and Joystick button for the DC motors)
The problem I have is:
When i use the slider (to control servo) the DC motor start running. (and i don't want that).
I can't figure out how to combine them.
Code1:
#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3, myservo4; // servo name
void setup()
{
myservo1.attach(3); // attach servo signal wire to pin 9
myservo2.attach(5);
myservo3.attach(6);
myservo4.attach(9);
//Setup usb serial connection to computer
Serial.begin(9600);
}
void loop()
{
//Read from Serial and write to usb serial
if(Serial.available()>= 2 )
{
unsigned int servopos = Serial.read();
unsigned int servopos1 = Serial.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);
}
if (realservo >= 3000 && realservo <3180) {
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180, 0, 180);
myservo3.write(servo3);
Serial.println("Servo 3 ON");
delay(10);
}
if (realservo >= 4000 && realservo <4180) {
int servo4 = realservo;
servo4 = map(servo4, 4000, 4180, 0, 180);
myservo4.write(servo4);
Serial.println("Servo 4 ON");
delay(10);
}
}
}
Code2:
#define enA 10
#define in1 4
#define in2 5
#define in3 6
#define in4 7
#define enB 9
int xAxis=140, yAxis=140;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
// Default value - no movement when the Joystick stays in the center
//xAxis = 140;
//yAxis = 140;
// Read the incoming data from the Smartphone Android App
while (Serial.available() >= 2) {
xAxis = Serial.read();
delay(10);
yAxis = Serial.read();
Serial.print(xAxis);
Serial.print(",");
Serial.println(yAxis);
}
delay(10);
// Makes sure we receive corrent values
if (xAxis > 130 && xAxis <150 && yAxis > 130 && yAxis <150){Stop();}
if (yAxis > 130 && yAxis <150){
if (xAxis < 130){turnRight();
motorSpeedA = map(xAxis, 130, 60, 0, 255);
motorSpeedB = map(xAxis, 130, 60, 0, 255);
}
if (xAxis > 150) {turnLeft();
motorSpeedA = map(xAxis, 150, 220, 0, 255);
motorSpeedB = map(xAxis, 150, 220, 0, 255);
}
}else{
if (xAxis > 130 && xAxis <150){
if (yAxis < 130){forword();}
if (yAxis > 150){backword();}
if (yAxis < 130){
motorSpeedA = map(yAxis, 130, 60, 0, 255);
motorSpeedB = map(yAxis, 130, 60, 0, 255);
}
if (yAxis > 150){
motorSpeedA = map(yAxis, 150, 220, 0, 255);
motorSpeedB = map(yAxis, 150, 220, 0, 255);
}
}else{
if (yAxis < 130){forword();}
if (yAxis > 150){backword();}
if (xAxis < 130){
motorSpeedA = map(xAxis, 130, 60, 255, 50);
motorSpeedB = 255;
}
if (xAxis > 150){
motorSpeedA = 255;
motorSpeedB = map(xAxis, 150, 220, 255, 50);
}
}
}
//Serial.print(motorSpeedA);
//Serial.print(",");
//Serial.println(motorSpeedA);
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
void forword(){Serial.println("forword");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void backword(){Serial.println("backword");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void turnRight(){Serial.println("turnRight");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void turnLeft(){Serial.println("turnLeft");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void Stop(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println("stop");
}