Using two switches to control large 7 segment display with ULN2803A

Hi there,

I've been working on a project which for me is slightly complictaed.

Basically I've got 2 inputs that are push buttons and 3 outputs, 2 leds and a seven segment display.

I'm using 2 ULN2803A to provide power to my large 7 segment anode display and large LED, I have a 12v supply powering my Arduino uno board and this 12v is coming out of the VIN Pin.

The function that I want is that button 1 turns on the big LED.
Button 2 blinks the small led once and also starts a 3, 2, 1 sequence (x4) and turns off the big LED. However it should not be possible for a press of button 2 to do any of this unless bthe big LED is ON.

The system is working OK, but I need it to work long term 24/7 for years, can anyone see any potential probelms in my design and code?

It's really important that the main sequence attached to button 2 can never be started unless the big LED is on which is done by pressing button 1.

Any help would be much appreciated.

Many thanks

example_script.ino (4.89 KB)

For those interested, here is his code embedded directly here:

int swhPin = 2;         // switch pin
int led1 = 12;       // Main Large LED
int swh2Pin = 3;       // switch pin #2
int blueLed = 13;       // green led 
int state = LOW;      // the current state of the output pin
int state2 = HIGH;      // the current state of the output pin

int reading;           // the current reading from the input pin
int reading2;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin
int previous2 = HIGH;   // the previous reading from the input pin

// these declare pins for the 7 segment display.
int topright = 8; 
int buttom = 5;
int Top= 9;
int middle = 11;
int btmRight= 6;
int tpLeft = 10;
int btmLeft = 4;
int dot = 7;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 1000;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(swhPin, INPUT); //switch is an input
  pinMode(led1, OUTPUT); //LED is an output
  pinMode(swh2Pin, INPUT); //switch2 is an input
  pinMode(blueLed, OUTPUT); //Blue led is a output

  pinMode(topright, OUTPUT); // All segments of display are outputs
  pinMode(buttom, OUTPUT); 
  pinMode(Top, OUTPUT); 
  pinMode(middle, OUTPUT); 
  pinMode(btmRight, OUTPUT); 
  pinMode(tpLeft, OUTPUT); 
  pinMode(btmLeft, OUTPUT); 
}

void loop()
{
  
  reading = digitalRead(swhPin); //reading first switch
  reading2 = digitalRead(swh2Pin); //reading second switch
   

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(led1, state); //changing red pin state to on or off

  previous = reading;
  

   if (reading2 == HIGH && state == HIGH) {     
    // turn LED on:    
    digitalWrite(blueLed, HIGH);
    delay(100); 
    state = LOW; //Stes state to low so that switch 2 can't be used until switch one is pressed again.
     digitalWrite(led1 , LOW); //turns off large LED light; 

     // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW; 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 
   
   
    delay(1300) // space between every count down.
    
       // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW; 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 

  
      delay(1300) // space between every count down.
    
       // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW; 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 

 delay(1300) // space between every count down.
    
       // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW; 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 

  } 
 
  else {
    // keeps led from turning on:
    digitalWrite(blueLed, LOW);
     
  }

    
}

You should use "unsigned long" for time.

Also, see this link

millis() (also unsigned long) will wrap around after about 50 days, so you have to account for that in your code.

Hi dbarby,

I think you should have a series resistor on the small led if you need very long life. 100R should be fine.

I don't think you need resistors on the inputs of the 2803s. Seem to remember there are internal resistors in the 2803.

Ddo you need the second 2803? What current does the large led draw? The Arduino may be able to sink it directly, oe you could use a bc337 with 1~5K on the base.

Paul

I do hope your diagram is not a faithful representation of your wiring.

You show the ground of one ULN2803 tied to the 5V supply rail.

You show the red and blue LEDs with the wrong polarity.

You show the blue LED connected from D13 to ground without a current limiting resistor.

You show the display segments connected in a peculiar order.

You show each of the current limiting resistors (and one is missing) bridging or being bridged by the same row of connections on the breadboard so that they would be ineffective in limiting the current.

And while it is for purported "simplicity" shown in most of the tutorials, it is bad practice to source the pushbuttons from 5V and use pull-down resistors, much more sensible to tie them to ground and use the internal pull-up function pinMode(swhPin, INPUT_PULLUP) and set up the logic to match.

I presume you have not in fact wired it as such and actually taken most of these things into consideration. :slight_smile:

Thanks for the help guys!

arduinodlb could you explain how to account for the time wrapping after 50 days?

