Hello,
I am trying to do a simple check - setting an output and read it's state, but something is wrong in my code.
I have 2 output pins, changing their state and reading it, but the problem is that I am always read high for both of them. ![]()
I would like to toggle the outputs and read them constantly, could you please point out my mistake?
Here is my code:
const int pinOut1 = 5;
const int pinOut2 = 6;
const int read1 = 3;
const int read2 = 4;
// state of the outputs
int out1State;
int out2State;
void setup() {
// serial comm at 9600bps
Serial.begin(9600);
// pin config
pinMode(pinOut1, OUTPUT);
pinMode(pinOut2, OUTPUT);
pinMode(read1, INPUT);
pinMode(read2, INPUT);
}
void loop() {
out1State = digitalRead(read1);
out2State = digitalRead(read2);
// ptint the outputs state in the console
Serial.print("\nOut 1 = ");
Serial.print(out1State);
Serial.print("\n Out 2 = ");
Serial.print(out2State);
digitalWrite(pinOut1, HIGH);
digitalWrite(pinOut2, LOW);
delay(500);
Serial.print("\nOut 1 = ");
Serial.print(out1State);
Serial.print("\n Out 2 = ");
Serial.print(out2State);
delay(500);
digitalWrite(pinOut1, HIGH);
digitalWrite(pinOut2, LOW);
delay(500);
Serial.print("\nOut 1 = ");
Serial.print(out1State);
Serial.print("\n Out 2 = ");
Serial.print(out2State);
delay(500);
digitalWrite(pinOut1, HIGH);
digitalWrite(pinOut2, HIGH);
delay(500);
Serial.print("\nOut 1 = ");
Serial.print(out1State);
Serial.print("\n Out 2 = ");
Serial.print(out2State);
delay(500);
}
and here is the console output:
Out 1 = 0
Out 2 = 0
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1
Out 1 = 1
Out 2 = 1