Adding conditions to if statements.

I'm trying to add a condition to my program. The outline is that the output (pin 12) requires two conditions to be true in order to turn the output pin HIGH. Using an analogue sensor (LDR) and a hall effect sensor to provide output to switch on the relay. Detects the presence of a cup (No light or very low reading on LDR and presence of magnet on cup with output pin 12 high to switch on motor connected to 5V relay. Should I use the If (condition 1) && (Condition 2) true = output HIGH.

Hope I've explained this succinctly enough.

I have some errors in my code can anybody see a better way? Please help I must prove that I can this to show my students learning to code. I'm not a master of my domain yet.

const int sensorMin = 0;
const int sensorMax = 600;

int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0;
int motor = 12;
int sensor = 8;
int val;

void setup ()
{
Serial.begin(9600);
pinMode (motor, OUTPUT);
pinMode (sensor, INPUT);
}

void loop() {

{
int sensorReading = analogRead (A0);
int range = map(sensorReading, sensorMin, sensorMax, 0, 1);
switch (range){
case 0: // Low level of light is detected due to the presence of cup on the sensor.
Serial.println("min");
break;
case 1: // No cup or maximum amount of light is detected.
Serial.println("max");
break;

val = digitalRead (sensor) ; // read sensor line
sensorValue = analogRead (sensorPin);
}

if (val == HIGH)
{
digitalWrite (motor, HIGH);
Serial.println("Magnetic field detected");
}

if (sensorValue == 0);
{
digitalWrite (motor, HIGH);
}
else
{
digitalWrite (motor, LOW);
Serial.println("No magnetic field detected");
}
delay(1000);
}
}

Sorry, I realise some of my code probably has a few extra parentheses and I have changed things to trouble shoot. I'm trying to com up with a more readable version that outlines my problems at the moment.

Maybe I don't know where to declare the variables I'm trying to use.

Thankyou.

You should use code tags, it makes it easier to read and copy.

I wouldn't bother with a switch case on the range. Just use if with a greater than condition. If you want an if statement that checks two conditions then yes you use && to combine them. Or you can nest them. Whichever makes it easier for you to read and teach.

If you sort out your curly braces and remove the semicolon from the second if, the errors will go away. Pressing Ctrl+T in the IDE will help show you where you may have unnecessary braces.

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;
int motor = 12;
int sensor = 8;

You started out good, using pin and value in the names. Then, you fumbled. Are those pin number? Part numbers?

  if (val == HIGH)
  {
    digitalWrite (motor, HIGH);
    Serial.println("Magnetic field detected");
  }

  if (sensorValue == 0);

Consistency is a good thing. If if statements are to end with ;, ALL of them should. (Actually, none of them should...)

I don't see any compound, or nested, if statements. The && operator could prove useful, although nested if statements are often clearer.