Pages: [1]   Go Down
Author Topic: millis with random switching question  (Read 822 times)
0 Members and 1 Guest are viewing this topic.
australia
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi guy's
 have been stumped for days on this one,trying to create chopper for water stream,and light show, have done code with delays and works and now wanted to change out to millis type program the theory is turn on led (will be stepper motor in future) then wait random time ,then turn off after seperate random time the code included is only one i would like help with
 thanks in advance


* sketch_random_millis.ino (1.2 KB - downloaded 12 times.)
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
randomSeed(analogRead(0));

...is unreliable.  Here's why...

http://arduino.cc/forum/index.php/topic,66206.msg630884.html#msg630884


These are much better choices...

http://arduino.cc/forum/index.php/topic,108380.0.html
http://arduino.cc/forum/index.php/topic,66206.msg537783.html#msg537783
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Reading from an OUTPUT works so toggling pin 13 can be reduced to this single line of code...

Code:
digitalWrite(13, ! digitalRead( 13 ) );
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


For your application I doubt it will matter but this...

Code:
millis() >= waitUntilb

...is not a reliable way to mark time.


Comparing unsigned values (return from millis) with signed values is not good...

Code:
long waitUntilb=0;

The wait* variables need to be unsigned long.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


From the code, I gather the strategy is to wait a random amount of time (either 100 to 180 milliseconds or 4000 to 5000 milliseconds) and then toggle pin 13.  The HIGH time is 100 to 180 milliseconds and the LOW time is 4000 to 5000 milliseconds.  Sound about right?

All the "problems" listed above are minor.  In other words, there is nothing wrong with how you were approaching the problem.  However, I believe there is a simpler approach.  Maybe something like this...


Code:
void setup()
{
  pinMode(13, OUTPUT);
}

unsigned long delta;
unsigned long previous;

void loop()
{
  unsigned long now;

  now = millis();

  if ( now - previous >= delta )
  {
    digitalWrite(13, ! digitalRead( 13 ) );

    if ( digitalRead( 13 ) )
    {
      // Determine the HIGH time
      delta = random( 100, 180 );
    }
    else
    {
      // Determine the LOW time
      delta = random( 4000, 5000 );
    }
    previous = now;
  }
}
Logged

australia
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks so much
but please dont tell me it took you less than than a day . it worked as expected and i dont think i will be looking for a new wife now!
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks so much

You are welcome.  I hope you learned a few things.

Quote
but please dont tell me it took you less than than a day

Just so you know, the posts are timestamped (03:13:37 AM to 02:57:25 AM).  Of course, that doesn't include the time I spent learning to program.   smiley-wink

Quote
it worked as expected and i dont think i will be looking for a new wife now!

A man's blessing (and curse).  May you spend too much time on things that are not on the wife's Approved Activities list.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 3965
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For your application I doubt it will matter but this...

Code:
millis() >= waitUntilb

...is not a reliable way to mark time.

Because there would be a glitch when the millis() value rolls over?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Exactly.
Logged

australia
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi guys
would a hardware issue be causing led to hang on high after say 5 min or less,i am using uno smd with led in pins 13 and ground,and have only been fooling with random off and on times now longer on than off ?
cheers
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 3965
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hi guys
would a hardware issue be causing led to hang on high after say 5 min or less,i am using uno smd with led in pins 13 and ground,and have only been fooling with random off and on times now longer on than off ?
cheers

Please post your code, hard to diagnose otherwise. Is the issue consistent, i.e. does it always occur after 5 minutes?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Do you have a current limiting resistor in series with that LED?

Or are you talking about the on-board LED?
Logged

australia
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok
the code is as was the provided by me old mate coding badly, the led was another seprate to board but as i was lead to believe pin 13 has inbuilt resistor for this purpose ( maybe not ha).over night i left it running with only board led doing the work and it is still working this morning,looks like i either need to provide seperate power source via switching transistor to led  or increse the resistance via a couple of resistors on  bread board,lesson learnt !
and thanks for your reply.
Logged

Pages: [1]   Go Up
Jump to: