Go Down

Topic: NEW on-off cycle question!!! (Read 579 times) previous topic - next topic

dedler

First off, Thanks very much to AWOL and brtech for the prompt help with my earlier question http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274292639/0#0

ive managed to get my code to a usable point...i just repeated my sequence 18 times and put a big delay at the end, its been running fine and ive only used about 5500 bytes of memory...but now on to my next problem:

Since I am working with two separate arduinos running two different codes, I now need help figuring how to cycle the second adruino... I am using arduinos to control an electronic switch which is cycling lights on and off in morse code....and I want the morse sequence to run for 5 minutes and then turn off for 10 minutes, then run for 5 minutes..etc...I have the morse code sequence figured out...i just need to get it to cycle on and off...

my problem now is how to get the following bit of code to run for 5 minutes, then pause for 10 minutes, then run for 5..etc..


The code is meant to generate random morse code...either dots or dashes... it works great, i just dont have any idea how i can make it cycle on and off like i have described...

Any and all help would be much appreciated!!!!

Thank you, Don
:D

int pin = 12;
long randOn = 0;

void setup()
{
 pinMode(pin, OUTPUT);
}

void loop()
{
 
}
 randOn= random(0,10);
 if (randOn > 5) {
   digitalWrite(pin, HIGH);
 delay(100);
 digitalWrite(pin, LOW);
 delay(100);
}
else
{digitalWrite(pin, HIGH);
 delay(400);
 digitalWrite(pin, LOW);
 delay(100);
}
}

crimony

#1
May 20, 2010, 04:12 am Last Edit: May 20, 2010, 01:38 pm by crimony Reason: 1
How about this:

Code: [Select]

#define _5MIN 300000
int pin = 12;
long randOn = 0;
long remaining;

void setup()
{
pinMode(pin, OUTPUT);
}

void loop()
{

remaining = _5MIN;

while (remaining >= 0)
{
 randOn= random(0,10);
 if (randOn > 5)
 {
  digitalWrite(pin, HIGH);
  delay(100);
  digitalWrite(pin, LOW);
  delay(100);
  remaining -= 200;
 }
 else
 {
  digitalWrite(pin, HIGH);
  delay(400);
  digitalWrite(pin, LOW);
  delay(100);
  remaining -= 500;
 }
}

delay(_5MIN * 2 + remaining); // 10 minutes minus whatever we exceeded the 5 minutes by.
}


We keep track of the 5 minutes active time with the "remaining" variable, which is decremented each time we spend some time doing something. When the 5 minutes is up, "remaining" will be < 0, in which case we reduce the 10 minute delay by the amount we went over, resulting in a total of 15 minutes spent in the full loop{}.

PaulS

@dedler
If you properly indented your code, and lined up the curly braces, you'd see that it doesn't look right:
Code: [Select]
void loop()
{

[glow]}[/glow]
 randOn= random(0,10);
 if (randOn > 5)
 {
   digitalWrite(pin, HIGH);
   delay(100);
   digitalWrite(pin, LOW);
   delay(100);
 }
 else
 {
   digitalWrite(pin, HIGH);
   delay(400);
   digitalWrite(pin, LOW);
   delay(100);
 }
}


You could also do us a big favor and learn to use the # key on the top row when posting code.

Groove

Quote
How about this:

Code:#define 5MIN 300000


Bzzzt!
Per Arduino ad Astra

crimony

Quote
Bzzzt!


I didn't say it was tested  ;)

dedler

Bzzzt! ???What does that mean???

im guessing it means some kind of a problem with the code...?

that being said, I tried the code crimony posted and it seems to be working beautifully!!!! THANK YOU VERY MUCH!


AWOL

#6
May 20, 2010, 10:38 pm Last Edit: May 20, 2010, 10:40 pm by AWOL Reason: 1
Quote
I tried the code crimony posted and it seems to be working beautifully!!!!


From groove's post, and the fact that crimony edited the code post after groove's "bzzt", I'm going for crimony originally posted an illegal macro name "5MIN" and then changed it to "_5MIN"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dedler


Go Up