0

I want When,

  • light=0, ir=0 then output will pin 8 high & pin 12 low.

  • light=0, ir=1 then output will pin 8 high & pin 12 high.

  • light=1, ir=0 then output will pin 8 low & pin 12 low.

  • light=1, ir=1 then output will pin 8 low & pin 12 low (This statement does not work).

Please find the code & suggest me, if any modification is needed.

void setup() {

    pinMode(8,OUTPUT); //logic output.
    pinMode(12,OUTPUT); //logic output.
    pinMode(2, INPUT); //value input.
    pinMode(4, INPUT); //value input.

    Serial.begin(9600);   
}


void loop() {

    int light = digitalRead(2); //value input.
    int ir = digitalRead(4); //value input.

    if (light == LOW && ir == LOW)
    {

      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
      delay(2000);

    }

    if (light == LOW && ir == HIGH)
    {

      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
      delay(2000);

    }

    if (light == HIGH && ir == LOW)
    {

      digitalWrite(8, LOW);
      digitalWrite(12, LOW);
      delay(2000);

    }

    if (light == HIGH && ir == HIGH) //This condition does not work.
    {

      digitalWrite(8, LOW);
      digitalWrite(12, LOW);
      delay(2000);

    }

    digitalWrite(8, LOW);
    digitalWrite(12, LOW);   
    delay(2000);

}
  • Please format your code using a code block (see meta.stackexchange.com/questions/22186/…). I don't know why your code does not work like you intended, but the last two IF statements (including the one that does not work) are the same for ir being LOW or HIGH, so you can just as well combine them into one, for example: if (light == HIGH) { digitalWrite(8,LOW); digitalWrite(12,LOW); delay (2000); }. Or maybe that isn't what you intended and that's where the error in your code is. – StarCat Jan 25 at 9:40
  • Also, it would be helpful to explain what your code is supposed to do and what hardware it is connected to. – StarCat Jan 25 at 9:52
  • Here is two input & two output. I want, if light=0, ir=0 then output will pin 8 high & pin 12 low. if light=0, ir=1 then output will pin 8 high & pin 12 high. Otherwise output will, pin 8 low & pin 12 low. – Shahin Kadir Jan 25 at 11:20
  • 1
    print some debug messages so you can see what‘s going on. – Sim Son Jan 25 at 12:30
  • put the delay(2000); after all of the if statements – jsotola Jan 25 at 16:10
0

You can simplify your requirements somewhat by splitting your inputs up.

light=0, ir=0 then output will pin 8 high & pin 12 low.

light=0, ir=1 then output will pin 8 high & pin 12 high.

If light is 0 then pin 8 is high. AND ir is echoed to pin 12.

light=1, ir=0 then output will pin 8 low & pin 12 low.

light=1, ir=1 then output will pin 8 low & pin 12 low

If light is 1 then both pins 8 and 12 are low, and you don't care what ir does, since it doesn't have any effect on the outputs.

So your structure could look more like:

if (light == 0) {
    digitalWrite(8, HIGH); // 8 will always be high with light == 0
    digitalWrite(12, ir); // echo the ir value to pin 12
} else {
    digitalWrite(8, LOW); // if light is 1 then everything
    digitalWrite(12, LOW);// is low regardless of ir.
}
| improve this answer | |
0

Note this is not an answer to the question (I don't see any problem initially), but to clean the code it might help to debug it (using print statements as Sim Son suggests).

Instead of all the if statements, check when pin 8 and pin 12 needs to be HIGH:

 pin 8:  (light == LOW && ir == LOW) 
 pin 12: (light == LOW && ir == LOW)  or (light == LOW && ir == HIGH)

For pin 12, the condition can be rewritten as

 pin 12: (light == LOW) || (ir == LOW or ir == HIGH)

which is equal to

 pin 12: light == LOW

So now you can change the code from the loop function to:

void loop()
{
   digitalWrite(8 , (light == LOW && ir == LOW) ? HIGH : LOW);
   digitalWrite(12, (light == LOW             ) ? HIGH : LOW);
   delay(2000);

   digitalWrite(8, LOW);
   digitalWrite(12, LOW);   
   delay(2000);

}

the x ? : z statements means: if x is true, do y, else z.

The last three statements are fixed (copied from your code).

Now we can even further reduce it, by removing the duplicated code

void loop()
{
   processLeds((light == LOW && ir == LOW), (light == LOW));
   processLeds(False                      , False         );
}

void processLeds(int conditionLed8, int conditionLed12)
{
    digitalWrite( 8, conditionLed8  ? HIGH : LOW);
    digitalWrite(12, conditionLed12 ? HIGH : LOW);
    delay(2000);
}

You pass the condition when led 8 and 12 have to be high or log to a function, which sets the LEDs.

Now you can also use much less print statements for finding your problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.