Go Down

Topic: Help with some programming!! (Read 2 times) previous topic - next topic

UKHeliBob

No problem.

OK, so now we know that setCounter is zero each time through loop(), which is what we would expect if you have not set anything using the buttons, which I think you haven't.  The servo should not be moving because setCounter is not 7.  Can you confirm that it is not moving ?

If so, use the buttons to input values and get the servo moving.  You say that the movement occurs more than once.  Watch the value of setCounter just before it moves and just after.  It may help to put some more Serial.println()s in the servo() function so that you can see where it starts running in the Serial monitor window.

As before, paste some output here of the value of setCounter before and after the servo() function.

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

kurtselva

#26
Feb 11, 2013, 07:55 pm Last Edit: Feb 11, 2013, 07:59 pm by kurtselva Reason: 1
Ok. Actually I have made some changes to my programme (have attached it). Now the servo should rotate when setCursor is 5.  

I have added the serial codes to the servo () function as well and this a reduced version of what I got as output:

setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 1
Starting loop()
setCounter = 1
Starting loop()
setCounter = 2
Starting loop()
setCounter = 2
Starting loop()
setCounter = 3
Starting loop()
setCounter = 3
Starting loop()
setCounter = 4
Starting loop()
setCounter = 4
Starting loop()
setCounter = 5
Starting loop()
setCounter = 5

The setCounter = 5 appears when the servo rotates, and I got this when it rotates at the required timing

But when the servo rotated randomly at unwanted timing, the output was still showing as setCounter = 0

To be more specific of my problem, the servo motor only rotates randomly when I have uploaded the programme onto the arduino and till the point when the cursor has been set to 5. After which, it rotates as needed

UKHeliBob

What meaning has setCounter == 5 got in your program ?

You are calling the servo() function from 8 places in your code.  That can't be right, can it ?  You only need to call it when the clock time equals the requested feed time.

I have just realised something.  You don't seem to have any form of clock in your program to compare the feeding times with, or have I missed something fundamental ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Perhaps you should add a Servo.write() before the Servo.attach() call, to direct the servo to a specific starting point. The servo should then not move until commanded to, in servo().

Code: [Select]
void servo()
{
  Serial.println("Starting loop()");
  Serial.print("setCounter = ");
  Serial.println(setCounter);

That first Serial.println() statement is crap.

kurtselva


Perhaps you should add a Servo.write() before the Servo.attach() call, to direct the servo to a specific starting point. The servo should then not move until commanded to, in servo().


Wow, it worked! Thanks alot!

Go Up