Pages: 1 2 [3]   Go Down
Author Topic: Help with some programming!!  (Read 2154 times)
0 Members and 1 Guest are viewing this topic.
East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4060
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That first Serial.println() statement is crap.
What's wrong with it ?
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What's wrong with it ?
It's not in loop. It should be saying that it is.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a limit to the delay () function? As in the max milis value it can take?
Yes, there is.

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

« Last Edit: February 11, 2013, 02:51:41 pm by kurtselva » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Quote
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  smiley-red)... So what is meant by unsigned long? And is 3600000 fits in an unsigned long?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4060
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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-grin
« Last Edit: February 11, 2013, 03:13:37 pm by kurtselva » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Hmm well, for different feeding interval, the timing for the servo to rotate differs:
for interval 2, it rotates at firstfeedhour and at m = firstfeedhour +12 hr..so I called 2 servo () after each condition is true
for interval 3, it rotates at firstfeedhour, at m = firstfeedhour +8 and at n = m +8.. so I called 3 servo () after each condition is true
and similar for interval 4

The good thing is that the programme is working..but I do not know if it could be done simpler..
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

I guess it is time to try other ways of creating a delay  smiley-grin

Any suggestions?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4060
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the BlinkWithoutDelay example sketch.

As to your current program calling servo() many times, I would do it like this
Code:
calculate numberOfTimesToFeed from information entered by the user
put the delays between feeds in an array waitingTimes[]
set feedcounter =0

  while feedcounter less than numberOfTimesToFeed
    set wait time to waitingTimes[feedcounter]
    wait
    move servo
    increment feedcounter
    exit while if feedcounter equals numberOfTimesToFeed
   else go back and do it again





Logged

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

Pages: 1 2 [3]   Go Up
Jump to: