just cant get what's wrong (noob)

I'm trying to change the button example to work with multiple buttons and LEDs.
So I decided to create a function (my first) to just "link" the input and output pins.

Am I missing something under setup?

Here's my code:

int buttonPin = 0;        // variable for the input pin
int outputPin = 0;        // variable for the output pin

void turnOnLED(int buttonPin, int outputPin) {

  // check if the button is pressed.
  // if it is, the buttonPin is HIGH:

  if (buttonPin == HIGH) {
    // turn LED on:
    digitalWrite(outputPin, HIGH);
    delay(500);

  } else {
    // turn LED off:
    digitalWrite(outputPin, LOW);
  }
}

void setup() {

  // initialize the LED pin as an output:
  pinMode(outputPin, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

}

void loop() {

 turnOnLED (22, 13);

}

You're missing a digitalRead :wink:

And you first use pinMode(0, OUTPUT) and next pinMode(0, INPUT). Reason is that both buttonPin and outputPin are set to 0.

so I just changed:

if (buttonPin == HIGH) {

to:

(digitalRead(buttonPin) == HIGH) {

I didn't understand the next part

Sterretje is saying that

int buttonPin = 0;        // variable for the input pin
int outputPin = 0;        // variable for the output pin

seems weird... are you sure your input pin and output pin are the same and connected to pin 0 (which is used by the Serial connection as well)

Also pins are better declared as cont byte rather than int

Edit: (typo --> const not cont)

J-M-L:
Sterretje is saying that

int buttonPin = 0;        // variable for the input pin

int outputPin = 0;        // variable for the output pin



seems weird... are you sure your input pin and output pin are the same and connected to pin 0 (which is used by the Serial connection as well)

Also pins are better declared as `cont byte` rather than `int`

I want the function to chnage the pin number
in this case, buttonPin to 22 and outputPin to 13

updated code:

int buttonPin;        // variable for the input pin
int outputPin;        // variable for the output pin

void turnOnLED(int buttonPin, int outputPin) {

  // check if the button is pressed.
  // if it is, the buttonPin is HIGH:

  if (digitalRead(buttonPin) == HIGH) {
    // turn LED on:
    digitalWrite(outputPin, HIGH);
    delay(500);

  } else {
    // turn LED off:
    digitalWrite(outputPin, LOW);
  }
}

void setup() {

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output:
  pinMode(outputPin, OUTPUT);

  

}

void loop() {

 turnOnLED (22, 13);

}
void setup() {

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

What is the value of buttonPin at this point?

Well then what are your pinMode() instructions referring to in the setup()?
To read from a pin it must be an INPUT, to write to a pin it must be an OUTPUT

AWOL:

void setup() {

// initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);


What is the value of buttonPin at this point?

on the updated code I hoped it to have no value and only assign one when the function is called

J-M-L:
Well then what are your pinMode() instructions referring to in the setup()?
To read from a pin it must be an INPUT, to write to a pin it must be an OUTPUT

I get what you are saying, there is no pin referal before setup sets pinModes.
but does that mean I have to "manually" set all the pins before setup()? then write the variables names in the function arguments instead of just the number?

Well you could "manually" set them to the correct pinMode() before reading and writting to the pins, in your function ...

But usually you know ahead of time what is an input and what is an output depending on how you wired things, so that's why this is usually done in the setup()

J-M-L:
Also pins are better declared as cont byte rather than int

Small typo there - "cont" should be "const" (meaning constant).

...R

J-M-L:
Well you could "manually" set them to the correct pinMode() before reading and writting to the pins, in your function ...

But usually you know ahead of time what is an input and what is an output depending on how you wired things, so that's why this is usually done in the setup()

so I can define pinModes outside setup()? if I just move the pinModes inside the function it should be fine. right?

Tell me if I understood well. When I state the function as:

void turnOnLED(int buttonPin, int outputPin)

everytime I write "buttonPin" or "outputPin" it will be replaced by the values I state when calling the function in this case 22 and 13:

turnOnLED (22, 13);

This would be the function?:

void turnOnLED(int buttonPin, int outputPin) {

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output:
  pinMode(outputPin, OUTPUT);

  // check if the button is pressed.
  // if it is, the buttonPin is HIGH:

  if (digitalRead(buttonPin) == HIGH) {
    // turn LED on:
    digitalWrite(outputPin, HIGH);
    delay(500);

  } else {
    // turn LED off:
    digitalWrite(outputPin, LOW);
  }
}

Try it :slight_smile:

(Yes you understood correctly what function parameters are)

J-M-L:
Try it :slight_smile:

It's working!! Thanks a lot all of you :smiley:

Final Code:

void turnOnLED(byte buttonPin, byte outputPin) {

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output:
  pinMode(outputPin, OUTPUT);

  // check if the button is pressed.
  // if it is, the buttonPin is HIGH:

  if (digitalRead(buttonPin) == HIGH) {
    // turn LED on:
    digitalWrite(outputPin, HIGH);
   // delay(500);

  } else {
    // turn LED off:
    digitalWrite(outputPin, LOW);
  }
}

void setup() {

}

void loop() {

  turnOnLED (22, 13);

}

Good job! :slight_smile:

J-M-L:
seems weird... are you sure your input pin and output pin are the same and connected to pin 0 (which is used by the Serial connection as well)

Putting a normally-open pushbutton on pins 0 or 1 won't affect serial unless you hold 'em down. Jamming a LED on them totally will. :slight_smile:

Well that was not really my point - hence the note in parenthesis - I was just trying to say that these are not the best pins to use (esp when you start, using serial to debug might come handy)

( his code was hinting at an output being on pin 0)