It only outputs pin5 to be high

No matter what number is in pinOn it outputs pin5 to be high, I would like it so what ever number is in pinOn is what LED will turn on. Also the serial reads 4.

int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pinNum = 1;

void setup() {
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  pinOn(3);
}

void pinOn (int pinNum) {
  if (pinNum = 1) {
  digitalWrite(pin2, HIGH);                          
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);     
  digitalWrite(pin5, LOW);
  }

  if (pinNum = 2) {
  digitalWrite(pin2, LOW);                          
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, LOW);     
  digitalWrite(pin5, LOW);  
  }

  if (pinNum = 3) {
  digitalWrite(pin2, LOW);                          
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, HIGH);     
  digitalWrite(pin5, LOW);   
  }

  if (pinNum = 4) {
  digitalWrite(pin2, LOW);                          
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);     
  digitalWrite(pin5, HIGH); 
  }
  Serial.println(pinNum);
}

Every instance of β€œif (pinNum = x) {” should be

if (pinNum == x) {

With β€˜=’ you’re assigning, with β€˜==’, you’re comparing.

1 Like

You are using β€œ=” in your β€˜if’ statements where you mean β€œ==”.

1 Like

Ohhh thank you

thank you

Thanks for sharing.

Hello
To perfom a simple process flow control the usage of switch/case instruction is more convient.

Hello
study and try the small changes within your sketch.

int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pinNum = 1;

//--- code modification
enum {PinNum1 = 1, PinNum2, PinNum3, PinNum4};
void writeRelais (bool relPin2, bool relPin3, bool relPin4, bool relPin5) {
  digitalWrite(relPin2, HIGH);
  digitalWrite(relPin3, LOW);
  digitalWrite(relPin4, LOW);
  digitalWrite(relPin5, LOW);
}
//----------------------

void setup() {
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  pinOn(3);
}

void pinOn (int pinNum) {
  switch  (pinNum)
  {
    case PinNum1:
      writeRelais (HIGH, LOW, LOW, LOW);
      break;
    case PinNum2:
      writeRelais (LOW, HIGH, LOW, LOW);
      break;
    case PinNum3:
      writeRelais (LOW, LOW, HIGH, LOW);
      break;
    case PinNum4:
      writeRelais (LOW, LOW, LOW, HIGH);
      break;
  }
  Serial.println(pinNum);
}

@paulpaulson - When posting code , can you thoroughly document it, as you seem to just post working code, how is the OP suppose to expand their knowledge? Does the OP know what an ENUM is? or even understand the concept?

Hello
that is your decission to work with my propsal.
Have nice day.

This is equivalent to:

  digitalWrite(1, HIGH);
  digitalWrite(0, LOW);
  digitalWrite(0, LOW);
  digitalWrite(0, LOW);

It makes no sense to write to pins 0 and 1. I think you meant to write:

void writeRelais (bool val2, bool val3, bool val4, bool val5) {
  digitalWrite(pin2, val2);
  digitalWrite(pin3, val3);
  digitalWrite(pin4, val4);
  digitalWrite(pin5, val5);
}

That was the test for the TO :sunglasses:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.