Thanx for the code, I made a few adjustments and it works flawlessly.
Now I have a question. If the input to input 5 only pulses high for say 1.5 seconds how do I need to alter the code so that led 3 latches regardless of the state change? The altered code is below.
You need to read the time, as an unsigned long, when pin 5 goes high and subtract that from the time when pin 5 goes low and compare that to the duration (value or range) you want.
To do these different tasks together you need to use loop() to do your progressive checks and not lock code execution inside those blocking while loops. Just as Bulldog suggests, change them from while() to if().
Let loop() go around quickly. If the pin states are right you will get the results along with other checks and actions you have or may add.
When you get rid of the blocking loops you will also need to add a time based if() to get the led blinking.
Run that purely off time and a flag variable. The flag says yes or no to blink the led at all, the time check says when to change the led ON/OFF, as Bulldog showed.
If you read pin 4 and set bit 1 of a cleared ( == 0 ) variable to whatever it reads then set bit 0 to what pin 5 reads then the result will be a value of 0 to 3 exactly equivalent to
(digitalRead(4) == LOW && digitalRead(5) == LOW) equals 0
(digitalRead(4) == LOW && digitalRead(5) == HIGH) equals 1
(digitalRead(4) == HIGH && digitalRead(5) == LOW) equals 2
(digitalRead(4) == HIGH && digitalRead(5) == HIGH) equals 3
You can use a switch-case statement to handle that quick and clean.
And when you read pin 5, use a variable to keep track of the last reading because it's certain changes you want to time.
I don't know what you mean about pin 3 latching. Can you explain?
Also pin 3 is an OUTPUT. You read an OUTPUT? And all this time most of us keep the state in a variable.
All those int variables to hold small numbers won't hurt but making it a habit to use int someday may.
byte will hold 0 to 255 and takes 1 byte of RAM
char or short will hold -128 to 127 in 1 byte of RAM
int takes 2 bytes of RAM and will hold -32768 to 32767
word takes 2 bytes and will hold 0 to 65535
for now just say that long and unsigned long lets you work in with billions.
Variables are made of series' of bits. Each bit is 0 or 1 (OFF or ON, LOW or HIGH, FALSE or TRUE).
Bit 0 is the low order bit, bit 0 1 is 1. Each next bit in line is worth 2x the last.
If all 8 bits of a byte are 1's, binary 11111111, then the total value is 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255.
if any of those bits are 0 then take the corresponding value away from the total. Binary 11 is decimal 3.
In time you will want to learn to use bitwise logic to manipulate bits (truth tables if you want) as patterns.
But for now, just 1 bit in position 0 will do.
byte ledState = 0; // use ledState to keep track of what you set the led to, also to change and write the pin with.
There is a logical operation that compares 2 bits and returns 0 if they are the same and 1 if they are different.
The operation is XOR. The code symbol is ^. It is the quick way to "flip a bit".
ledState = ledState ^ 1; // bit 0 in ledState is compared to 1,
// if they are different, 0 ^ 1, ledState gets set to 1.
// if they are the same, 1 ^ 1, ledState gets set to 0
// whatever ledState was, it changes to the opposite.
ledState ^= 1; // same thing, less typing
ledState = !ledState; // same result but using NOT ( ! ) logic instead
int led1 = 13; //led 1 on pin 13
int led2 = 12; //led 2 on pin 12
int led3 = 11; //led 3 on pin 11
int inp1 = 4; // input 1 on pin 4
int inp2 = 5; // input 2 on pin 5
unsigned long startTime;
unsigned long blinkRate = 500UL;
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (inp1, INPUT);
pinMode (inp2, INPUT);
while (digitalRead(4) == LOW && digitalRead(5) == LOW)
while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
if (millis() - startTime >= blinkRate)
startTime = millis();
digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);