LED Status indicator

Good day all.

I am very new to programming and seem to have some difficulty with my project.

I am running arduino 1.0.5 ide on windows 7 professional.

I am using an arduino uno rev 3.

I can upload and run the example sketches without any problems.

I think there is a problem with my sketch, it uploads fine but the code is not executing.

The sketch is below:

/* LED Status Indicator
if input 1 is low: led1 is on, led2 is off, led3 is off
if input 1 is high: led1 is off, led2 is on, led 3 is off
if input 2 is low: led1 is off, led2 is off, led3 is off
if input 2 is high: led1 is off, led2 is off, led3 is off
if input 1 and input 2 is high at the same time: led 1 is off, led2 is off, led3 blinks for half a second on and half a second off

*/

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

void setup() {
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (inp1, INPUT);
pinMode (inp2, INPUT);
}

void loop() {
digitalRead(4);
delay(10);
digitalRead(5);
delay(10);
while (inp1 = LOW)
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
delay (100);
while (inp1 = HIGH)
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
delay (100);
while (inp2 = HIGH)
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
delay (100);
while (inp1 = HIGH, inp2 = HIGH)
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,HIGH);
delay (500);
digitalWrite(led3,LOW);
delay (500);
}

Any help would be gladly appreciated, thanx in advance.

what is connected to pins 4 & 5 and how?

the code is really a mess, sorry to be so blunt.

Can you post your actual code between code tags (the # pound sign or crosshatch button above)?

Then help can come more easily...

Hi thanx for the quick reply.
Connected to pins 4 and 5 will be the output from a optoisolator connected to a alarm panels outputs.
No need to be sorry, this is bassicly my first sketch so its very rough.

/* LED Status Indicator
if input 1 is low: led1 is on, led2 is off, led3 is off
if input 1 is high: led1 is off, led2 is on, led 3 is off
if input 2 is low: led1 is off, led2 is off, led3 is off
if input 2 is high: led1 is off, led2 is off, led3 is off
if input 1 and input 2 is high at the same time: led 1 is off, led2 is off, led3 blinks for half a second on and half a second off

  */


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

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
}

void loop() {
  digitalRead(4);
  delay(10);
  digitalRead(5);
  delay(10);
  while (inp1 = LOW)
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    delay (100);
    while (inp1 = HIGH)
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
    delay (100);
    while (inp2 = HIGH)
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    delay (100); 
    while (inp1 = HIGH, inp2 = HIGH) 
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,HIGH);
    delay (500);
    digitalWrite(led3,LOW);
    delay (500);
  }

You also need to cater for both inputs low, by the way.

Then a simple (but inelegant) way of doing it is like this pseudo code:

if inp1 is high and inp2 is high
     do stuff
else if inp1 is high and inp2 is low
    do other stuff
else if .....etc etc

BTW the syntax for checking two variables at the same time is (from some old code of mine):

    if (results.value == 3148 || results.value == 1100)
 {  
       //do stuff
}

NvB the == in comparisions....

You need { and } curley braces around your 'while' functions.

F_Swart:
Connected to pins 4 and 5 will be the output from a optoisolator connected to a alarm panels outputs.
No need to be sorry, this is bassicly my first sketch so its very rough.

OK

I didn’t test this but you may want to start here for form and structure:

acquaint yourself with the Blink Without Delay sketch too.

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
int led3State = -1;
unsigned long startTime;
unsigned long blinkRate = 500UL;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop() {
  while (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    if (millis() - startTime >= blinkRate)
    {
      blinkToggle();
      startTime = millis();
    }
  }
}

void blinkToggle()
{
  led3State == 1 ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
  led3State = ( 1  ? 0 : 1 );
}

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.

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;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop() {
  while (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    if (millis() - startTime >= blinkRate)
    {
      blinkToggle();
      startTime = millis();
    }
  }
}
void blinkToggle()
{
  digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
}

Start with replacing the blocking while loops with if statements instead.

Something like....

if(...)

Else if

Else if

Else if

Else

// test here for how long pin was high
/// if pin was high for your time, do something.

F_Swart:
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.

are you talking about this being true for 1500ms:

 (digitalRead(4) ==LOW && digitalRead(5) == HIGH)

