Please Help if you can understand me!

I want to be able to find a way of writing this code. All the examples don't make really fit this, and when I Have asked previously I think I have not explained my self correctly.
Hopefully someone can help here, if you can get my drift here!! :frowning:

(If button 1 is pushed)

digitalWrite(LED_13, HIGH);
delay(100);
digitalWrite(LED_12, HIGH);
delay(100);
digitalWrite(LED_13, LOW);
delay(100);
digitalWrite(LED_12, LOW);

(If Button 2 Is pushed)

digitalWrite(LED_10, HIGH);
delay(100);
digitalWrite(LED_09, HIGH);
delay(100);
digitalWrite(LED_10, LOW);
delay(100);
digitalWrite(LED_09, LOW);

What I want is to do something like this:

Void Loop () {

If Button 1 is pushed

Sequence_A ();   // (using LEDs 13 and 12)

If Button 2 is pushed

Sequence_A ();   // (Using LEDs 10 and 09)
}

Void Sequence_A (){
digitalWrite(**Either LED_13 or LED 10 depending on which button has been pressed**, HIGH);
delay(100);
digitalWrite(**Either LED_12 or LED 09 depending on which button has been pressed**, HIGH);

**And so on**

What I want is to do something like this:

What's stopping you? You could pass Sequence_A() a value (the pin number to deal with).

void loop(){
     if(button 1 is pressed) {
          sequence_A(13);
     }
     if(button 2 is pressed) {
          sequence_A(10);
     }


void sequence_A (int whichPin) {
     digitalWrite(whichPin, HIGH);
     delay(100);
     digitalWrite(whichPin, LOW);
}

I'm new to Arduino, so I'm still confused a little. Each button will control 2 LEDs, so below would run the sequence on pin 13, but how do I get it where the 2 LEDs are working

if(button 1 is pressed) {
         sequence_A(13);
         }

void sequence_A (int whichPin) {
     digitalWrite(whichPin, HIGH);
     delay(100);
     digitalWrite(whichPin, LOW);
}

I think I'm missing the point of the "int whichPin" bit?

Sorry to be a pain, It's just a bit new to me :slight_smile:

The value or values passed to the function are given names that are used in the function

void sequence_A (byte whichPin1, byte whichPin2)
{
  digitalWrite(whichPin1, HIGH);
  digitalWrite(whichPin2, HIGH);
  delay(100);
  digitalWrite(whichPin1, LOW);
  digitalWrite(whichPin2, LOW);
}

Call it like this

sequence_A(13, 12);

and

sequence_A(10, 9);

void sequence_A (int whichPin) {
digitalWrite(whichPin, HIGH);
delay(100);
digitalWrite(whichPin, LOW);
}

is a function definition. It defines a function called sequence_A, which returns nothing (void), and takes 1 arguement, of type int, and we're calling that argument whichPin, and we can refer to it from within the function by that name

This, btw, is equivalent and uses 1 byte less of SRAM at runtime - an int is 16 bit signed number. But pins are always positive numbers, and no arduino has more than 255 pins, so the 'byte' datatype (8 bit unsigned number) can be used instead.

void sequence_A (byte whichPin) {
digitalWrite(whichPin, HIGH);
delay(100);
digitalWrite(whichPin, LOW);
}

Thanks for the quick responses. I am looking forward to trying it out. I will be back!

UKHeliBob:
The value or values passed to the function are given names that are used in the function

void sequence_A (byte whichPin1, byte whichPin2)

{
 digitalWrite(whichPin1, HIGH);
 digitalWrite(whichPin2, HIGH);
 delay(100);
 digitalWrite(whichPin1, LOW);
 digitalWrite(whichPin2, LOW);
}




Call it like this


sequence_A(13, 12);



and 


sequence_A(10, 9);

Okay It's getting there, I hope this is the last question, I have assigned my pins a name:

int trlpin_A = 13;

so in my Loop I'm using

sequence_A(LEDpin_A, LEDpin_E);
[code/]

Thankfully this still seems to Compile, but my main question is on some of my sequences, I use one pin (LED) on others I use 2, so where I use only one pin how do I put that in the loop?

is this okay???

sequence_A(LEDpin_A, LEDpin_A);

Or is there another way to just use the one?

Thanks

If sequence_A() diddles with two pins, there is no way that sequence_A() is going to work right if you pass it the same number twice.

Have you noticed that the Arduino functions are named digitalRead(), analogWrite(), etc? That is the function name consists of a noun and a verb AND describes what the function does. sequence_A() does not consist of a noun and a verb and does NOT describe what the function does.

Just because one function takes two arguments does not mean that all functions take two arguments.

Okay, so how do I change it when I'm using only One pin, but the other button using the same sequence uses 2 pins?

sequence_A(LEDpin_A, **What do I put in here??**)

You don’t get it, do you? sequence_A() has ONE job. That involves two pins. There is NO way that there is the SAME job to be done that only involves one pin.

There is NOTHING stopping you from creating sillyNamedFunctionThatOnlyTakeOnePinNumber() to do the OTHER job.

PaulS:
You don't get it, do you? sequence_A() has ONE job. That involves two pins. There is NO way that there is the SAME job to be done that only involves one pin.

There is NOTHING stopping you from creating sillyNamedFunctionThatOnlyTakeOnePinNumber() to do the OTHER job.

I'm afraid to say I'm only a few weeks into Arduiono/Programing of ANY kind, I just wanted to cut down my code a little, as it was huge.

I was just asking if there was a way to use the same process that had been kindly given to me above, but on the one with only one pin used find out if there is some kind of Magic thing to do that would ignore the other byte.

I know I don't make much sense, but I am not the most intelligent guy!

You could write the function to take 2 values and compare their values. If the numbers are the same then the function executes the code for one pin. If they are different, execute the code for two different pins.

PaulS:
If sequence_A() diddles with two pins, there is no way that sequence_A() is going to work right if you pass it the same number twice.

This seems to work OK.

void loop() {
  sequence_A(10,10);

}
void sequence_A (byte whichPin1, byte whichPin2)
{
  digitalWrite(whichPin1, HIGH);
  digitalWrite(whichPin2, HIGH);
  delay(100);
  digitalWrite(whichPin1, LOW);
  digitalWrite(whichPin2, LOW);
  delay(100);
}

jcallen:
This seems to work OK.

void loop() {

sequence_A(10,10);

}
void sequence_A (byte whichPin1, byte whichPin2)
{
  digitalWrite(whichPin1, HIGH);
  digitalWrite(whichPin2, HIGH);
  delay(100);
  digitalWrite(whichPin1, LOW);
  digitalWrite(whichPin2, LOW);
  delay(100);
}

When I put the above into my sketch it did compile fine, but I thought I should check on here if it's okay to use, I don't fancy damaging anything.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We need to see how you have your buttons wired and your LEDs, make sure that the LEDs have current limit resistors in series with them.

Tom.... :slight_smile:

The circuit I'm using would take me hours to draw, it all works perfectly, I'm just trying to cut my code down a little bit.
I'm just wondering now if I can use the same pin in the loop as above.
Thanks

Hi.
If your circuit is not drawn and it is so big, I would make it common practice to draw one.

If your sketch compiles then it is okay to load.

But we need to see how you have connected the hardware together to see if the arduino or hardware will be damaged when you run the sketch.
For example how you have connected the LEDs and Buttons

Tom... :slight_smile:

That snippet works on my Nano, all it does is turn the same pin ON or OFF twice which won't hurt anything.
Some programs do that thousands of times a second.

So you don't have to write a separate function to only turn 1 led on or off.

Yes I have tried it and it works great, I have managed to cut the length of my sketch dramatically.
Thank again for the help.