Infinite loop with other things happening.

Hello
Just one question. I found a pretty cool 9 lines of code that blink several LEDs randomly. However, when I try to use the coding in my sketch, that does other stuff, it doesn't work. I followed the Robin2 tutorial on "Doing Several Things At Once". No luck. I have reread my programming book. It explains how to use a for loop, however not how to use it with other code. I've been searching around the net and had no luck there either. Can someone please just tell me this; Is it possible to use a for loop in a program that does other things?

Yes, you can use a for loop, as long as you do not use delay, or block while waiting for input, etc.

However, the loop() function loops, several tens of thousands of times per second, depending on how much you have to do. That is PLENTY fast for blinking a bunch of LEDs randomly, while doing some other stuff.

I followed the Robin2 tutorial on "Doing Several Things At Once". No luck.

Work on that a bit more, and if you run into trouble, post a question. But if you have trouble with that tutorial, here are two more excellent ones:

Thanks... I'll look into the links

Also the code I used can be customized but with delays. See it below.. I will try to set it up without delays.

int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;
int led5 = 12; 
int leds[5] = {8,9,10,11,12};
const int LDRpin = 0;
int LDRValue = 0;

void setup()
{
  for (int jj; jj<sizeof(leds)/sizeof(int);jj++)
{
       pinMode(leds[jj],OUTPUT);    
       pinMode(LDRpin, INPUT);
       delay(10);
       Serial.begin(9600);   //debug
}
}

void loop()
{
  int ldrStatus = analogRead(LDRpin);
      LDRValue = analogRead(LDRpin);     // read the value from the LDR
      Serial.println(LDRValue);          // print to the serial port for testing
      delay(100);

  if (ldrStatus <= 235)                   //it is dark, turn on lights
{  
      digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
      delay(random(1000,1500));
      digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);

} else                                             //suns up turn light off
{                     
      digitalWrite (leds, LOW);
      digitalWrite (led2, LOW);
      digitalWrite (led3, LOW);
      digitalWrite (led4, LOW);          
      digitalWrite (led5, LOW);
}    
}

OldJohn:
I followed the Robin2 tutorial on "Doing Several Things At Once". No luck.

Unless you post the code that you tried I can't help.

...R

That code is a bit stupid, here is a less stupid version of it:-

byte leds[5] = {8,9,10,11,12};
const int LDRpin = 0;
int LDRValue = 0;

void setup()
{
  for (int jj=0; jj<sizeof(leds);jj++)
{
       pinMode(leds[jj],OUTPUT);
}   
       // you only have to do the next bit once so take it outside the for loop
       pinMode(LDRpin, INPUT);
       // delay(10); // remove it is doing nothing for you
       Serial.begin(9600);   //debug
}

void loop()
{
  int ldrStatus = analogRead(LDRpin);
      LDRValue = analogRead(LDRpin);     // read the value from the LDR
      Serial.println(LDRValue);          // print to the serial port for testing
      delay(100); // again this just slows everything down

  if (ldrStatus <= 235)                   //it is dark, turn on lights
{ 
     // you need to work on this and write it as a state machine
      digitalWrite(leds[random(0,sizeof(leds))],HIGH);
      delay(random(1000,1500));
      digitalWrite(leds[random(0,sizeof(leds))],LOW);

} else                                             //suns up turn light off
{                     
      for(int i =0; i<<sizeof(leds); i++)
      digitalWrite (leds[i], LOW);

}   
}

Robin2:
Unless you post the code that you tried I can't help.

...R

Thanks, I have gotten a lot of help here already. I think Jremington had the right idea. I need to find a way to lose the delays

Thanks Grumpy. Will this part of code make the program stop?
delay(random(1000,1500));

BTW Code being stupid is a reflection of my changes.

Will this part of code make the program stop?

Yes for between one and one and a half seconds. During that time the processor can do nothing useful no matter how you write the rest if the code.

for (int jj;

déjà vu

OldJohn:
I have reread my programming book. It explains how to use a for loop, however

... I chose to ignore it. Again.

AWOL:

for (int jj;

déjà vu
... I chose to ignore it. Again.

I didn't choose to ignore it. I just can't, not use it, and make the LEDs do what I want them to do. I was trying to find a work around. I was trying to understand the programming aspect! I was trying to finish one project. I now understand "blink without delay" but that is not what I wanted. I have a grasp on using milliseconds but can't make that work the way I want in this case.

It is becoming more and more clear now that I don't have the aptitude for this. I did in college but that was a very long time ago.

I have given up and decided to solve the problem the easy way. I will simply use two nano boards.

So, thank you everyone for your help.

“It is becoming more and more clear now that I don't have the aptitude for this.”

Balderdash.

Balderdash.

Balderdash; Late 16th century (denoting a frothy liquid; later, an unappetizing mixture of drinks): of unknown origin.
Nonsense, rubbish, gibberish, claptrap, blather, blether; informalrot, tripe, hogwash, baloney, drivel, bilge, bosh, bull, bunk, guff, eyewash, piffle, poppycock, phooey, hooey, malarkey, twaddle, dribble; informal cobblers, codswallop.

I prefer Codswallop.

I guess that is meant as encouragement. If so, thanks but I'm well past that state.

Quite a few of us old timers here, many over 70 years of age.

Note to self, when you write code, always add a ‘good’ comment for each line.

This way when you forget what that line does, in 5 minutes, the comment tells you what the author ??? was trying to do.

I didn't choose to ignore it. I just can't, not use it, and make the LEDs do what I want them to do.

So you tried it and something stopped working? No you didn't because that fixing that error will not do that. So I prefer to use the word bollocks to that comment. You will notice that I fixed that problem in the code in reply #4.

I now understand "blink without delay" but that is not what I wanted.

Yes it is exactly what you want. Your random delay is simply accommodated by making the time interval before the next time you call the function a random number of mS between 1000 and 1500.

Have a look at http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html For another take on the technique.

If you want to see this technique in a complex context then look at the code for http://www.thebox.myzen.co.uk/Hardware/Dice_Game.html
Note the bit where I say

I wrote the firmware as one great big state machine, with the lid sensor being read on every loop.

I have attached the code for you to look at.

Dice_Game.pde (10.7 KB)

OldJohn:
It is becoming more and more clear now that I don't have the aptitude for this.

Or patience. How many commands that you didn't have a solid understanding of have you looked up on the Reference Page?

How long did you spend on BWD or Robin2's thread or Nick Gammon's tutorial?

The actual do many things at once lesson has a technique (end time - start time = elapsed time) and the principle of non-blocking. The full understanding cannot be gained by simply reading the code or running the code unless maybe you're a Mensa member with the background to get it.

You don't have the background down at all. Taking time to learn it would save you a lot in the long run.

If you don't practice the lesson in other forms, you will lose it in a month or less. Neural connections have to GROW in order for long term memory to form and that takes time which takes patience. The way to grow connections is by doing.

Thanks everyone for all your help.
I will go over all of it once I have finished this pressing project. Time was a big problem.

To the people who say I need to go over the basics again, you are correct. As for the older people here, remember the movie 2001 A space Odyssey. "My mind is going Dave, I can feel it."

Thanks again, everyone.

@OldJohn

When you return, let’s start out slow.

Layout exactly what you need your project to do.

Or:

Tell us what you have trouble understanding/comprehending.