Input PULLUP Testing

Print

 

/*
Input Pull-up Serial

This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
input on pin A3,A4 and ON the LED connected to D8,D9 When two pushbutton is pressed
then activate the relay and ON the third LED .

The circuit:
- momentary switch attached from pin A3 to ground
- momentary switch attached from pin A4 to ground
- LED on pin 8 relates to A3
- LED on pin 9 relates to A4
- Relay on pin 16

Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
HIGH when the switch is open, and LOW when it is closed.

*/

void setup() {
//start serial connection
Serial.begin(115200);
//configure pin 2 as an input and enable the internal pull-up resistor
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(16, OUTPUT);

digitalWrite(16,LOW ); // Relay off
}

void loop() {

bool Flag1,Flag2;
//read the pushbutton value into a variable
int Val1 = digitalRead(A3);
int Val2 = digitalRead(A4);
//print out the value of the pushbutton
Serial.println(Val1);
Serial.println(Val2);

digitalWrite(16,LOW );

// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 8,9 when the
// button's pressed, and off when it's not:
if (Val1 == HIGH) {
digitalWrite(8, LOW);
Flag1=LOW;
} else {
digitalWrite(8, HIGH);
Flag1=HIGH;
}

if (Val2 == HIGH) {
digitalWrite(9, LOW);
Flag2=LOW;
} else {
digitalWrite(9, HIGH);
Flag2=HIGH;
}

if ((Flag1 == HIGH) && (Flag2 == HIGH))
digitalWrite(16, HIGH); // Relay on
else
digitalWrite(16,LOW ); // Relay off

}