Triggering events with multiple sensors

Im working on a project which involves a parallel assembly line so when both sensors are triggered, it triggers a new chain of events. in terms of leds, i have a white, a blue, and the third a green. If you trigger both white, and blue, green should illuminate. However, if you trigger white then blue, it works as it should, however if you trigger blue then white it does not
im really lost on this one. please excuse the sloppy programming, im really new at this

const byte BUTTON=7;
const byte BUTTON1=9;
const byte LED=3; //blue
const byte LED1=13; // white
int LED2=12;//green
int val = 0;
int val1 = 1;

unsigned long buttonPushedMillis;
unsigned long ledTurnedOnAt;
unsigned long turnOnDelay = 0;
unsigned long turnOffDelay = 2000;
unsigned long buttonPushedMillis1;
unsigned long ledTurnedOnAt1;
unsigned long turnOnDelay1 = 2000;
unsigned long turnOffDelay1 = 10;

bool ledReady = false;
bool ledState = false;

bool ledReady1 = false;
bool ledState1 = false;

void setup() {

pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

pinMode(BUTTON1, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);

pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

pinMode(BUTTON1, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);

pinMode(LED2, OUTPUT);

}

void loop() {

unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();

if (digitalRead(BUTTON) == LOW) {

buttonPushedMillis = currentMillis;
ledReady = true;
}
{

if (ledReady) {

if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {

digitalWrite(LED, HIGH);

ledState = true;

ledTurnedOnAt = currentMillis;

ledReady = false;

}
}

if (ledState) {

if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
ledState = false;
digitalWrite(LED, LOW);
}

}

{

if (digitalRead(BUTTON1) == LOW) {

buttonPushedMillis1 = currentMillis1;
ledReady1 = true;
}
{

if (ledReady1) {
//this is typical millis code here:
if ((unsigned long)(currentMillis1 - buttonPushedMillis1) >= turnOnDelay1) {

digitalWrite(LED1, LOW);

ledState1 = true;

ledTurnedOnAt1 = currentMillis1;

ledReady1 = false;

}

if (ledState1) {

if ((unsigned long)(currentMillis1 - ledTurnedOnAt1) >= turnOffDelay1) {
ledState1 = false;
digitalWrite(LED1, HIGH);

val = digitalRead(3);

val1 = digitalRead(13);
if (val == val1) {
digitalWrite(LED2, HIGH);
delay(2000);
digitalWrite(LED2, LOW);

}
}
} }
}
}
}
}

Read the forum guidelines to see how to post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

Please edit your post to put the code in code tags after formatting the code.

Hello
please take some time and specify the functional dependencies more precisely. What shall happens when the BUTTON and BUTTON1 are pressed and released?
And post you sketch in code tags
grafik
Do you have some experience in OPP?
The task screems to be organize the sketch with object and array´s.

ok, ive auto formatted it, and the lables are in place
what im trying to do in my project is i have a toaster that toasts 2 buns at the same time, so im trying to get the 2 ir sensors, after both triggered to light 1 led, basically i want to either continue to the next step in my process if both peices of bread come out toasted, or shut off the toaster if 1 remains inside
i am extremely new to arduino, i have no experience in OPP (nor do i even know what that means!!)

//Global Variables
const byte BUTTON = 7; // our button pin
const byte BUTTON1 = 9; // our button pin
const byte LED = 3; // LED (built-in on Uno)
const byte LED1 = 13; // LED (built-in on Uno)
int LED2 = 12;
int val = 0;
int val1 = 1;

unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 0; // wait to turn on LED
unsigned long turnOffDelay = 2000; // turn off LED after this time
unsigned long buttonPushedMillis1; // when button was released
unsigned long ledTurnedOnAt1; // when led was turned on
unsigned long turnOnDelay1 = 2000; // wait to turn on LED
unsigned long turnOffDelay1 = 10; // turn off LED after this time

bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.

bool ledReady1 = false; // flag for when button is let go
bool ledState1 = false; // for LED is on or not.

void setup() {

pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

pinMode(BUTTON1, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);

pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

pinMode(BUTTON1, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);

pinMode(LED2, OUTPUT);

}

