2 independent inputs to independent LEDs

Hi there, I am beginner and I am struggling to get 2 independent TTL (TTLA, TTLB) signals to initiate 2 independent LEDs (LEDA, LEDB) sequences.
Basically, each TTL will trigger a sequence of 4 stages of 300s of duration each with diff Hz (0, 10, 30 and 50). I am pretty sure the Hz code can be done in a different way and probably easier, but as a beginner that's what I've come across.
Unfortunately, every time I start, all will happen sequentially. If the TTLA is active first, LEDB won't start its sequence until LEDA has finished. the same the other way around. (Mind TTL inactive is HIGH).
Thanks in advance.Preformatted text
Here is the code:


byte TTLA = 2;
byte TTLB = 3;
byte LEDA = 10;
byte LEDB = 11;
byte TTLAstate, TTLBstate;
int x, z, a, b;


void setup() {


  Serial.begin (9600);

  pinMode (TTLA, INPUT);
  pinMode (LEDA, OUTPUT);
  pinMode (TTLB, INPUT);
  pinMode (LEDB, OUTPUT);
}

void loop() {



  TTLAstate = digitalRead(TTLA);
  TTLBstate = digitalRead(TTLB);


  //I dont knwo how to write IF code for 2 independet scenarios at once, so one for each cage
  //300s each stage (300s/1.5s=200 repetitions, at 0, 10, 30 and 50Hz each.

  ////----------------------------------CAGE A------------------------------------

  if (TTLAstate == LOW) {                // here should be "when is HIGH" however, TTL is by default HIGH

    digitalWrite(LEDA, LOW);            // Stage 1, 300s, no pulse
    delay (5000);

    for (z = 1; z <= 200; z++) {         // Stage 2 300s, 10Hz. 200 reps, 10 tims/sec, 10msP, 90ms IP, 0.5s OFF
      for (x = 1; x <= 10; x++) {
        digitalWrite(LEDA, HIGH);
        delay(10);
        digitalWrite(LEDA, LOW);
        delay(90);
      }
      delay(500);
    }

    for (z = 1; z <= 200; z++) {         // Stage 3 300s, 30Hz. 200 reps, 30 tims/sec, 10msP, 23ms IP, 0.5s OFF
      for (x = 1; x <= 30; x++) {
        digitalWrite(LEDA, HIGH);
        delay(10);
        digitalWrite(LEDA, LOW);
        delay(23);
      }
      delay(500);
    }

    for (z = 1; z <= 200; z++) {         // Stage 4 300s, 50Hz. 200 reps, 50 tims/sec, 10msP, 10ms IP, 0.5s OFF
      for (x = 1; x <= 50; x++) {
        digitalWrite(LEDA, HIGH);
        delay(10);
        digitalWrite(LEDA, LOW);
        delay(10);
      }
      delay(500);
    }
  }
  else {
    digitalWrite (LEDA, LOW);
  }


  ////-----------------------------CAGE B------------------------------------------------------------------

  if (TTLBstate == LOW) {                // here should be "when is HIGH" however, TTL is by default HIGH

    digitalWrite(LEDB, LOW);            // Stage 1, 300s, no pulse
    delay (5000);

    for (a = 1; a <= 200; a++) {         // Stage 2 300s, 10Hz. 200 reps, 10 tims/sec, 10msP, 90ms IP, 0.5s OFF
      for (b = 1; b <= 10; b++) {
        digitalWrite(LEDB, HIGH);
        delay(10);
        digitalWrite(LEDB, LOW);
        delay(90);
      }
      delay(500);
    }

    for (a = 1; a <= 200; a++) {         // Stage 3 300s, 30Hz. 200 reps, 30 tims/sec, 10msP, 23ms IP, 0.5s OFF
      for (b = 1; b <= 30; b++) {
        digitalWrite(LEDB, HIGH);
        delay(10);
        digitalWrite(LEDB, LOW);
        delay(23);
      }
      delay(500);
    }

    for (a = 1; a <= 200; a++) {         // Stage 4 300s, 50Hz. 200 reps, 50 tims/sec, 10msP, 10ms IP, 0.5s OFF
      for (b = 1; b <= 50; b++) {
        digitalWrite(LEDB, HIGH);
        delay(10);
        digitalWrite(LEDB, LOW);
        delay(10);
      }
      delay(500);
    }
  }
  else {
    digitalWrite (LEDB, LOW);
  }

}

Hello pablobmm
The delay() function blocks the execution of the whole sketch.
Take a look into the IDE examples and find BLINKWITHOUTDELAY as base for your project.
Have a nice day and enjoy coding in C++.

1 Like

Your code is written sequentially, so it runs sequentially. To make it independent, you need to disassemble the for loops into variables that independently track state.

Your comments are good -- Depending on TTLAstate, Stage, z,x, and time in millis(), you should be able to know when to switch LEDA HIGH or LOW.

I'd suggest starting with https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay and recoding the inner portion to do one stage of the LEDA toggling by also toggling interval between an on-time and an off-time.

void loop() {
   // modified to do the inner part of stage 2 of  https://forum.arduino.cc/t/2-independent-inputs-to-independent-leds/960485
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long interval = 0;
  unsigned long currentMillis = millis();
  static unsigned long previousMillis = 0;

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis += interval;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      interval = 10;
    } else {
      ledState = LOW;
      interval = 90;
   }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Then build up that BWOD stage 2 inner code to manage your x state variable and set the on/off intervals according to x, then expand again with the z state variable, then again with the stage state variable. Then you rename the loop() function to something TTLA-related, duplicate it for TTLB, and call both functions within a new, smaller loop():

void loop(){
   manageTTLA();
   manageTTLB();
}

As long as you have the for()s and delay()s, it will trap the attention of the code in within their loops, and prevent independent processing.

Right now, loop runs once per (something on the order of minutes), checking TTLA and TTLB only once each time.

To make it run it independently, you need to change the for/delay coding pattern and trim it down to where loop runs completely through in microseconds, so it can update the A or B LED, their intervals, x,z,a,b, or stage as needed.

thanks
I am having a look right now to the Blink.

thanks for help.
Just to add more info to my aim. I just want the TTLstate to read once, the first time that is active, then trigger the LED loop for the 20 min (4 stages of 300s) and finish. The first delay should be 300000 not 500, my bad.
thanks

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