If statement including several variables to be true

I have 4 switches in a coin game, and i need a statement (motorstatus7 = 1)that is true only when motorstate 7, 2, 3 and 4 is LOW.

i need the statements behind // to be included in this, and i am not sure how to.

 motorstate7 = digitalRead(kr7);
    if (motorstate7 == LOW){// != (motorstate2 == LOW) != (motorstate3 == LOW) != (motorstate4 == LOW){
      motorstatus7 = 1;

if motorstatus7 is a boolean you can do

motorstatus7 = (motorstate2 == LOW) && (motorstate3 == LOW) && (motorstate4 == LOW);

or

if ( (motorstate2 == LOW) && (motorstate3 == LOW) && (motorstate4 == LOW)) {
  motorstatus7 = 1;
} else {
  motorstatus7 = 42; // something else
}

&& is a logical AND, you can also write and if you feel it's easier to read

if ( (motorstate2 == LOW) and(motorstate3 == LOW) and (motorstate4 == LOW)) {
  motorstatus7 = 1;
} else {
  motorstatus7 = 42; // something else
}
1 Like

i was possibly thinking wrong when i described the problem, (motorstaus7=1) has to be true only when (motorstate7 == LOW) and motorstate 2,3 and 4 is not HIGH.

In this coin game there is 30V DC that is daisy chained trough all the switches, 30V on Common, then out on NC to common on the next switch and so on. Switch 2, 3 and 4 has a wire from NO to different inputs on the Arduino trough voltage dividers that gives 4,15V on the input pins. the problem is the switch for the 7 NKR win, this switch does not have a wire on the NO pin. so the end of the daisy chained switches is the input on the Arduino for the 7 NKR win switch. The problem is that all the switches makes that input go LOW.

schematic of the switches

Lars Berg Brytere.pdf (65.6 KB)

Schematic of the old circuit board with a mechanical counter relay

Capri kretskort.pdf (75.7 KB)

Here is a Youtube video when i just made a code to test the relay outputs

here is the code

const int kr2 = 3;
const int kr3 = 4;
const int kr4 = 5;
const int kr7 = 6;
const int kam = 2;
const int motor = 7;
const int gteller = 8;
// setter verdien som skal trekkes fra av kam ved gevinst
int kr2value = 2;
int kr3value = 3;
int kr4value = 4;
int kr7value = 7;

int motorstatus2 = 0;
int motorstate2 = 0;
int camstate2 = 0;
int camstatus2 = 0;

int motorstatus3 = 0;
int motorstate3 = 0;
int camstate3 = 0;
int camstatus3 = 0;

int motorstatus4 = 0;
int motorstate4 = 0;
int camstate4 = 0;
int camstatus = 0;

int motorstatus7 = 0;
int motorstate7 = 0;
int camstate7 = 0;
int camstatus7 = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(motor, OUTPUT);
  digitalWrite(motor, LOW);
  pinMode(gteller, OUTPUT);
  digitalWrite(gteller, LOW);
  pinMode(kr2, INPUT);
  pinMode(kr3, INPUT);
  pinMode(kr4, INPUT);
  pinMode(kr7, INPUT);
  pinMode(kam, INPUT);
}

void loop() {

  // kr2 utbetalings sekvens

    motorstate2 = digitalRead(kr2);
    if (motorstate2 == HIGH){
      motorstatus2 = 1;
    }
    if (motorstatus2 == 1){  
      digitalWrite(motor, HIGH);
      delay(100);
    }
    camstate2 = digitalRead(kam);
    if (camstate2 == LOW){
      kr2value --;
    } else {kr2value == kr2value;
    }
    if (kr2value == 0){  
      motorstatus2 = 0;
    }
    if (motorstatus2 == 0){  
      digitalWrite(motor, LOW);
      kr2value = 2;
    }

      // kr3 utbetalings sekvens

       motorstate3 = digitalRead(kr3);
    if (motorstate3 == HIGH){
      motorstatus3 = 1;
    }
    if (motorstatus3 == 1){  
      digitalWrite(motor, HIGH);
      delay(100);
    }
    camstate3 = digitalRead(kam);
    if (camstate3 == LOW){
      kr3value --;
    } else {kr3value == kr3value;
    }
    if (kr3value == 0){  
      motorstatus3 = 0;
    }
    if (motorstatus3 == 0){  
      digitalWrite(motor, LOW);
      kr3value = 3;
    } 

     // kr4 utbetalings sekvens

      motorstate4 = digitalRead(kr4);
    if (motorstate4 == HIGH){
      motorstatus4 = 1;
    }
    if (motorstatus4 == 1){  
      digitalWrite(motor, HIGH);
      delay(100);
    }
    camstate4 = digitalRead(kam);
    if (camstate4 == LOW){
      kr4value --;
    } else {kr4value == kr4value;
    }
    if (kr4value == 0){  
      motorstatus4 = 0;
    }
    if (motorstatus4 == 0){  
      digitalWrite(motor, LOW);
      kr4value = 4;

      Serial.println(kr4value);
    }  
   
   // kr7 utbetalings sekvens

      motorstate7 = digitalRead(kr7);
    if ((motorstate7 == LOW) && (motorstate2 == LOW) && (motorstate3 == LOW) && (motorstate4 == LOW)){
      motorstatus7 = 1;
    }
    if (motorstatus7 == 1){  
      digitalWrite(motor, HIGH);
      delay(100);
    }
    camstate7 = digitalRead(kam);
    if (camstate7 == LOW){
      kr7value --;
    } else {kr7value == kr7value;
    }
    if (kr7value == 0){  
      motorstatus7 = 0;
    }
    if (motorstatus7 == 0){  
      digitalWrite(motor, LOW);
      kr7value = 7;

      Serial.println(kr7value);
    }  
   
}

If they are not HiGH they are low too?

Can you rephrase using AND / OR

i have uploaded a schematic af the switches, there you can see that switch for 7KR is not connected on the NO pin. but the 30V that goes trough all the switches ends op as an input on the Arduino, the problem is all the switches is making this input go LOW, si i was thinking that the statement needs to be when 7kr inputs get LOW and none of the other switches is high, then the statement needs to be true.

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