&& (logical and) HELP

Greeting,

My name is Dejan from Serbia. Getting to learn Arduino. I tried to write the code for lighting LEDs. To stay ON after the release button 6s. I need help with coding and wiring diagram.

I tried with this code

int LP1 = 3;
int LP2 = 4;

int LEDG = 12;
int LEDB = 13; 


void setup() {
  pinMode(LP1, INPUT);
  pinMode(LP2, INPUT);

  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  
  if(digitalRead(LP1) == HIGH && digitalRead(LP2) == LOW){
    digitalWrite(LEDG, HIGH);
    delay(6000);
  }
  if(digitalRead(LP1) == LOW && digitalRead(LP2) == HIGH){
    digitalWrite(LEDB, LOW);
    delay(6000);
  }
  if(digitalRead(LP1) == HIGH && digitalRead(LP2) == HIGH){
    digitalWrite(LEDB, LOW);
    digitalWrite(LEDG, HIGH);
    delay(1000);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, LOW);
    delay(1000);
    digitalWrite(LEDB, LOW);
    digitalWrite(LEDG, HIGH);
    delay(1000);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, LOW);
    delay(1000);
  }
}

Thanks in advance Dejan

You are using && correctly. It's impossible to help without knowing what your specific problem is.

The first thing I would do is add some extra parentheses to make sure the order of operations is what you expect...

http://web.ics.purdue.edu/~cs240/misc/operators.html

the second thing I would do is use INUPT_PULLUP

int LP1 = 3;
int LP2 = 4;

int LEDG = 12;
int LEDB = 13; 


void setup() {
  pinMode(LP1, INPUT_PULLUP);
  pinMode(LP2, INPUT_PULLUP);

  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  
  if((digitalRead(LP1) == HIGH) && (digitalRead(LP2) == LOW)){
    digitalWrite(LEDG, HIGH);
    delay(6000);
  }
  if((digitalRead(LP1) == LOW) && (digitalRead(LP2) == HIGH)){
    digitalWrite(LEDB, LOW);
    delay(6000);
  }
  if((digitalRead(LP1) == HIGH) && (digitalRead(LP2) == HIGH)){
    digitalWrite(LEDB, LOW);
    digitalWrite(LEDG, HIGH);
    delay(1000);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, LOW);
    delay(1000);
    digitalWrite(LEDB, LOW);
    digitalWrite(LEDG, HIGH);
    delay(1000);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, LOW);
    delay(1000);
  }
}

MarshaJ847: The first thing I would do is add some extra parentheses to make sure the order of operations is what you expect...

Why? The operator precedence is both well defined and well-known, comparisons bind tighter than logical ops.

Hello,

Attempted to write code that will control the 4 relays board.
Activation of the relay depends on the deployment of active limit switch which has 4 pcs.
Sometimes it is an active one limit switch, and sometimes the two limit switches.
After complains that limit switches need to activate a particular relay 4 relay board. But when the limit switch that initiated the relay is inactive it stays on as long as does not activate another limit switch or a weather break of say 6s.

To complete this process, which I have listed above started, I need more to over one switch when activated after the 15s starts the process that I have described.

If the same switch is not active. terminate the process and again to engage one by one two relays until they are activated by a limit switch.

To give you explain to me why the project.

The relays will be used for automatic control two DC motors that operate the tool at two axes on a lathe for wood. Something like CNC lathe just as reluctant at G-code is already the template on which slides behind the slider.

Something like this

I apologize for the poor English.

the second thing I would do is use INUPT_PULLUP

Yeah? How well does that (spelling) work for you?

MarkT: Why? The operator precedence is both well defined and well-known, comparisons bind tighter than logical ops.

Why ?

Because it makes the order in which comparisons are done much clearer to anyone reading the code. Whilst it is true that operator precedence is well defined it is not the case that they are well known.

UKHeliBob:
Why ?

Because it makes the order in which comparisons are done much clearer to anyone reading the code. Whilst it is true that operator precedence is well defined it is not the case that they are well known.

I have to disagree. The operator precedence is both well defined and well documented. What it is not, and this is the point that I think UKHeliBob is trying to make, is well remembered. I, for one, know that multiplication and division have higher precedence than addition and subtraction, and that comparisons are higher precedence than logical operations, but the exact order of all the operations is too much for me to remember.

What I CAN remember is that if I use parentheses, I can make the order be what I want. So, if there is any doubt, I use more parentheses than strictly necessary.

Hello,

I tried with this code

int left = 3;
int right = 4;
int up = 5;
int down = 6;

int RL4 = 10;
int RL3 = 11;
int RL2 = 12;
int RL1 = 13; 


void setup() {
  pinMode(left, INPUT_PULLUP);
  pinMode(right, INPUT_PULLUP);
  pinMode(up, INPUT_PULLUP);
  pinMode(down, INPUT_PULLUP);

  pinMode(RL4, OUTPUT);
  pinMode(RL3, OUTPUT);
  pinMode(RL2, OUTPUT);
  pinMode(RL1, OUTPUT);
}

void loop() {
  
  if((digitalRead(left) == LOW) && (digitalRead(right) == HIGH)&& (digitalRead(up) == HIGH)&& (digitalRead(down) == LOW)){
    digitalWrite(RL2, HIGH);
    
    }
  if((digitalRead(left) == HIGH) && (digitalRead(right) == LOW)&& (digitalRead(up) == HIGH)&& (digitalRead(down) == LOW)){
    digitalWrite(RL2, LOW);
    delay(1000);
    digitalWrite(RL3, HIGH);
    delay(2000);
    digitalWrite(RL3, LOW);
    
    }    
  if((digitalRead(left) == HIGH) && (digitalRead(right) == LOW)&& (digitalRead(up) == LOW)&& (digitalRead(down) == LOW)){
    digitalWrite(RL2, HIGH);
    delay(2000);
    
    }
  if((digitalRead(left) == LOW) && (digitalRead(right) == HIGH)&& (digitalRead(up) == LOW)&& (digitalRead(down) == LOW)){
    digitalWrite(RL2, LOW);
    delay(2000);
    
    }


  }

Works pretty well what I imagined.

I tried to use a State function for keys and relays but we do not succeed. If you have other suggestions, let me know.

Thank you

Works pretty well what I imagined.

I tried to use a State function for keys and relays but we do not succeed.

So post the code that wasn't good for you, and say why it fell short of your expectations.

I'm making these days a mechanism for moving the tool. will install the switches and relays engines and try.

I think it will work just maybe minor adjustments, about the length of delay().

Although I think this is not even close to that code should I use. To start might be good until you overcome ARDUINO better.