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);
}
}