Sub Routine

I wonder if one of you lovely helpful people can help a newbie.

I would like to make a sub routine for the following code.

int pinArray[] = {12, 11, 10, 9, 8, 7, 6};
int count = 0;
int timer = 20;

void setup(){
  for (count=0;count<7;count++) {
    pinMode(pinArray[count], OUTPUT);
  }
}

void loop() {
  for (count=0;count<6;count++) {
   digitalWrite(pinArray[count], HIGH);
   delay(timer);
   digitalWrite(pinArray[count + 1], HIGH);
   delay(timer);
   digitalWrite(pinArray[count], LOW);
   delay(timer*2);
  }
  for (count=6;count>0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(timer);
   digitalWrite(pinArray[count - 1], HIGH);
   delay(timer);
   digitalWrite(pinArray[count], LOW);
   delay(timer*2);
  }
}

So I can do other tasks in the main loop and call this as i need it.

I cant seem to suss out how it works.

Right now it's in the loop "sub routine". So create one that looks like loop, but has a different name and copy the code into it. That's the easy answer, but I get the feeling you're going to run into issues with trying to do other things in the loop because that code is blocking. If that's a problem, you'll need to learn about writing non blocking code using millis() and state machines. The former of which is demonstrated in the blink without delay example.

Do you know what a subroutine does?
From the sound of your question I think that you think it will execute a piece of code at the same time as other code is running. That is not a subroutine but a thread.
The Arduino is a single threaded machine as are all the other single code processors so it you want to make it look like it is doing two or more things at once you need to implement what is known as a state machine.

Get rid of all the delays and rewrite your code in the style of the blink without delay example.

This might also help:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Ahhh got you.

Thanks, got it working :slight_smile:

No block, as this is a state I want to lock into if one of several errors happen. Then will use in interrupt to clear.

Grumpy_Mike:
Do you know what a subroutine does?
From the sound of your question I think that you think it will execute a piece of code at the same time as other code is running. That is not a subroutine but a thread.
The Arduino is a single threaded machine as are all the other single code processors so it you want to make it look like it is doing two or more things at once you need to implement what is known as a state machine.

Get rid of all the delays and rewrite your code in the style of the blink without delay example.

This might also help:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

No sorry, I do understand the difference, in this case I want a sub to keep code tidy that's all :slight_smile: When this sub is called no other work will be done, as this is a warning state :slight_smile:

Cheers

did you google for 'C function syntax' or 'C for VB programmers' or do you want us to do that for you?

in this case I want a sub to keep code tidy that's all

OK that is fine. In C these are not called subroutines but functions but the two words mean very much the same thing.

In general subroutines do not accept or return parameters where as functions do.