or this being true for 1500ms:

(digitalRead(4) == HIGH && digitalRead(5) ==HIGH)

or either bing true?

F_Swart:
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;

void setup()
{
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop()
{
  while (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    if (millis() - startTime >= blinkRate)
    {
      blinkToggle();
      startTime = millis();
    }
  }
}
void blinkToggle()
{
  digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
}

BulldogLowell:

F_Swart:
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.

are you talking about this being true for 1500ms:

 (digitalRead(4) ==LOW && digitalRead(5) == HIGH)

or this being true for 1500ms:

(digitalRead(4) == HIGH && digitalRead(5) ==HIGH)

or either bing true?

I think I phrased the question wrong, let me try again;

If input 5 pulses for 1.5 seconds ie.

(digitalRead(4) == HIGH && digitalRead(5) ==HIGH)

how do i need to alter the code so that when input 5 goes low again ie.

(digitalRead(4) == HIGH && digitalRead(5) ==LOW)

led3 continues to blink? led3 only resets (goes low) when

(digitalRead(4) == LOW && digitalRead(5) ==LOW)

or

(digitalRead(4) == LOW && digitalRead(5) ==HIGH)

GoForSmoke:

F_Swart:
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.
Arduino - SwitchCase

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;

void setup()
{
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop()
{
  while (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    if (millis() - startTime >= blinkRate)
    {
      blinkToggle();
      startTime = millis();
    }
  }
}
void blinkToggle()
{
  digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
}

Ok I'm totally lost.... I have absolutely no idea what you just said =(

You know that digital read returns either 0 for LOW or 1 for HIGH?

F_Swart:
Ok I'm totally lost.... I have absolutely no idea what you just said =(

So you left off here:

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;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop() {
  while (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
  }
  while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    if (millis() - startTime >= blinkRate)
    {
      blinkToggle();
      startTime = millis();
    }
  }
}
void blinkToggle()
{
  digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
}

Did you convert it to if statements yet?

something like this (not tested):

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;
boolean Blink3 = false;
boolean timerOn = false;
unsigned long pulseTimer;
unsigned long pulseStart;
unsigned long pulseInterval = 1500UL;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop() {
  if (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
  else if (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
  else if (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    if (Blink3)
    {
      blinkToggle();
    }
    else
    {
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }
  }
  else //if (digitalRead(4) == HIGH && digitalRead(5) ==HIGH), the only other possibility
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    blinkToggle();
    if (timerOn) // if the timer is on, check the elapsed time
    {
      pulseTimer = millis();
      if (pulseTimer - pulseStart >= pulseInterval)
      {
        Blink3 = true;//toggle Blink3 if timer expires
      }
    }
    else// start the timer
    {
      timerOn = true;
      pulseStart = millis();
    }
  }
}

void blinkToggle()
{
  if (millis() - startTime >= blinkRate)
  {
    digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
    startTime = millis();
  }
}

These lines below compare the states of 2 pins that can be 0 or 1 each. That is binary.
// I make a variable to hold the pin states together as 1 value,
byte pinStates = 0;
// read pin 4, get 0 or 1. I multiply it by 2 to get 0 or 2. Right, if pinStates is 2 or more then pin 4 was HIGH
pinStates = digitalRead( 4 ) * 2;
// read pin 5, I get 0 or 1. add that to pinStates then pinStates will be 0 to 3
pinStates += digitalRead( 4 );

pinStates will be 0 when both pins are LOW, 1 when 4 is LOW and 5 is HIGH, 2 when 4 is HIGH and 5 is LOW, and 3 when both are HIGH.

  while ( pinStates == 0 )   // same as (digitalRead(4) == LOW && digitalRead(5) == LOW) 

  while  ( pinStates == 1 )   // same as (digitalRead(4) ==LOW && digitalRead(5) == HIGH)

  while ( pinStates == 2 )   // same as  (digitalRead(4) == HIGH && digitalRead(5) == LOW)

  while ( pinStates == 3 )   // same as  (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)

And BTW, those while loops are blocking execution because they WAIT on the pins to change to exit.
You should be letting loop() handle what you are using while for by changing those while's to if's.

BulldogLowell:

F_Swart:
Ok I’m totally lost… I have absolutely no idea what you just said =(

So you left off here:

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;

void setup() {
 pinMode (led1, OUTPUT);
 pinMode (led2, OUTPUT);
 pinMode (led3, OUTPUT);
 pinMode (inp1, INPUT);
 pinMode (inp2, INPUT);
 startTime=millis();
}

void loop() {
 while (digitalRead(4) == LOW && digitalRead(5) == LOW)
 {
   digitalWrite(led1,HIGH);
   digitalWrite(led2,LOW);
   digitalWrite(led3,LOW);
 }
 while (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
 {
   digitalWrite(led1,HIGH);
   digitalWrite(led2,LOW);
   digitalWrite(led3,LOW);
 }
 while (digitalRead(4) == HIGH && digitalRead(5) == LOW)
 {
   digitalWrite(led1,LOW);
   digitalWrite(led2,HIGH);
   digitalWrite(led3,LOW);
 }
 while (digitalRead(4) == HIGH && digitalRead(5) ==HIGH)
 {
   digitalWrite(led1,LOW);
   digitalWrite(led2,LOW);
   if (millis() - startTime >= blinkRate)
   {
     blinkToggle();
     startTime = millis();
   }
 }
}
void blinkToggle()
{
 digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
}




Did you convert it to if statements yet?

something like this (not tested):



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;
boolean Blink3 = false;
boolean timerOn = false;
unsigned long pulseTimer;
unsigned long pulseStart;
unsigned long pulseInterval = 1500UL;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  startTime=millis();
}

void loop() {
  if (digitalRead(4) == LOW && digitalRead(5) == LOW)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
  else if (digitalRead(4) ==LOW && digitalRead(5) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
  else if (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    if (Blink3)
    {
      blinkToggle();
    }
    else
    {
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }
  }
  else //if (digitalRead(4) == HIGH && digitalRead(5) ==HIGH), the only other possibility
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    blinkToggle();
    if (timerOn) // if the timer is on, check the elapsed time
    {
      pulseTimer = millis();
      if (pulseTimer - pulseStart >= pulseInterval)
      {
        Blink3 = true;//toggle Blink3 if timer expires
      }
    }
    else// start the timer
    {
      timerOn = true;
      pulseStart = millis();
    }
  }
}

void blinkToggle()
{
  if (millis() - startTime >= blinkRate)
  {
    digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
    startTime = millis();
  }
}

Bulldog< thank you very much! I have learned a lot from you the last code you have posted works 99% correctly, the only problem is that when led3 blinks after input 2 (pin5) goes low again led2 remains on.

any suggestions?

I have tried adding the following code:

else if (digitalRead(4) == HIGH && digitalRead(5) == LOW && (Blink3) == true)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    if (Blink3)
    {
      blinkToggle();
    }
    else
    {
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }

I'm not sure I understand...

Do you mean this?

else if (digitalRead(4) == HIGH && digitalRead(5) == LOW)
  {
    digitalWrite(led1,LOW);
    if (Blink3)
    {
      digitalWrite(led2,LOW);
      blinkToggle();
    }
    else
    {
      digitalWrite(led2,HIGH);
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }

BulldogLowell:
I'm not sure I understand...

Do you mean this?

else if (digitalRead(4) == HIGH && digitalRead(5) == LOW)

{
    digitalWrite(led1,LOW);
    if (Blink3)
    {
      digitalWrite(led2,LOW);
      blinkToggle();
    }
    else
    {
      digitalWrite(led2,HIGH);
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }

Exactly! Thank you Bulldog

Hi everyone,

Since the last time I posted I have made a few adjustments to the code to allow for positive and negative triggers on the inputs.

I'm stuck with the following problem however:

If I want to read a single output from a device and use it as a input to the arduino to control the 3 led's how do I go about that?

The single input states and led outputs will have to be as follows:
Input LOW,LED1=High, LED2=LOW, LED3=LOW
Input HIGH, LED1= LOW, LED2=HIGH, LED3=LOW
Input PULSE, LED1= LOW, LED2=LOW, LED3= PULSE

The pulse length on the input is between 300-450ms

I include my code below:

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
int inp3 = 6; // input 3 on pin 6 Input 4 selects Positive Trigger when HIGH, Negative Trigger when LOW
int inp4 = 7; // input 4 on pin 7 Selects or used for single input LOW, HIGH and PULSE???????
unsigned long startTime;
unsigned long blinkRate = 400UL;
boolean Blink3 = false;
boolean timerOn = false;
unsigned long pulseTimer;
unsigned long pulseStart;
unsigned long pulseInterval = 1500UL;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (inp1, INPUT);
  pinMode (inp2, INPUT);
  pinMode (inp3, INPUT);
  pinMode (inp4, INPUT);
  startTime=millis();
}

void loop() {
  if (digitalRead(4) == LOW && digitalRead(5) == LOW && digitalRead(6) ==LOW) // Negative Trigger
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
  else if (digitalRead(4) ==LOW && digitalRead(5) == HIGH && digitalRead(6) ==LOW) // Negative Trigger
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
else if (digitalRead(4) == HIGH && digitalRead(5) == LOW && digitalRead(6) ==LOW) // Negative Trigger
  {
    digitalWrite(led1,LOW);
    if (Blink3)
    {
      digitalWrite(led2,LOW);
      blinkToggle();
    }
    else
    {
      digitalWrite(led2,HIGH);
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }
  }
  else if (digitalRead(4) == HIGH && digitalRead(5) ==HIGH && digitalRead(6) ==LOW)// the only other possibility  Negative Trigger
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    blinkToggle();
    if (timerOn) // if the timer is on, check the elapsed time
    {
      pulseTimer = millis();
      if (pulseTimer - pulseStart >= pulseInterval)
      {
        Blink3 = true;//toggle Blink3 if timer expires
        digitalWrite(led1,LOW);
        digitalWrite(led2,LOW);
   else if (digitalRead(4) == HIGH && digitalRead(5) == HIGH && digitalRead(6) ==HIGH) // Positive Trigger
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
  else if (digitalRead(4) ==HIGH && digitalRead(5) == LOW && digitalRead(6) ==HIGH) // Positive Trigger
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Blink3 = false;// This will turn off Blinking Led3
    timerOn = false;
  }
else if (digitalRead(4) == LOW && digitalRead(5) == HIGH && digitalRead(6) ==HIGH) // Positive Trigger
  {
    digitalWrite(led1,LOW);
    if (Blink3)
    {
      digitalWrite(led2,LOW);
      blinkToggle();
    }
    else
    {
      digitalWrite(led2,HIGH);
      digitalWrite(led3,LOW);// This will turn off Blinking Led3
    }
  }
  else if (digitalRead(4) == LOW && digitalRead(5) ==LOW && digitalRead(6) ==HIGH)// the only other possibility Positive Trigger
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    blinkToggle();
    if (timerOn) // if the timer is on, check the elapsed time
    {
      pulseTimer = millis();
      if (pulseTimer - pulseStart >= pulseInterval)
      {
        Blink3 = true;//toggle Blink3 if timer expires
        digitalWrite(led1,LOW);
        digitalWrite(led2,LOW);
      }
    }
    else// start the timer
    {
      timerOn = true;
      pulseStart = millis();
    }
  }
}

void blinkToggle()
{
  if (millis() - startTime >= blinkRate)
  {
    digitalRead(led3) == HIGH ? digitalWrite(led3, LOW): digitalWrite(led3, HIGH);
    startTime = millis();
  }
}

F_Swart:
Hi everyone,

Since the last time I posted I have made a few adjustments to the code to allow for positive and negative triggers on the inputs.

I'm stuck with the following problem however:

If I want to read a single output from a device and use it as a input to the arduino to control the 3 led's how do I go about that?

The single input states and led outputs will have to be as follows:
Input LOW,LED1=High, LED2=LOW, LED3=LOW
Input HIGH, LED1= LOW, LED2=HIGH, LED3=LOW
Input PULSE, LED1= LOW, LED2=LOW, LED3= PULSE

The pulse length on the input is between 300-450ms

I include my code below:

you want to add another input?