Paul__B you're correct, I've already created the project and made the diagram afterwards, sorry for the mistakes but thanks for pointing them out. Could you help with a few points? You said

"You show each of the current limiting resistors (and one is missing) bridging or being bridged by the same row of connections on the breadboard so that they would be ineffective in limiting the current."

  • I thought that the connections on the breadboard where horizontal, so how would the resistors be ineffective?

Thanks also for the note about the push buttons, I'll try the pull-up function, is there a reason this is better practice?

Thanks

This is what you want connected.

Using the internal pullup and connecting the pin to Gnd when the switch is pressed limits the current to a few mA thru the internal resistor and, prevents any chance of shorting 5V to something and causing damage.

if (digitalRead(pinX) == LOW){
// do something
}

Re: your fritzing, the 5 pins a-b-c-d-e are all connected to each other.
So having the resistor in holes a & e are like connecting the 2 ends of the resistor together.

Great, that makes sense I think, I've made some adjustments to the diagram, the resistors are correct now I think. I've changed the set-up of the 2nd push button to use the pull-up function, is it wired correctly? And will the new code work on the push button...?

int swhPin = 2;         // switch pin
int led1 = 12;       // Main Large LED
int swh2Pin = 3;       // switch pin #2
int blueLed = 13;       // green led 
int state = LOW;      // the current state of the output pin
int state2 = HIGH;      // the current state of the output pin

int reading;           // the current reading from the input pin
int reading2;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin
int previous2 = HIGH;   // the previous reading from the input pin

// these declare pins for the 7 segment display.
int topright = 8; 
int buttom = 5;
int Top= 9;
int middle = 11;
int btmRight= 6;
int tpLeft = 10;
int btmLeft = 4;
int dot = 7;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 1000;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(swhPin, INPUT);
   pinMode(led1, OUTPUT); //LED is an output
  pinMode(swh2Pin, INPUT); //switch2 is an input
    digitalWrite(swh2Pin, HIGH); // Sets pull up resistor on.
    
  pinMode(blueLed, OUTPUT); //Blue led is a output

  pinMode(topright, OUTPUT); // All segments of display are outputs
  pinMode(buttom, OUTPUT); 
  pinMode(Top, OUTPUT); 
  pinMode(middle, OUTPUT); 
  pinMode(btmRight, OUTPUT); 
  pinMode(tpLeft, OUTPUT); 
  pinMode(btmLeft, OUTPUT); 
}

void loop()
{
  
  reading = digitalRead(swhPin); //reading first switch
  reading2 = digitalRead(swh2Pin); //reading second switch
   

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(led1, state); //changing red pin state to on or off

  previous = reading;
  

   if (reading2 == LOW && state == HIGH) {     
    // turn LED on:    
    digitalWrite(blueLed, HIGH);
    delay(100); 
    state = LOW; //Stes state to low so that switch 2 can't be used until switch one is pressed again.
     digitalWrite(led1 , LOW); //turns off large LED light; 

     // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW); 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 
   
   
    delay(1300); // space between every count down.
    
       // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW); 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 

  
      delay(1300); // space between every count down.
    
       // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW); 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 

 delay(1300); // space between every count down.
    
       // start seven segment
   digitalWrite(Top , HIGH); // says 3 
   digitalWrite(topright , HIGH); 
   digitalWrite(middle , HIGH); 
   digitalWrite(btmRight , HIGH); 
   digitalWrite(buttom , HIGH); 
 
 delay(1000); // pauses for 1 second
 
    digitalWrite(btmRight , LOW);  // says 2
    digitalWrite(btmLeft , HIGH);  

  delay(1000);
  
   digitalWrite(Top , LOW); // says 2
   digitalWrite(middle , LOW); 
   digitalWrite(btmLeft , LOW); 
   digitalWrite(buttom , LOW); 
   digitalWrite(btmRight , HIGH); 

 delay(1000);
  
   digitalWrite(topright , LOW); 
   digitalWrite(btmRight , LOW); 

  } 
 
  else {
    // keeps led from turning on:
    digitalWrite(blueLed, LOW);
     
  }

    
}

Still a lot of wacky things in that diagram; LEDs still shown with wrong polarity, can't presently check the sequence of the segments, more-or-less bottom-most segment wire wrong but the resistors otherwise sensibly wired. May be other things I missed.

The lower pushbutton seems fine for use with the internal pull-up; the 220 ohm resistor is not necessary but will not interfere with its functioning, haven't verified your code but of course, the other pushbutton would logically be wired the same way. Others have explained why this is better practice.