Trying to run 7 Segment Display

Hey guys,

so I'm trying to run a set of four 7 segment displays with an arduino. To save pins what I'm doing is using a single 7447 BCD converter, and cycling the power so that only one digit is ever on at a time, but going so fast that they look like they're all on.

Here is the code (note this is just for testing purposes as the grand scheme is for a digital clock):

byte pin0=0;
byte pin1=1;
byte pin2=2;
byte pin3=3;
byte min1pwr=4;
byte min10pwr=5;
byte hr1pwr=6;
byte hr10pwr=7;
byte var;

void setup()
{
  pinMode(pin0,OUTPUT);
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);
  pinMode(pin3,OUTPUT);
  pinMode(min1pwr,OUTPUT);
  pinMode(min10pwr,OUTPUT);
  pinMode(hr1pwr,OUTPUT);
  pinMode(hr10pwr,OUTPUT);
}

void loop()
{
  
  digitalWrite(hr10pwr,LOW);
var=val2;
  digitalWrite(min1pwr,HIGH);
  delay(1);
  digitalWrite(min1pwr,LOW);
  var=val5;
  digitalWrite(min10pwr,HIGH);
  delay(1);
  digitalWrite(min10pwr,LOW);
  var=val8;
  digitalWrite(hr1pwr,HIGH);
  delay(1);
  digitalWrite(hr1pwr,LOW);
  var=0;
  digitalWrite(hr10pwr,HIGH);
  delay(1);
   if(var==0){
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 0=(0,0,0,0)
  }
  if(var==1) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 1=(0,0,0,1)
  }
  if(var==2) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 2=(0,0,1,0)
  }
  if(var==3) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 3=(0,0,1,1)
  }
  if(var==4) {
  digitalWrite(pin0,LOW);
  digitalWrite(pin1,LOW);
  digitalWrite(pin2,HIGH);
  digitalWrite(pin3,LOW);  //4=(0,1,0,0)
  }
  if(var==5) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  // 5=(0,1,0,1)
  }
  if(var==6) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  //6=(0,1,1,0)
  }
  if(var==7) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  // 7=(0,1,1,1)
  }
  if(var==8) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,HIGH);  // 8=(1,0,0,0)
  }
  if(var==9) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,HIGH);  // 9=(1,0,0,1)
  }
}

Now, what's happening is that rather than showing 0852 as the code says it should, it displays 0 on each digit once, then just continually cycles on whatever number the last "var" is (in the sketch provided above, that would be 0).

As far as I can tell, that code should call for it to turn on the 4th digit, and display one number, then turn it off and turn on the 3rd digit displaying another number, and so on. However, while the power cycles properly, the displayed digits do not. interestingly enough it displays the numbers correctly if instead of saying "var=", i explicitly tell it to output the correct binary number. However, this would not be feasible for the entire code, so I was hoping for a solution similar to the one above.

Does anyone have any ideas?

Maybe not exactly what you're looking for, but have you considered using a MAX7219/7221? There's a great library for it and you can control all four (actually even more) displays with just the one MAX72##.

Mikael

Come on, what is that, just screen dumps. Use copy and paste or post the arduino file as an attachment. Read the how to use this forum sticky.
If you post the code properly then we can look at it and run it on our own machines.

Sorry guys it was really early in the morning, must have missed that.

I edited in the code to the original message, thanks for the tip!

And I will look into the max72, but i would still really like to know why this is behaving like it is?

The display is only showing the last "var" because that is what you have told it to do.

Remember, the Arduino follows each instruction before proceeding to the next instruction. If you follow the instructions in exactly the order they appear in the program, you will see why only the last value of "var" is being used.

If you want the Arduino to jump around and follow the instructions in a different order, there are various ways to do that.

Please read about "functions" in the Arduino reference.
I have pulled some of the code out of your loop() and put it into a function I named setupForDigit().

I have also removed your "var2", "var5", and "var8". (What purpose did they serve, anyway?) I have replaced them with the numbers you seemed to have wanted.

Here is the code after my modifications. Please study it carefully.

byte pin0=0;
byte pin1=1;
byte pin2=2;
byte pin3=3;
byte min1pwr=4;
byte min10pwr=5;
byte hr1pwr=6;
byte hr10pwr=7;
byte var;

void setup()
{
  pinMode(pin0,OUTPUT);
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);
  pinMode(pin3,OUTPUT);
  pinMode(min1pwr,OUTPUT);
  pinMode(min10pwr,OUTPUT);
  pinMode(hr1pwr,OUTPUT);
  pinMode(hr10pwr,OUTPUT);
}

void loop()
{
  
  digitalWrite(hr10pwr,LOW);
  setupForDigit(2);
  digitalWrite(min1pwr,HIGH);
  delay(1);
  digitalWrite(min1pwr,LOW);
  setupForDigit(5);
  digitalWrite(min10pwr,HIGH);
  delay(1);
  digitalWrite(min10pwr,LOW);
  setupForDigit(8);
  digitalWrite(hr1pwr,HIGH);
  delay(1);
  digitalWrite(hr1pwr,LOW);
  setupForDigit(0);
  digitalWrite(hr10pwr,HIGH);
  delay(1);

}

void setupForDigit(byte d) {
  if(d==0){
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 0=(0,0,0,0)
  }
  if(d==1) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 1=(0,0,0,1)
  }
  if(d==2) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 2=(0,0,1,0)
  }
  if(d==3) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,LOW);  // 3=(0,0,1,1)
  }
  if(d==4) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  //4=(0,1,0,0)
  }
  if(d==5) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  // 5=(0,1,0,1)
  }
  if(d==6) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  //6=(0,1,1,0)
  }
  if(d==7) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
    digitalWrite(pin3,LOW);  // 7=(0,1,1,1)
  }
  if(d==8) {
    digitalWrite(pin0,LOW);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,HIGH);  // 8=(1,0,0,0)
  }
  if(d==9) {
    digitalWrite(pin0,HIGH);
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
    digitalWrite(pin3,HIGH);  // 9=(1,0,0,1)
  }
}

NOTE: I just noticed I had a bug in my code so I went back and edited it. Try this version instead.