Can I use an IF statement of some flavor to create a program to give an output only if two different digital inputs happen in a certain sequence? Example: Model train approaches two infrared sensor pairs mounted on my layout 4" apart. If the train is moving in a certain direction the two sensors will activate their relays in a certain sequence as the train passes by them. If the train is going the opposite direction, those sensor relays will activate in the opposite sequence.
It can't be just an IF statement; it must be an IF this relay closes first then the second relay closes second go ahead an do this.
Anybody want to help a rookie Arduino train nut?
IF's will work. There is likely a more elegant solution but...
IF relay1 == true Then{
IF relay2 == true do something.
}
The issue here will be if there is a time limit on how long could relay1 be true without a relay2 before you reset relay1 to false. Or in it limited by the mechanical interaction of the two relays
Yes you can use an if but You need to register somewhere that the first event happened and wait then for the second event. This is often done with a state machine
enum : byte {WAITING_1, WAITING_2} state = WAITING_1;
…
void loop() {
switch (state) {
case WAITING_1:
if (<test here for event 1>) state = WAITING_2;
break;
case WAITING_2:
if (<test here for event 2>) {
// Event2 happened after Event1, do what’s needed
state = WAITING_1;
} else {
// test other stuff, if needed
}
break;
}
}
(Typed here so might have typos)
Here I use the state variable to remember what I have seen.
may need more than just a couple states
consider ** corrected**
#define LED1 10
#define LED2 11
byte ledPins [] = { LED1, LED2 };
byte butPins [] = { A1, A2 };
enum { Off = HIGH, On = LOW };
enum { S_None, S_West, S_East, S_ActWest, S_ActEast };
int state = S_None;
unsigned long msecLst;
unsigned long msec;
#define Interval 250
void
ledTgl (
byte pin )
{
Serial.print ("ledTlg: ");
Serial.println (pin);
if ( (msec - msecLst) > Interval) {
msecLst = msec;
digitalWrite (pin, ! digitalRead (pin));
}
}
void
ledsOff (void)
{
for (unsigned n = 0; n < sizeof(ledPins); n++)
digitalWrite (ledPins [n], Off);
}
void
loop (void) {
Serial.println (state);
msec = millis ();
switch (state) {
case S_None:
ledsOff ();
if (On == digitalRead (A1))
state = S_East;
if (On == digitalRead (A2))
state = S_West;
break;
case S_East:
if (Off == digitalRead (A1))
state = S_None;
if (On == digitalRead (A2))
state = S_ActEast;
break;
case S_West:
if (On == digitalRead (A1))
state = S_ActWest;
if (Off == digitalRead (A2))
state = S_None;
break;
case S_ActEast:
ledTgl (ledPins [0]);
if (Off == digitalRead (A1) && Off == digitalRead (A2))
state = S_None;
break;
case S_ActWest:
ledTgl (ledPins [1]);
if (Off == digitalRead (A1) && Off == digitalRead (A2))
state = S_None;
break;
}
}
void
setup (void) {
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(butPins); n++)
pinMode (butPins [n], INPUT_PULLUP);
for (unsigned n = 0; n < sizeof(ledPins); n++) {
digitalWrite (ledPins [n], Off);
pinMode (ledPins [n], OUTPUT);
}
Serial.println ("ready");
}
if the states are truly unique and must occur in a specific sequence and that sequence must be verified before the next operation can commence, then give each unique state a prime number value and add that value to an accumulator. The sum now tells you exactly which states have occurred prior to now.
Paul
You don’t want to add, you would need to multiply (or double check your prime numbers in detail).
For example 2+3+5+7=17 and 17 is also prime so if 17 is in your event list your accumulator can be 17 in two different ways.
The traditional way though is to use bit masks and flip a bit in a byte (or larger type unsigned long long gives you 64 bits) to mark the event occurred
When I went to school, -2- was not a prime number.
Paul
A prime number (or prime ) is a natural numbergreater than 1 that has no positive divisorsother than 1 and itself. (Said Wikipedia)
So 2 is prime …
Yeah they alla time changing what we learned so carefully…
And adding stuff, thought I had that Periodic Table nailed.
a7
Really never heard that 2 was not prime.
Aren’t you confusing with 1?
1 is not prime for some interesting mathematical reasons that lead to exclude 0 and 1 from prime.
To be sure, I'm up to speed on primes. My attempt at humor obvsly did not land. It was more an acknowledgement that I have probably forgotten more than a few things… and have increasing difficulty learning new things or unlearning the old thing.
0 and 1 are not prime by definition, as 2 is prime by definition - a natural number (positive integer) with exactly two factors…
a7
Sorry I missed the humor.
Yes and it drives interesting consequences for example One of the "points" of defining primes is to be able to uniquely and finitely prime factorize every natural number. 1 being the unit, If 1 was prime, then this would not work. (And 0 is of course special for multiplication)
Also prime are not the power of some other number and X[sup]0[/sup] is 1 so 1 would be the power of a lot of numbers
That’s examples why it was mathematically interesting to make them non prime.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.