Can someone help me with the following question,
i want to have a and function with 2 or more inputs on an arduino uno.
e.g. input 3, 4, 5 together must have an and function.
What makes you say that the test does not work ?
Please post a simple but complete sketch that shows what you are seeing
Do you want to perform a logical and or a binary and ?
/*
if 2 inputs HIGH, sketch must continue.
*/int Resist = 3;
int Bat_test = 4; // battery test
int Led = 5;
#include <Wire.h> // I2C bus
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(){
pinMode (Resist, INPUT_PULLUP);
pinMode (Bat_test, INPUT_PULLUP);
pinMode (Led, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
if ((Bat_test) && (Resist) == HIGH){ // testing both inputs are High
// if ((Bat_test == HIGH) && (Resist == HIGH)){ also try this, testing both inputs are High
Wire.beginTransmission(27);
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" No Action ");
lcd.setCursor(0,1);
lcd.print(" Test Case ");
Wire.endTransmission();
}
else
digitalWrite(Led, HIGH);
}
this is a part of the sketch, I want to use a logical and.
The commented out one is correct. The one that isn't commented out is checking whether Bat_test !=0, that part will always be true.
You are testing the pin numbers. You have to
int pressed = digitslRead(Bat_test);
and use pressed as a term in your logic. You have to read the pin, and use the value that riding yields.
Both switches,
and adding, the commented out form, but with the readings, not the pin numbers. THX @Dave_Lowther, didna see the other problem.
a7
Yes, sorry, I just replied quickly without realising the correct syntax would be:
void loop() {
if ((digitalRead(Bat_test) == HIGH) && (digitalRead(Resist) == HIGH)){
Wire.beginTransmission(27);
That would be logically correct if the intention is to check that nothing is pulling either of the two input pins low. IDK what the intention is.
Might as well go all the way
// somewhere up top
# define PRESSED LOW
// and later in loop() or other function
bool batTestPressed = digitalRead(Bat_test) == PRESSED;
bool resistPressed = digitalRead(Resist) == PRESSED;
if (batTestPressed && resistPressed) {
// now here if both buttons are pressed, assuming they pull down their input pins
}
// or if the OP really meant it, when neither switch is being pressed
if (!batTestPressed && !resistPressed ) {
// now here if both buttons are pressed, assuming they pull down their input pins
}
This results in easy reading, keeps the API happy and allows changing just the defined constant PRESSED if someone uses switches wired wrong differently.
a7
That's the way I do things. Well better.
Thank you all, I used Dave's solution and it works fine.
@antonio_rapido Suggestion, for education, you consider carefully both technically correct forms you've been offered, and if you don't understand either, or the advantages thereof, you question further, before indicating which is your preferred solution by marking the appropriate post.