void loop() {
// get the time at the start of this loop()
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();

// check the button
if (digitalRead(BUTTON) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
{

// make sure this code isn't checked until after button has been let go
if (ledReady) {
  //this is typical millis code here:
  if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
    // okay, enough time has passed since the button was let go.
    digitalWrite(LED, HIGH);
    // setup our next "state"
    ledState = true;
    // save when the LED turned on
    ledTurnedOnAt = currentMillis;
    // wait for next button press
    ledReady = false;
  }
}

// see if we are watching for the time to turn off LED
if (ledState) {
  // okay, led on, check for now long
  if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
    ledState = false;
    digitalWrite(LED, LOW);
  }

} if (digitalRead(BUTTON1) == LOW) {
  // update the time when button was pushed
  buttonPushedMillis1 = currentMillis1;
  ledReady1 = true;
}
{

  // make sure this code isn't checked until after button has been let go
  if (ledReady1) {
    //this is typical millis code here:
    if ((unsigned long)(currentMillis1 - buttonPushedMillis1) >= turnOnDelay1) {
      // okay, enough time has passed since the button was let go.
      digitalWrite(LED1, LOW);
      // setup our next "state"
      ledState1 = true;
      // save when the LED turned on
      ledTurnedOnAt1 = currentMillis1;
      // wait for next button press
      ledReady1 = false;
    }


    // see if we are watching for the time to turn off LED
    if (ledState1) {
      // okay, led on, check for now long
      if ((unsigned long)(currentMillis1 - ledTurnedOnAt1) >= turnOffDelay1) {
        ledState1 = false;
        digitalWrite(LED1, HIGH);


        {

          if (digitalRead(BUTTON1) == LOW) {
            // update the time when button was pushed
            buttonPushedMillis1 = currentMillis1;
            ledReady1 = true;
          }
          {

            // make sure this code isn't checked until after button has been let go
            if (ledReady1) {
              //this is typical millis code here:
              if ((unsigned long)(currentMillis1 - buttonPushedMillis1) >= turnOnDelay1) {
                // okay, enough time has passed since the button was let go.
                digitalWrite(LED1, LOW);
                // setup our next "state"
                ledState1 = true;
                // save when the LED turned on
                ledTurnedOnAt1 = currentMillis1;
                // wait for next button press
                ledReady1 = false;
              }


              // see if we are watching for the time to turn off LED
              if (ledState1) {
                // okay, led on, check for now long
                if ((unsigned long)(currentMillis1 - ledTurnedOnAt1) >= turnOffDelay1) {
                  ledState1 = false;
                  digitalWrite(LED1, HIGH);




                  unsigned long currentMillis = millis();
                  unsigned long currentMillis1 = millis();

                  // check the button
                  if (digitalRead(BUTTON) == LOW) {
                    // update the time when button was pushed
                    buttonPushedMillis = currentMillis;
                    ledReady = true;
                  }
                  {

                    // make sure this code isn't checked until after button has been let go
                    if (ledReady) {
                      //this is typical millis code here:
                      if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
                        // okay, enough time has passed since the button was let go.
                        digitalWrite(LED, HIGH);
                        // setup our next "state"
                        ledState = true;
                        // save when the LED turned on
                        ledTurnedOnAt = currentMillis;
                        // wait for next button press
                        ledReady = false;
                      }
                    }

                    // see if we are watching for the time to turn off LED
                    if (ledState) {
                      // okay, led on, check for now long
                      if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
                        ledState = false;
                        digitalWrite(LED, LOW);
                      }

                    }


                    val = digitalRead(13);
                    val1 = digitalRead(3); // read input value
                    if (val == val1)
                      if (val1 == val)

                      { // check if the input is HIGH (button released)
                        digitalWrite(LED2, HIGH);
                        delay(2000);
                        digitalWrite(LED2, LOW);
                      }
                    val = digitalRead(13);
                    val1 = digitalRead(3); // read input value
                    if (val != val1)

                    { // check if the input is HIGH (button released)
                      digitalWrite(LED2, LOW);

                    }

                  }
                }
              }
            }
          }
        }

      }
    }
  }

again im very new to this, and i apologize for any issues with the format, i appreciate the help

Please look at how your code appears. It is half in a code block and half as normal text. Many posts from new users appear this way, and I don't understand how this happens.

Can you describe in detail how you posted your code, and how you used the code tags?

all i did was format, then cut and paste

Did you use code tags as already mentioned , didn´t you?

not really sure whats going on

ive simplifies the sketch, now what i really need is to add the millis function to hold the leds high for a few sec in case the bread doesnt pass the sensor at the same time, hopefully this comes through as the complete sketch

int IRSensor = 2;
int IRSensor1 = 3;
int LEDIN = 7;
int LED1IN = 8;

int LED = 12;
int LED1 = 13;
int LED2 = 11;

void setup()
{

pinMode (IRSensor, INPUT); // sensor pin INPUT
pinMode (IRSensor1, INPUT); // sensor pin INPUT
pinMode (LED, OUTPUT); // Led pin OUTPUT
pinMode (LED1, OUTPUT); // Led pin OUTPUT
pinMode (LED2, OUTPUT); // Led pin OUTPUT
pinMode (LEDIN, INPUT); // Led pin OUTPUT
pinMode (LED1IN, INPUT); // Led pin OUTPUT

}

void loop()
{
int statusSensor = digitalRead (IRSensor);
int statusSensor1 = digitalRead (IRSensor1);
int WHITE = digitalRead (LEDIN);
int BLUE = digitalRead (LED1IN);
int val = 1;
int val1 = 1;
int val2 = 2;

if (statusSensor == 1)
digitalWrite(LED1, LOW); // LED LOW

else
{
digitalWrite(LED1, HIGH); // LED High

}
if (statusSensor1 == 1)
digitalWrite(LED, LOW); // LED LOW

else
{
digitalWrite(LED, HIGH); // LED High

}
val = digitalRead(7);
val1 = digitalRead(8);

if (val + val1 == val2)

digitalWrite(LED2, HIGH); // LED LOW

else
{
digitalWrite(LED2, LOW);
}
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.