output dependent on two inputs.

I've checked through other examples and havent found much of a solution.

i'm building a coin slot with two infrared sensors positioned between the two top most coins. the idea is that when both sensors read LOW, it directs power to a transistor that turns on the rest of the system of attinys. as a test, i'm lighting up a led instead of switching on a transistor.

however my code is not working right. when I upload, the led lights up without the sensors being low, when I block the sensors, no change.

here is my code. I've tried a few different things. this is what I settled on with no change.

int inputPin = 3;
int inputPin2 = 4;
int transPin = A1;

void setup() {
// put your setup code here, to run once:
pinMode(inputPin, INPUT);
pinMode(inputPin2, INPUT);
pinMode(transPin, OUTPUT);
}

void loop() {
if (digitalRead(inputPin) == HIGH && digitalRead(inputPin2) == HIGH);
{
digitalWrite(transPin, LOW);
}
if (digitalRead(inputPin) == LOW && digitalRead(inputPin2) == LOW);

{
digitalWrite(transPin, HIGH);
}

}
/td]

coinslot_basic_builder.ino (437 Bytes)

I copied the OP's code, put it in the IDE, hit Control-T to format it, and then copied it here into code tags.

OP, that's a skill you should pick up soon. Especially the formatting part. It will save you more times than you can count. It makes errors with braces and nesting stand out like a sore thumb.

int inputPin = 3;
int inputPin2 = 4;
int transPin = A1;

void setup() {
  // put your setup code here, to run once:
  pinMode(inputPin, INPUT);
  pinMode(inputPin2, INPUT);
  pinMode(transPin, OUTPUT);
}

void loop() {
  if (digitalRead(inputPin) == HIGH && digitalRead(inputPin2) == HIGH);
  {
    digitalWrite(transPin, LOW);
  }
  if (digitalRead(inputPin) == LOW && digitalRead(inputPin2) == LOW);

  {
    digitalWrite(transPin, HIGH);
  }
}

Your problem is the semicolon on the end of your if statements. That means do nothing. So if (this is true) do nothing. And then the bits after that you think are controlled by the if statement aren't because of the semicolons.

:open_mouth:

I never realized that!

if I hook up buttons and press them to read LOW, it works find...

I wonder if perhaps i need to do a little more reading on the proximity sensors i'm using. i'm using GP2Y0D8xx sensors.

i remember using them before and I was able to write a code that worked like a push button and it worked fine... that was years ago. i cant remember how i did it.