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