if clause on a sensor

How do i put an if clause reading a sensor, ie.,

if (light == 0);
{
digitalWrite(LED2, HIGH);

My code is a "keylock."
I'm trying to make it read multiple sensors, and if it is a one certain number, light a led.
Also if all the leds are lit from all the sensors, blink an led.

Here is my code

#define LED1 13
#define LED2 12
#define LED3 11
#define LED 10

int force;
int flex;
int light;

void setup()
{
pinMode(LED, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(force, INPUT);
pinMode(flex, INPUT);
pinMode(light, INPUT);
}
void loop()
{
force = analogRead(0);
flex = analogRead(2);
light = analogRead(1);

if (light == 0)
{
digitalWrite(LED2, HIGH);

}
if (flex == 10)
{
digitalWrite(LED3, HIGH);
}

if (force == 400)
{
digitalWrite(LED1, HIGH);
}

if (force == 400, flex == 10, + light == 0)
{
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
delay(1000);
}
}

When I upload the code, I make the sensor the value in the code, and it doesn't light the led.

Any Help?

Thank You,
qtechknow

Thanks for the a ton for the help :slight_smile: 8) :D. I got the sensor part, but does anyone know the blinking part? :relaxed:

Here is my new code

#define LED1 13
#define LED2 12
#define LED3 11
#define LED 10

int force;
int flex;
int light;

void setup()
{
pinMode(LED, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(force, INPUT);
pinMode(flex, INPUT);
pinMode(light, INPUT);
}
void loop()
{
force = analogRead(0);
flex = analogRead(2);
light = analogRead(1);

if (light < 1)
{
digitalWrite(LED2, HIGH);

}
if (flex > 8)
{
digitalWrite(LED3, HIGH);
}

if (force > 300)
{
digitalWrite(LED1, HIGH);
}

if (force > 300, flex > 8, light < 0)
{
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
delay(1000);
}
}

The last part is where I'm having problems.

When you post code select it and then hit the # icon, it will then be in a box and will not contain smilies.

if (force > 300, flex > 8, light < 0)

Is not the way you combine things you compare, replace each comma with the symbol &&

Thank you Grumpy Mike! I added a few more sensors that work correctly lighting an led, but still the same error on the blinking led. Here is the new code

#define LED1 13
#define LED2 12
#define LED3 11
#define LED4 9
#define LED5 8
#define LED6 7
#define LED 10

int force;
int flex;
int light;
int button;
int pot;
int switches;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(pot, INPUT);
  pinMode(switches, INPUT);
  pinMode(force, INPUT);
  pinMode(flex, INPUT);
  pinMode(light, INPUT);
  pinMode(button, INPUT);
  
}
void loop() 
{
  force = analogRead(0);
  flex = analogRead(2);
  light = analogRead(1);
  button = analogRead(3);
  switches = analogRead(5);
  pot = analogRead(4);
 
  if (light < 1)
  {
     digitalWrite(LED2, HIGH);
     
  }
     if (flex > 8 )
     {
       digitalWrite(LED3, HIGH);
     }
     
     if (force > 300)
   {
       digitalWrite(LED1, HIGH);
     }
     if (button > 1022)
     {
       digitalWrite(LED4, HIGH);
     }
     
     if (pot > 1022)
     {
       digitalWrite(LED5, HIGH);
     }
      
     if (switches > 1022)
     {
       digitalWrite(LED6, HIGH);
     }
     
    if (force > 300 && flex > 8 && light < 1 && button > 1022 && pot > 1022 && switches > 1022)
     {
       digitalWrite(LED, HIGH);
       delay(10);
       digitalWrite(LED, LOW);
       delay(1000);
     }
}

That blink is far too short to see, just 10mS on and 1 second off and it never comes on again. Replace your blink code

if (force > 300 && flex > 8 && light < 1 && button > 1022 && pot > 1022 && switches > 1022)
     {
       digitalWrite(LED, HIGH);
       delay(10);
       digitalWrite(LED, LOW);
       delay(1000);
     }

with:-

if (force > 300 && flex > 8 && light < 1 && button > 1022 && pot > 1022 && switches > 1022)
     {
   for(int 1=0; i<10; i++ {
       digitalWrite(LED, HIGH);
       delay(400);
       digitalWrite(LED, LOW);
       delay(400);
 }
     }

Of course make sure that you test it in a state where all those conditions apply.

Thanks again grumpy mike! When I change the code to what you said it should be, I get the error message "expected unqualified id before numeric constant." Any help? Here is my new code.

#define LED1 13
#define LED2 12
#define LED3 11
#define LED4 9
#define LED5 8
#define LED6 7
#define LED 10

int force;
int flex;
int light;
int button;
int pot;
int switches;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(pot, INPUT);
  pinMode(switches, INPUT);
  pinMode(force, INPUT);
  pinMode(flex, INPUT);
  pinMode(light, INPUT);
  pinMode(button, INPUT);
  
}
void loop() 
{
  force = analogRead(0);
  flex = analogRead(2);
  light = analogRead(1);
  button = analogRead(3);
  switches = analogRead(5);
  pot = analogRead(4);
 
  if (light < 1)
  {
     digitalWrite(LED2, HIGH);
     
  }
     if (flex > 8 )
     {
       digitalWrite(LED3, HIGH);
     }
     
     if (force > 300)
   {
       digitalWrite(LED1, HIGH);
     }
     if (button > 1022)
     {
       digitalWrite(LED4, HIGH);
     }
     
     if (pot > 1022)
     {
       digitalWrite(LED5, HIGH);
     }
      
     if (switches > 1022)
     {
       digitalWrite(LED6, HIGH);
     }
     
    if (force > 300 && flex > 8 && light < 1 && button > 1022 && pot > 1022 && switches > 1022)
     {
       for(int 1=0; i<10; i++)
       digitalWrite(LED, HIGH);
       delay(400);
       digitalWrite(LED, LOW);
       delay(400);
     }
}
 pinMode(pot, INPUT);
  pinMode(switches, INPUT);
  pinMode(force, INPUT);
  pinMode(flex, INPUT);
  pinMode(light, INPUT);
  pinMode(button, INPUT);

These all set digital pin zero to be an input.

force = analogRead(0);
  flex = analogRead(2);
  light = analogRead(1);
  button = analogRead(3);
  switches = analogRead(5);
  pot = analogRead(4);

And then the pin numbers all change.
If they're anaolgue inputs, they don't need "pinMode"s

if (light < 1)

analogRead only returns zero or positive numbers.

It should be
for(int i=0; i<10; i++)
not
for(int 1=0; i<10; i++)

Yes that
light < 1
will ensure that nothing happens most of the time in that last statement.

Thanks for the help. The led still doesn't blink though. Here's the code again! Any more help?

#define LED1 13
#define LED2 12
#define LED3 11
#define LED4 9
#define LED5 8
#define LED6 7
#define LED 10

int i;
int force;
int flex;
int light;
int button;
int pot;
int switches;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(pot, INPUT);
  pinMode(switches, INPUT);
  pinMode(force, INPUT);
  pinMode(flex, INPUT);
  pinMode(light, INPUT);
  pinMode(button, INPUT);
  
}
void loop() 
{
  force = analogRead(0);
  flex = analogRead(2);
  light = analogRead(1);
  button = analogRead(3);
  switches = analogRead(5);
  pot = analogRead(4);
 
  if (light < 1)
  {
     digitalWrite(LED2, HIGH);
     
  }
     if (flex > 8 )
     {
       digitalWrite(LED3, HIGH);
     }
     
     if (force > 300)
   {
       digitalWrite(LED1, HIGH);
     }
     if (button > 1022)
     {
       digitalWrite(LED4, HIGH);
     }
     
     if (pot > 1022)
     {
       digitalWrite(LED5, HIGH);
     }
      
     if (switches > 1022)
     {
       digitalWrite(LED6, HIGH);
     }
     
    if (force > 300 && flex > 8 && light < 1 && button > 1022 && pot > 1022 && switches > 1022)
     {
       for(int i=0; i<10; i++) 
       digitalWrite(LED, HIGH);
       delay(400);
       digitalWrite(LED, LOW);
       delay(400);
     }
}
  /code]
 if (light < 1)

See my earlier reply.

My guess is that you are never getting conditions to pass that if() statement.
So add some print statements to your code so you can look at the values before the compare

 Serial.print(" force -> "); Serial.print(force); 
 Serial.print(" light -> "); Serial.print(light);
 Serial.print(" button -> "); Serial.print(button);
 Serial.print(" pot -> "); Serial.print(pot);
 Serial.print(" switches -> "); Serial.println(switches);
if (force > 300 && flex > 8 && light < 1 && button > 1022 && pot > 1022 && switches > 1022)
     {
       for(int i=0; i<10; i++) 
       digitalWrite(LED, HIGH);
       delay(400);
       digitalWrite(LED, LOW);
       delay(400);
     }

Also add a Serial.begin(9600);
in the setup function.

Run this with the monitor open, my guess will be that as AWOL says that light will never be equal to zero so you never get into the flashing LED code. There may be some other values that don't beet your criteria.