Can't get actual output when, both input logic is high

Can't get actual output when, both input logic is high. Please help me, if any modification on this code.

void setup() {

pinMode(8,OUTPUT);
pinMode(12,OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);

Serial.begin(9600);
}

void loop() {

int light = digitalRead(2);
int ir = digitalRead(4);

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

}

else
{
digitalWrite(8, LOW);
digitalWrite(12, LOW);
delay(2000);
}

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

}

Can't get actual output when, both input logic is high

Where are you testing when both are HIGH ?

Here is your code auto formatted in the IDE and posted here in code tags
Please do the same in future

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  pinMode(4, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int light = digitalRead(2);
  int ir = digitalRead(4);
  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);
  }
  else
  {
    digitalWrite(8, LOW);
    digitalWrite(12, LOW);
    delay(2000);
  }
  digitalWrite(8, LOW);
  digitalWrite(12, LOW);
  delay(2000);
}

I tested in two DC motor

Can't get actual output when, both input logic is high

Where in your code are you testing when both are HIGH ?

Have you tried printing the values that you are testing ?
Are they what you expect ?

Please find the code now. 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)

void setup() {

pinMode(8,OUTPUT);
pinMode(12,OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);

Serial.begin(9600);
}

void loop() {

int light = digitalRead(2);
int ir = digitalRead(4);

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)
{

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

}

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

}

Please find the code now. I want

Still no code tags or formatting to make your code easier to read and copy

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

The same output for different input conditions ?
Is that right ? If so, then why test ir when light is HIGH ?

UKHeliBob:
Still no code tags or formatting to make your code easier to read and copy
The same output for different input conditions ?
Is that right ? If so, then why test ir when light is HIGH ?

Sorry for no code tag.
Yes, same output for different input conditions.
Can you suggest me any modification?

Try this (untested)

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  pinMode(4, INPUT);
  Serial.begin(9600);
}

void loop()
{
  byte light = digitalRead(2);
  byte ir = digitalRead(4);
  if (light == LOW)
  {
    if (ir == LOW)
    {
      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
    }
    else
    {
      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
    }
    delay(2000);
  }
  else
  {
    digitalWrite(8, LOW);
    digitalWrite(12, LOW);
    delay(2000);
  }
  digitalWrite(8, LOW);
  digitalWrite(12, LOW);
  delay(2000);
}

UKHeliBob:
Try this (untested)

  else

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

When logic, light=high & ir=low the else statement works perfectly.
But, when light=high & if=high. It does not work as previous.

My code does not work properly when both input is high (light=1 & ir=1)
Please find the code. 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 help me. If any modification needed.

void setup() {

pinMode(8,OUTPUT);
pinMode(12,OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);

Serial.begin(9600);
}

void loop() {

int light = digitalRead(2);
int ir = digitalRead(4);

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)
{

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

}

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

}

how can you tell the difference between the last 2 cases: light == 1 and ir == 0/1 since the outputs are the same in both cases: 0,0?

Topics merged

Why did you start a second topic on the same subject ?

So what you mean is "if light is HIGH you don't care what ir is because you're going to do the same thing".

So replace the last 2 if blocks with a simple

if (light==HIGH) 
{
  digitalWrite(8, LOW);
  digitalWrite(12, LOW);
  delay(2000);
}

Steve