programming emergency

i’m trying to make a robot with a robotic arm (heavily modified mearm 1.0) that uses 4 servos for the arm and 2 dc motors to move around. this robot is controlled by bluetooth from an android app but that’s where my problems started. i can’t for the life of me figure out how to write the code or how to to make the app and i have 2 days to do it but i tried everything and i’m at a dead end so if anyone here can help that would be hugely appreciated <3
i should mention that i’m using an arduino uno rev3 and an L298N and an HC-05 and the servos are MG995
Here’s the code so far:

#include <SoftwareSerial.h>

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

int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int in4 = 2; //declaring the in 1 to 4 for the 4 bridge
int in3 = 3;
int in2 = 4;
int in1 = 5;
int light = 12; //lights for decoration
void setup()
{
pinMode(in4,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(light,OUTPUT);
myservo1.attach(6); // attach servo signal wire to pin 9
myservo2.attach(7);
myservo3.attach(8);
myservo4.attach(9);
//Setup usb serial connection to computer
Serial.begin(9600);

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

void loop()
{
digitalWrite(light,HIGH);
//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);
}
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);
}
}
}

again if anyone can help that would be very much appreciated as i still am new to this.
Cheers!

Please provide some examples of the messages that are being sent to the Arduino.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R

Which parts work and which don't? For a project like this its essential to proceed methodically and get each
device working independently first, before any attempt to combine into an application - its really hard to
test and debug 4 broken bits of code simultaneously - so proceed one function at a time.

Like i said I'm on a tight schedule so i had to rush everything and to answer your question nothing is working at the moment and the reason being that i don't know where to start, got the arm assembled and the chassis built so the hardware part is pretty much taken care of but what remains is the much harder software side of things and like i said the code posted here is the farthest I've been able to get

    #include <SoftwareSerial.h>

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

int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int in4 = 2; //declaring the in 1 to 4 for the 4 bridge
int in3 = 3;
int in2 = 4;
int in1 = 5;
int light = 12; //lights for decoration
void setup()
{
  pinMode(in4,OUTPUT);
  pinMode(in3,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(in1,OUTPUT);
  pinMode(light,OUTPUT);
  myservo1.attach(6); // attach servo signal wire to pin 9
  myservo2.attach(7);
  myservo3.attach(8);
  myservo4.attach(9);
  //Setup usb serial connection to computer
  Serial.begin(9600);

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

void loop()
{
  digitalWrite(light,HIGH);
  //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);
    }
    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);
    }
  }
}

Here is the code in a more readable form, sorry about that

First get the servos working. Use one of the servo examples such as Sweep. Just do one at a time. When they are all proven to work, put them all together - this tests the power supply can run all 4 at the same time.

Then read Serial Input Basics You can't just wait until there's 2 characters in the input buffer.

I can't help you out with programming the app on Android. I hope that you have some resources to do this. I believe the MIT App Inventor kit is pretty good.

The servos are indeed working along with the motors so like i said my issue is purely software related, struggling on the app front whether i used MIT app inventor or remoteXY and while this code compiles and does in fact work i can't figure out how to add the code for the motors

I'm sure the C language could have included a function called addCodeForMotors() but the designers cruelly omitted it to make things hard for you.

For anyone to help you we need some details like what do you expect these (unspecified) motors to do? And a whole lot more.

deadly_carrots:
Like i said I'm on a tight schedule

Then you need to answer ALL the questions you are asked without having to be reminded.

You have not yet answered the very first question you were asked.

And you have not said whether you have tried the examples in Serial Input Basics

...R

to answer the questions i do not know what you mean by messages being sent to the arduino but yes i have tried examples from the serial input basics but no avail
i should add the the motors are unlabeled 9V DC motors and what they’re supposed to do is move the whole thing with the arm on top around. So left motor activates to to go right, right one activates to go left both to go forward and they can also move backwards. the first image attached shows the arm i’m using (but with MG995 servos) and the second should give you an idea of i’m aiming for here.

Blue_on_Black_preview_featured.jpg

download.jpg

You have an app. Or an idea for an app. It's supposed to control the robot. HOW does the app control the robot? It must send some kind of message over bluetooth. What will that message look like?

deadly_carrots:
but yes i have tried examples from the serial input basics but no avail

There's several examples. Which one did you think would get closest to what you want? Did you try modifying the code to get it closer? Then show us that code. Tell us what you are having problems with.

"No avail" doesn't help us help you.

Posting pictures of "Like this one but different" doesn't really help. Can you post a picture of your actual robot? If it's currently a pile of parts, then don't bother. We don't need a picture of a pile.

deadly_carrots:
to answer the questions i do not know what you mean by messages being sent to the arduino but yes i have tried examples from the serial input basics but no avail

If you want to write successful code for your Arduino to interpret messages that are being sent to it then the first thing you need to know is the content of those messages.

Have you tried the second example in Serial Input Basics on its own - and if so, what is the output it produces?

...R

regarding my previous question the matter has been solved and big thanks to whoever helped or tried to, but after writing a messy janky barely working code it caused the servos (MG995) to have a seizure. As soon as they are plugged into the 5V and GND and their PWM pin they keep trying to rotate a few degrees every second, i’ve even had them do a sweep even though the code has nothing to do with a sweep. the code is as follows:

#include <Servo.h>
Servo claw,base,extension,elevation;
int in1 = 2;
int in2 = 4;
int in3 = 7;
int in4 = 8;
char val;

void setup() 
{
base.attach(3);
claw.attach(5);
extension.attach(6);
elevation.attach(11);
pinMode(in1, OUTPUT);  
pinMode(in2, OUTPUT); 
pinMode(in3, OUTPUT); 
pinMode(in4, OUTPUT);
Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0)
  {
  val = Serial.read();
  Serial.println(val);
  }

  if( val == 'z') // Forward
    {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW); 
    }
  else if(val == 's') // Backward
    {
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH); 
    }
  
    else if(val == 'q') //Left
    {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    }
    else if(val == 'd') //Right
    {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); 
    }
    else if(val == 'x') //stop
    {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); 
    }
     else if(val == 't') //claw open
    {
    claw.write(30);
    }
      else if(val == 'g') //claw close
    {
    claw.write(40);
    }
      else if(val == 'b') //base right  
    {
    base.write(130);
    }
      else if(val == 'v') //base middle
    {
    base.write(100);
    }
      else if(val == 'c') //base left
    {
    base.write(70);
    }
      else if(val == 'y') //up
    {
    elevation.write(30);
    }
     else if(val == 'h') //down
    {
    elevation.write(30);
    }
     else if(val == 'u') //long
    {
    extension.write(30);
    }
     else if(val == 'j') //short
    {
    extension.write(30);
    }
 
    
}

any suggestions on why thy might be doing this?

Hi,
What are you using to power your project?
I particular the servos?

Thanks... Tom... :slight_smile:

to test them one by one i connected a single servo to the 5v pin on the arduino and the arduino itself is connected via usb to a computer

Hi,
You cannot power servos from the USB/Arduino 5V pin.
Each servo will draw more than the USB can supply.
You need a separate power supply for the servos.

What course is this project for. College/University?

Thanks.. Tom... :slight_smile:

connected them to external power on 5.7V and now they refuse to move at all

Hi,
Have you got the gnd of the servo supply connected to the ground of the arduino?

Tom..

no, both the grounds of the arduino and the servo are connected together and to the ground of the external power

TomGeorge:
Hi,
Have you got the gnd of the servo supply connected to the ground of the arduino?

Tom..

deadly_carrots:
no, both the grounds of the arduino and the servo are connected together and to the ground of the external power

?