Go Down

Topic: Help with design project. (arduino timing) (Read 968 times) previous topic - next topic

IANfinity

This is for a design project. I need to program my robot to drive forward 10 feet, drop a wooden block, and drive forward 10 more feet.

What I need help with:

The robot uses one motor (actually a continuous rotation servo) and one servo. The motor drives the robot forward in a straight line. The servo controls the dropping of the block. Basically there is a bent shaft that the block rests on that will rotate up and out of the way to drop the block.

So I know that the robot takes 12.5 seconds to reach 10 feet. My idea was to program the robot to just keep driving until switched off, and to turn the servo on after the system has been turned on for 12.5 seconds. Sounds like it would be simple enough to program... I tried using an If statement:

myservo is the motor for the drive system. myotherservo is the servo for the release mechanism.

#include <Servo.h>

Servo myservo;
Servo myotherservo;
int time = millis();

void setup()
{
  myservo.attach(9);
  myotherservo.attach(10);
 
}

void loop()
{
  myservo.write(120);
 
  if(time > 12500)
  {
  myotherservo.write(120);
  }
}


When I use this code, the motor (myservo) runs continuously as it should, but the servo (myotherservo) just runs immediately as the system is switched on. After 12.5 seconds, it runs again. I have tried the idea of this code in different ways, but no matter what I do, it behaves the same way.

I am new to programming with an Arduino (and programming in general) so I am having a lot of trouble finding out what I'm doing wrong. If anyone could help lead me in the right direction it would greatly appreciated. I need this thing running perfectly by Tuesday.

Thanks in advance for your help.

robtillaart


please use the # button to apply proper code tags for your code

something like this ?
Code: [Select]

#include <Servo.h>

Servo myservo;
Servo myotherservo;

unsigned long start = 0;
bool flag = false;

void setup()
{
  myservo.attach(9);
  myotherservo.attach(10);
  start = millis();
  flag = false;
}

void loop()
{
  myservo.write(120);

  unsigned long now = millis();
  if (now - start > 12500UL && flag == false)
  {
     flag = true; // do it only once
     myotherservo.write(120);
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PaulS

Quote
The motor drives the robot forward in a straight line.

If it really does, your damned lucky, then.


IANfinity



please use the # button to apply proper code tags for your code

something like this ?
Code: [Select]

#include <Servo.h>

Servo myservo;
Servo myotherservo;

unsigned long start = 0;
bool flag = false;

void setup()
{
  myservo.attach(9);
  myotherservo.attach(10);
  start = millis();
  flag = false;
}

void loop()
{
  myservo.write(120);

  unsigned long now = millis();
  if (now - start > 12500UL && flag == false)
  {
     flag = true; // do it only once
     myotherservo.write(120);
  }
}


This seems to be doing the exact same thing as the code I had. The servo, "myotherservo", turns on immediately. It does rotate again after 12.5 seconds, but the first rotation is what I need to NOT happen. I don't see why the code you gave me shouldn't work though. Of course, I am not familiar with the boolean constants true and false. I don't really get how they work. Also, it seems to me like (now - start) should always equal 0 since they are both assigned to millis(). Perhaps I am misunderstanding how millis() works? I am thinking that it keeps a constant count of how much time has elapsed and continuously stores it into the variable you've assigned to it (in this case start or now). This must be wrong though. I read the info on it on this website.

Thank you so much for your help!


Quote
The motor drives the robot forward in a straight line.

If it really does, your damned lucky, then.



It does drive in a straight line. It might not be exactly straight, but it works well enough for our purposes. This robot uses a single motor drive, so it isn't as hard to keep straight as a differential drive system.

IANfinity

I switched the signal wires so that the pin 9 controls myotherservo and pin 10 controls myservo. Doing this, I was able to see that the code DOES work fine. It is the servo (myotherservo) that does not want to work right. The motor works fine. I don't know what the problem could be here. I tried three different size resistors. The way I hooked them up was by wiring the ground of the battery through the resistor into the ground of the servo. Is this correct? Doing this was as if the servo wasn't even attached at all.

PaulS

Quote
I tried three different size resistors. The way I hooked them up was by wiring the ground of the battery through the resistor into the ground of the servo. Is this correct?

Why are you using a resistor at all?

Go Up