Simple project, a bit lost on combining && and if functions in coding language

Hi,

First off, it's the first time i managed to tinker with an arduino and it's coding language. I'm trying to make a simple program to use in my classes. (Still studying to be a teacher.)

I'm used to programming in LAD and GRAFCET, so i'll explain what the program needs to run through in a similar way.

The project is a SAFE for valuable objects. It can only open when 3 conditions are met, which are in fact a button switch, a ON/OFF-Switch, and possibly a LDR-switch (still considering the last one, since it's analog).

if the button switch AND the ON/OFF-Switch and the LDR-switch are HIGH
LED 1 (Green one) burns
Servo turns 90° to open the door
if the button switch AND the ON/OFF-Switch and the LDR-switch are LOW
Servo turns 90° to close the door OR if they are stil LOW, the servo does nothing and stays closed obviously.

Now i've managed to get some functions working but when i try to read multiple inputs it gives the error: 'switchState1
' is not declared
I've tried giving multiple names to it like:
switchState1
switchState2
....

void setup() {
  int switchState1 = 0;
  int switchState2 = 0;
  int switchState3 = 0;
pinMode(2,INPUT);
//Push-button
pinMode(3,INPUT);
//LDR
pinMode(4,INPUT);
//ON/OFF SWITCH
pinMode(5,OUTPUT);
//Servo
pinMode(6,OUTPUT);
//LED RED
pinMode(7,OUTPUT);
//LED GREEN
// Toewijzen van de inputs en outputs aan pins
}

void loop() {
  // put your main code here, to run repeatedly:
switchState1 = digitalRead(2);
switchState2 = digitalRead(3);
switchState3 = digitalRead(4);}
if (switchState1 == HIGH)&&(switchState2 == HIGH)&&(switchState3 == HIGH)  {

digitalWrite(5,HIGH); //Servo


switchState = digitalRead(3);
digitalWrite(6,HIGH); //LED RED

switchState = digitalRead(4);
digitalWrite(7,HIGH); //LED GREEN
}

Now, i'm not asking to write the code for me (I need to be able to code it on my own, so i can learn it and furthermore translate it in simple exercises for my future classes), but i am asking someone willingly enough to point my wrongdoings out so i know where the faulty code is and whats wrong with it.

I'd be more than grateful for any help!

Thanks in advance.

Quick edit:
I'm using ARDUINO UNO
and ARDUINO IDE

All those variables you defined in setup are not visible in loop.
It's a concept called "scope".

Move the variable definitions outside the functions.

Thanks for the reply!

This led to some more errors and debugging, but i got it compiled without faults in the end.

Now off to the simulator.

Cheers

Also, make sure you give the pins names too.

digitalWrite(5,HIGH); //Servo

Has pin 5 really got a servo on it ?

TheMemberFormerlyKnownAsAWOL:
Also, make sure you give the pins names too.

You mean like this:

int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;

To:

int Switch = 0;
int Pushbutton = 0;
int LDR = 0;

No, pin numbers would be "const byte", and not zero, because you'll want pin zero for the serial interface.

UKHeliBob:

digitalWrite(5,HIGH); //Servo

Has pin 5 really got a servo on it ?

I reserved the pin to use it as a signalpin for the servo. I do need to do more studying on how to work with a servo though.

Also, I don't have a physical setup yet, since stores are closed today

TheMemberFormerlyKnownAsAWOL:
No, pin numbers would be "const byte", and not zero, because you'll want pin zero for the serial interface.

Hi again,

I got as far as this:

const byte Pushbutton = 2;
const byte LDR = 3;
const byte SWITCH = 4;

void setup() {
pinMode(2,INPUT);
//Push-button
pinMode(3,INPUT);
//LDR
pinMode(4,INPUT);
//ONOFF SWITCH
pinMode(5,OUTPUT);
//Servo
pinMode(6,OUTPUT);
//LED RED
pinMode(7,OUTPUT);
//LED GREEN
// Toewijzen van de inputs en outputs aan pins
}

void loop() {
  // put your main code here, to run repeatedly:
Pushbutton = digitalRead(2);
LDR = digitalRead(3);
SWITCH = digitalRead(4);
if ((Pushbutton == HIGH)&&(LDR == HIGH)&&(SWITCH == HIGH))
// Alle inputs zijn hoog
{digitalWrite(5,HIGH); //Servo
digitalWrite(7,HIGH); //LED GREEN
//Kluis gaat open en groene LED gaat branden
}
else //voorwaarden zijn niet voldaan
{
  digitalWrite(5,LOW);//Servo verder uitzoeken hoe terug te laten keren!!!!!!
  digitalWrite(6,HIGH);
  digitalWrite(7,LOW);
}
}

Now it gives me the error:
Exit status 1
Assignment of read-only variable 'Pushbutton'.

Now i did some searching and found out that 'const' is a constant variable that can't be changed.
So i figured when i want to read the status it isn't a problem, because its only reading it and not changing the value of the variable.

Or it makes the arduino think the value is always the programmed value? which doesn't make much sense to me.

pinMode(2,INPUT); You've given pin 2 a nice name; use it.

The pin name is not the same as the state on that pin.

const byte Pushbutton = 2;

You promised that the value of Pushbutton would not change by making it const(ant)

Pushbutton = digitalRead(2);

Then you break your promise by trying to change its value

Caused by a confusion between the name of the pin and the state of the pin

So its back to integers?

Update
I removed the 'const' command in 'const byte..'
now it compiles but is it right?

byte LDR = 3;
byte SWITCH = 4;
byte SERVO = 5;
byte LEDRED = 6;
byte LEDGREEN = 7;

No, it's not right.
A pin number is usually a constant.

A pin state is not.

const byte inputPin = 9;  //give the pin a name and a value that will not change
byte inputPinState;  //declare a variable to hold the state of the pin

Later in the program

inputPinState = digitalRead(inputPin);  //read the state of the pin and save it
//Constants that won't change
const byte PushbuttonPin = 2;
const byte LDRPin = 3;
const byte SwitchPin = 4;
const byte ServoLeftPin = 5;
const byte LedGreenPin = 6;
const byte LedRedPin = 7;
const byte ServoRightPin = 8;

//Variables that will change
byte PushbuttonState = 0; //Variable for reading the pushbutton status
byte LDRState = 0; //Variable for reading the pushbutton status
byte SwitchState = 0; //Variable for reading the pushbutton status
byte ServoLeftState = 5;
byte LedGreenState = 6;
byte LedRedState = 7;
byte ServoRightState = 8;

void setup() {
pinMode(PushbuttonPin,INPUT);
//Push-button
pinMode(LDRPin,INPUT);
//LDR
pinMode(SwitchPin,INPUT);
//ONOFF SWITCH
pinMode(ServoLeftPin,OUTPUT);
//Servo left
pinMode(LedGreenPin,OUTPUT);
//LED Green
pinMode(LedRedPin,OUTPUT);
//LED Red
pinMode(ServoRightPin,OUTPUT);
//LED Red
// Toewijzen van de inputs en outputs aan pins
}

void loop() {
  // put your main code here, to run repeatedly:
PushbuttonState = digitalRead(PushbuttonPin);
LDRState = digitalRead(LDRPin);
SwitchState = digitalRead(SwitchPin);
if ((PushbuttonState == HIGH)&&(LDRState == HIGH)&&(SwitchState == HIGH))
// Alle inputs zijn hoog
{digitalWrite(ServoLeftPin,HIGH); //Servo
digitalWrite(LedGreenPin,HIGH); //LED GREEN
//Kluis gaat open en groene LED gaat branden
} else //voorwaarden zijn niet voldaan
{
  digitalWrite(ServoLeftPin,LOW);//Servo verder uitzoeken hoe terug te laten keren!!!!!!
  digitalWrite(LedRedPin,HIGH);
  digitalWrite(ServoRightPin,LOW);//Servo verder uitzoeken hoe terug te laten keren!!!!!!
}
}

Like this? Don't mind the servo part. Anyways thanks in advance for the helping hand.

byte LedGreenState = 6;

Why would a LED be anything other than HIGH or LOW, unless you were doing an analogWrite to it?

digitalWrite(ServoLeftPin,HIGH); //Servo Pointless comment, similar elsewhere