Help with some programming!!

Just below this box, where you type messages, it says Additional Options...

One of them is Attach: with a name field and a Browse button. Browse to the file you want to attach (your sketch). Click Open. Then, type any text you want, and click Post, as normal. Your file will be attached.

UKHeliBob:
Put
Serial.begin(9600);in setup() and

Serial.println("Starting loop()");

Serial.print("setCounter = ");
Serial.println(setCounter);



as the first 3 lines in loop() and paste the output here.

May I know what is this supposed to do?

And by "paste the output here", which output are you referring to?

What I did was I just put the Serial.begin code in the setup and the 3 sentences in the loop..

Also, may I know if there is any limit to the delay () function?

Because if I am going to do for 12 hr intervals, the millis would be a very large number : 43200000.

As long as it works, I don't mind the large number being there..

kurtselva:

UKHeliBob:
Put
Serial.begin(9600);in setup() and

Serial.println("Starting loop()");

Serial.print("setCounter = ");
Serial.println(setCounter);



as the first 3 lines in loop() and paste the output here.

May I know what is this supposed to do?

And by "paste the output here", which output are you referring to?

What I did was I just put the Serial.begin code in the setup and the 3 sentences in the loop..

One of the very few ways to debug code in the Arduino IDE is to print the value of variables to the Serial monitor at strategic points in the program so their value at that point can be seen. That was what I was suggesting you do. As to what output, I would have thought that it was obvious that I was referring to what you saw on the Serial monitor, but obviously I was not specific enough.

Ok, sry as I mentioned being new to arduino, I am not yet familiar with many codes

After running the programme with the serial codes, I get the output as :
p()
setCounter = 0
Starti
StaòStarting loop()
setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 0

And this keeps on repeating..

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.

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

fypprogram2.txt (14.3 KB)

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 ?

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().

void servo() 
{ 
  Serial.println("Starting loop()");
  Serial.print("setCounter = ");
  Serial.println(setCounter);

That first Serial.println() statement is crap.

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().

Wow, it worked! Thanks alot!

PaulS:
That first Serial.println() statement is crap.

What's wrong with it ?

What's wrong with it ?

It's not in loop. It should be saying that it is.

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 ?

setCursor basically switches my programme to "run" mode..

From what I have tested so far, the programme seems to be working as how I wanted it to..

And you are right, I decided to do away a clock. Instead, I am using the delay() function as a clock. In the program, the delay (2000) increases the current hour by 1 in every 2sec. This is because it is more feasible to test it this way as I do not have to wait for minutes or hours to see if the code works. But in actual programme, I will change the (2000) to (3600000) which is 1hr. So the currenthour should increase by 1 every 1 hr, and when it reaches 24, it resets to 0 (therefore acting as a 24 hr clock). I hope it works...

My earlier post brings me back to this qn: Is there a limit to the delay () function? As in the max milis value it can take?

Is there a limit to the delay () function? As in the max milis value it can take?

Yes, there is.

Parameters
ms: the number of milliseconds to pause (unsigned long)

Any value that fits in an unsigned long is valid, though not necessarily reasonable, input.

UKHeliBob:
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 ?

Guess it is hard to see when the servo actually rotates from my code cos I did not include comments in the code.

So basically, totalfeedforfeedinterval calculates the total feeds needed according to the user input: eg if user input feedinterval = 2 and numberofdays = 2, then totalfeedforfeedinterval is 2 x 2 =4

Also, the user the set the 1st feed hour and the current hour. So, when the program is in run mode, i.e setCursor = 5, the currenthour will increment according to the delay () in the code. When the currenthour = 1stfeedhour, the servo will rotate once. And according to the programme chosen, the servo will then rotate at the specified intervals (if you see the codes you should understand this).

And there is this int feeddone which increments whenever the servo rotates. So, when the feeddone = totalfeedforfeedinterval, the programme exits run mode, i.e setCursor = 0

Parameters
ms: the number of milliseconds to pause (unsigned long)

Any value that fits in an unsigned long is valid, though not necessarily reasonable, input.
[/quote]

ermm..I am new to programming language (that is why my programme only consists of the basic if/else and for statements :blush:)... So what is meant by unsigned long? And is 3600000 fits in an unsigned long?

Sorry, I had forgotten that you were using delay() to do the timing. I is not very accurate but for your application that will probably not matter. There are other ways of creating a delay that do not use delay() and they don't suffer from the number of milliseconds ever being too big and also allow the system to do other things while the wait goes on but I suggest that you get the basics working first, which you seem to be close to doing.

By the way, I still don't understand the many calls to servo(). It seems unnecesary somehow.

So what is meant by unsigned long? And is 3600000 fits in an unsigned long?

It's time you became acquainted with the reference page.

PaulS:

So what is meant by unsigned long? And is 3600000 fits in an unsigned long?

It's time you became acquainted with the reference page.

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).

Thanks for referring me to the reference page..haha

So my delay (3600000) should work!! :smiley: