Need help with a "logic gate"

Hello,
I am trying to program a Sparkfun Redboard to act as a 3-input to 4- output converter (If you know what I mean).

The following is supposed to happen:

If Input 1 and input 2 are on, Output 3 turns on.
If Only Input 1 is on, Output 1 turns on.
If only Input 2 is on, then Output 2 turns on.
And finally, if Input 3 is on, Output 4 turns on.

The issue I am having has to do with Inputs 1 and 2.

If Input 2 is on, then Output 3 turns on
And if Input 3 is on, then Input 1 turns on.

I have no clue what is causing the bug to occur, but I'm sure someone can help me fix this.

Here is the code:

//Pin Setup
int F1In = 14;
int F2In = 15;
int F3In = 16;
int Out1 = 4;
int Out2 = 5;
int Out3 = 6;
int Out4 = 7;

void setup() {
  // put your setup code here, to run once:
  //Initialize Pins
pinMode(F1In, INPUT_PULLUP);
pinMode(F2In, INPUT_PULLUP);
pinMode(F3In, INPUT_PULLUP);
pinMode(Out1,OUTPUT);
pinMode(Out2, OUTPUT);
pinMode(Out3, OUTPUT);
pinMode(Out4, OUTPUT);
//Activate Serial
Serial.begin(9600);
}

void loop() {
  
  if (digitalRead(F1In)== HIGH && digitalRead(F2In) == HIGH){
    Serial.print("3");
    
  }
else if (digitalRead(F1In) == HIGH) {
  Serial.print("1");
  
}
else if (digitalRead(F2In) == HIGH){
  Serial.print("2");
}
else if (digitalRead(F3In) == HIGH){
  Serial.print("4");
}
}

void Clear(){
  digitalWrite(Out1, LOW);
  digitalWrite(Out2, LOW);
  digitalWrite(Out3, LOW);
  digitalWrite(Out4, LOW);
}

If anybody can help me with this, Please tell me what to do or better yet, fix this code so it will work as intended.

Because of your use of INPUT_PULLUP in pinMode() all 3 inputs will normally be HIGH

The switches should be wired to take the pin LOW when on and you should test for LOW rather than HIGH to detect a closed switch

I'm not sure if that would apply to an arduino communicating directly to another arduino
but I did some minor changes to my hardware, and I somehow fixed it

Hi,
I used slides switchs with your code and it worked as it should.
What kind of switchs are you using to simulate on/off on the inputs?

This is the first time that you have mentioned another Arduino. Is there anything else that you would like to share such as how the two are connected, the sketch that is running on the second one and what the purpose of the second Arduino and the whole project is

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