Help with code!!!!

Hey guys,
I'm currently working on project that involves controlling a boat autonomously and whilst that is going on collect GPS coordinates of the path the boat is travelling and send it to a log file. So far i have been able to write the program to control the movement of the boat autonomously works perfectly fine on its own, the same goes the GPS code, its also works on its own. But the minute i try to combine the two programs together, the boat goes out of control and the servos start turning uncontrollably, its just doesn't work. Any ideas on how i can achieve this. Thanks alot


Code for boat movement

#include <Servo.h>

Servo Rudders;
Servo fThrust1;
//Servo fThrust2;
Servo rThrust;

/
int minPulse = 850;
int maxPulse = 2250;
int pos = 0;
int counter = 0;
boolean ok = false;
boolean ready = false;

void setup()
{

Serial.begin(9600);

void loop()
{

if(!ready)
{

First();
}

if(!ok)
{

Move();

}

}

void Move(){ //Beginning of move function..

fThrust1.write(30);
// fThrust2.write(180);
rThrust.write(30);
delay(5000);

for(pos = 90; pos<=140; pos+=1) // goes from 180 degrees to 0 degrees
{
Rudders.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

delay(2000);

for(pos = 140; pos>=90; pos -= 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Rudders.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

// counter += 1;
//Serial.print("Counter is :");
//Serial.println(counter);

delay(5000); //wait for a bit before turning again ...

for(pos = 90; pos<=140; pos+=1) // goes from 180 degrees to 0 degrees
{
Rudders.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

delay(2000);

for(pos = 140; pos>=90; pos -= 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Rudders.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

//counter += 1;
//Serial.print("\nCounter is :");
//Serial.println(counter);

delay(5000); //wait for a bit before turning again ...

for(pos = 90; pos<=140; pos+=1) // goes from 180 degrees to 0 degrees
{
Rudders.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

delay(2000);

for(pos = 140; pos>=90; pos -= 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Rudders.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

delay(2000);

//counter += 1;
//Serial.print("\nCounter is :");
//Serial.println(counter);

delay(5000); //wait for a bit before turning again ...

fThrust1.write(0);
ok = true;

}// End of Move function....

void First(){ // Function to arm the thrusters ...

// Attach each Servo object to a digital pin
Rudders.attach(9, minPulse, maxPulse);
fThrust1.attach(10, minPulse, maxPulse);
//fThrust2.attach(10, minPulse, maxPulse);
rThrust.attach(11, minPulse, maxPulse);

fThrust1.write(0);
// fThrust2.write(0);
rThrust.write(0);
delay(2000);

fThrust1.write(180);
//fThrust2.write(180);
rThrust.write(180);
delay(2000);

fThrust1.write(0);
//fThrust2.write(0);
rThrust.write(0);
delay(2000);

ready = true;

}

P.S the codes for the GPS and the final output i attached to this post.. Pls check them and let me what you think ..

gpsCode.ino (8.25 KB)

finalCode.ino (11.9 KB)

Code goes inside code tags

I'm sorry i don't get, have you looked at the finalCode.ino file ?

lionel007:
I'm sorry i don't get, have you looked at the finalCode.ino file ?

If you can't follow simple directions, then there is no point in trying to give you direction to help you with your code.

See the "blink without delay" example. Until you get rid of the delays you won't get any where.

Mark

void setup() 
{
    

  Serial.begin(9600);


void loop() 
{

That's not going to go well

Did you see how I used code tags there?
Why didn't you?

ohh.. sorry about that, its a typo...

void setup()
{
Serial.begin(9600);
}

Missing code tags is not "a typo"

I'm sorry a little bit confused, don't get what you are saying, whats the right code tag ?

lionel007:
I'm sorry a little bit confused, don't get what you are saying, whats the right code tag ?

Follow the link I posted, it explains it pretty clearly. You could also look at AWOL's post as an example.

What everyone is trying to say is that you need to place your code in the following manner:

#include <Servo.h>

Servo Rudders;   
Servo fThrust1;   
//Servo fThrust2; 
Servo rThrust;   

int minPulse = 850;   
int maxPulse = 2250; 
int pos = 0;   
int counter = 0;
boolean ok = false;
boolean ready = false;

void setup()  {
  Serial.begin(9600);
}


void loop()  {
  if(!ready)  {
    First();
  }
  if(!ok)  {
    Move();
  }
}

void Move(){ //Beginning of move function..

  fThrust1.write(30);
  // fThrust2.write(180);
  rThrust.write(30);
  delay(5000);

  for(pos = 90; pos<=140; pos+=1)     // goes from 180 degrees to 0 degrees
  {                               
    Rudders.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

  delay(2000);

  for(pos = 140; pos>=90; pos -= 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    Rudders.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

  // counter += 1;
  //Serial.print("Counter is :");
  //Serial.println(counter);

  delay(5000); //wait for a bit before turning again ...

  for(pos = 90; pos<=140; pos+=1)     // goes from 180 degrees to 0 degrees
  {                               
    Rudders.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

  delay(2000);

  for(pos = 140; pos>=90; pos -= 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    Rudders.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

  //counter += 1;
  //Serial.print("\nCounter is :");
  //Serial.println(counter);

  delay(5000); //wait for a bit before turning again ...

  for(pos = 90; pos<=140; pos+=1)     // goes from 180 degrees to 0 degrees
  {                               
    Rudders.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

  delay(2000);

  for(pos = 140; pos>=90; pos -= 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    Rudders.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

  delay(2000);

  //counter += 1;
  //Serial.print("\nCounter is :");
  //Serial.println(counter);

  delay(5000); //wait for a bit before turning again ...

  fThrust1.write(0);
  ok = true;

}// End of Move function....


void First(){ // Function to arm the thrusters ...

  // Attach each Servo object to a digital pin
  Rudders.attach(9, minPulse, maxPulse);
  fThrust1.attach(10, minPulse, maxPulse);
  //fThrust2.attach(10, minPulse, maxPulse);
  rThrust.attach(11, minPulse, maxPulse);

  fThrust1.write(0);
  // fThrust2.write(0);
  rThrust.write(0);
  delay(2000);

  fThrust1.write(180);
  //fThrust2.write(180);
  rThrust.write(180);
  delay(2000);

  fThrust1.write(0);
  //fThrust2.write(0);
  rThrust.write(0);
  delay(2000);

  ready = true;
}

Type "[code ] YOUR CODE [ /code]" (take out the spaces of the brackets)

Also, when you copy paste your code and we find an error...it's not a typo. It may have been a typo on your sketch, but it's not a typo in the forum cuz it was simply a copy/paste. So basically what he's saying is...fix the bracket in your sketch and your code probably works. Btw, the code I posted above is a cleaned up version of your code. Have a good day and please read the post rules b4 posting next time.

Thanks for the reply you guys, I made the changes but the program is still not working fine..

but the program is still not working fine

Define "fine"

its not working at all .... having the same issues. The problem happens when i merge the two sketches together.

Where are your debug prints?
What do they tell you?

Post your code.
Don't forget the code tags.

Click on this link: How to post code properly
Read it. Do it.

Pete

I don't get any error, just that thrusters and the servos starts firing on their own uncontrollably..

It may come as a surprise to you, but

  1. We can't see your code
  2. we can't see your hardware
  3. we can't see your debug output.

Your move.

I attached the merged code I below, can't use paste it here cos it exceeds maximum character needed. For my hardware, all i have is an arduino uno board ,12volts battery (to power the EDF and servos) a pair of EDF(electric ducted fan) that act as thrusters for the boat, a pair servos that control the direction of the boat and a platform that looks like a boat. Whenever i run the program for movement, its works fine. But the minute i add the code for the GPS, its starts acting up, servos moving from left to right and the EDF firing from low to high... There's no debug output.. It all has to do with the coding... Thats where I need your help ...

finalCode_ino.ino (11.4 KB)

There's no debug output

Well add some.
How else do you expect to debug it?