7 Segment Help!

Hi everyone,
Right now I have made a 7 segment display, with some code to make it say zero, wait a second, say one, wait a second, and start over. I am using a common anode display. In the future I would like to make some dice too. I have seen this in many good books, but want to experiment myself. Here is the code.

/*
  7-Segment
 Turns a 7-Segment display to a random number.
 */
int ledPins[] = { 6, 7, 8, 9, 10, 11, 12 }; 
int erase[] = { 6, 7, 8, 9, 10, 11, 12 };
int zero[] = { 7, 12, 6, 8, 10, 9 };
int one[] = { 6, 10 };
int two[] = { 7, 6, 11, 8, 9 };
int three[] = { 7, 6, 11, 10, 9 };
int four[] = { 12, 11, 6, 10 };
int five[] = { 7, 12, 11, 10, 9 };
int six[] = { 7, 12, 11, 8, 9, 10 };
int seven[] = { 7, 6, 10 };
int eight[] = { 7, 6, 12, 11, 10, 9, 8 };
int nine[] = { 7, 12, 6, 11, 10 };
int value;

void setup() {                
  // initialize the digital pins as outputs
  for (int i=0; i<7; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {

  digitalWrite(zero[ 0, 1, 2, 3, 4, 5 ], LOW);  // display 0
  delay(1000);
  void erase(HIGH);
  digitalWrite(one[ 0, 1 ], LOW);
  void erase(HIGH);
  delay(1000);
  
  void erase(value);
  {
    for (int i=0; i<7; i++) {
      digitalWrite(ledPins[i], value);
    }
  }
}

When I tell it to compile, it gives me the message: variable or field 'erase' declared void twice.

Thanks,
Qtechknow

Looks like a good start.
Going to add a 2nd one also?
Or several, and play yahtzee?

Thats a good idea, CrossRoads. Thanks for the comment. After I get this to compile, I will add more digits. Could you guys help me compile it?

No, the compiler does that :slight_smile: We may help you with ideas on writing it - you did say you want to experiment yourself to start tho ...

Sorry, didn't realize there was a question at the end of the original post:

void erase(HIGH);
digitalWrite(one[ 0, 1 ], LOW);
void erase(HIGH);

You don't use void here to call the function - you just need
erase(0);
or
erase(1);
I believe.

This line:-

digitalWrite(zero[ 0, 1, 2, 3, 4, 5 ], LOW);  // display 0

is wrong. You can't access an array like that.
This also is wrong:-

digitalWrite(zero[ 0, 1, 2, 3, 4, 5 ], LOW);  // display 0

You need a function like your eraser function to write to the segments one at a time.

For help see:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Thanks for all your replies. I'm still looking at your awesome tutorial, Grumpy Mike, and when I compile it with the new code (CrossRoads), it now says 'erase' cannot be used as a function definition twice. Here is the new code.

/*
  7-Segment
 Turns a 7-Segment display to a random number.
 */
int ledPins[] = { 5, 6, 7, 8, 9, 10, 11, 12 }; 
int erase[] = { 6, 7, 8, 9, 10, 11, 12 };
int zero[] = { 7, 12, 6, 8, 10, 9 };
int one[] = { 6, 10 };
int two[] = { 7, 6, 11, 8, 9 };
int three[] = { 7, 6, 11, 10, 9 };
int four[] = { 12, 11, 6, 10 };
int five[] = { 7, 12, 11, 10, 9 };
int six[] = { 7, 12, 11, 8, 9, 10 };
int seven[] = { 7, 6, 10 };
int eight[] = { 7, 6, 12, 11, 10, 9, 8 };
int nine[] = { 7, 12, 6, 11, 10 };
const int dp = 5;
int value;

void setup() {                
  // initialize the digital pins as outputs
  for (int i=0; i<8; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {

  digitalWrite(zero[ 0, 1, 2, 3, 4, 5 ], LOW);  // display 0
  delay(1000);
  erase(HIGH);
  digitalWrite(one[ 0, 1 ], LOW);
  delay(1000);
  erase(HIGH);
  
  void erase(value);
  {
    for (int i=0; i<7; i++) {
      digitalWrite(ledPins[i], value);
    }
  }
}

Thanks
Qtechknow

I have uploaded the new code (at the bottom from Grumpy Mike's tutorial) and it says 'erase' cannot be used as a function.
Any Help?
Thanks
Qtechknow

/*
  7-Segment
 Turns a 7-Segment display to a random number.
 */
int ledPins[] = { 5, 6, 7, 8, 9, 10, 11, 12 }; 
boolean erase[] = { HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
boolean zero[] = { HIGH, LOW, LOW, LOW, LOW, LOW, HIGH, HIGH };
boolean one[] = { HIGH, LOW, HIGH, HIGH, HIGH, LOW, HIGH, HIGH };
boolean two[] = { HIGH, LOW, LOW, LOW, LOW, HIGH, LOW, HIGH };
boolean three[] = { HIGH, LOW, LOW, HIGH, LOW, LOW, LOW, HIGH };
boolean four[] = { HIGH, LOW, HIGH, HIGH, HIGH, LOW, LOW, LOW };
boolean five[] = { HIGH, HIGH, LOW, HIGH, LOW, LOW, LOW, LOW };
boolean six[] = { HIGH, HIGH, LOW, LOW, LOW, LOW, LOW, LOW };
boolean seven[] = { HIGH, LOW, LOW, HIGH, HIGH, LOW, HIGH, HIGH };
boolean eight[] = { HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
boolean nine[] = { HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW };

const int dp = 5;
int value;

void setup() {                
  // initialize the digital pins as outputs
  for (int i=0; i<8; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  
  for(int i=0; i<8; i++) {
  digitalWrite(ledPins[i], zero[i]);  // display 0
  delay(1000);
  erase(1);
  digitalWrite(ledPins[i], one[i]);
  delay(1000);
  erase(1);
  }
  
  void erase(value);
  {
    for (int i=0; i<7; i++) {
      digitalWrite(ledPins[i], value);
    }
  }
}

You have not got the right number of { and }.

If you click just after a { it will show the matching }

Please change : I made a few modification of your code. New technique...a boolean array, hum interesting.

When I put your code in my IDE, I notice erace[] <- array and erace() - function - CAN NOT be the same name. So I change your function name as erac() instead. And you place a ; after a function - void myfunction(); <--- No..No..No

Anyway, I clean the bugs out. The code is now compiling just fine.

/*
  7-Segment
 Turns a 7-Segment display to a random number.
 */
int ledPins[] = { 5, 6, 7, 8, 9, 10, 11, 12 }; 
boolean erase[] = { HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
boolean zero[] = { HIGH, LOW, LOW, LOW, LOW, LOW, HIGH, HIGH };
boolean one[] = { HIGH, LOW, HIGH, HIGH, HIGH, LOW, HIGH, HIGH };
boolean two[] = { HIGH, LOW, LOW, LOW, LOW, HIGH, LOW, HIGH };
boolean three[] = { HIGH, LOW, LOW, HIGH, LOW, LOW, LOW, HIGH };
boolean four[] = { HIGH, LOW, HIGH, HIGH, HIGH, LOW, LOW, LOW };
boolean five[] = { HIGH, HIGH, LOW, HIGH, LOW, LOW, LOW, LOW };
boolean six[] = { HIGH, HIGH, LOW, LOW, LOW, LOW, LOW, LOW };
boolean seven[] = { HIGH, LOW, LOW, HIGH, HIGH, LOW, HIGH, HIGH };
boolean eight[] = { HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
boolean nine[] = { HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW };

const int dp = 5;
int value;

void setup()
 {                
  // initialize the digital pins as outputs
  for (int i=0; i<8; i++) 
 {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop()
 {
  
  for(int i=0; i<8; i++) 
  {
  digitalWrite(ledPins[i], zero[i]);  // display 0
  delay(1000);
  }
  eras();
for ( int 1-0; i<8; i++)
  {
  digitalWrite(ledPins[i], one[i]);
  delay(1000);
  }
  eras();
  
}
  
  void eras()
  {
    for (int i=0; i<7; i++)
   {
      digitalWrite(ledPins[i], erase[i]);
    }
  }

Also give this a blast! I've written a method that outputs a character for you. I've also packed all the booleans into an integer array...

/*
  7-Segment
 Turns a 7-Segment display to a random number.
 */
int ledPins[] = { 5, 6, 7, 8, 9, 10, 11, 12 };

// Packed the boolean array into bits
unsigned int data[] =  {

//567890AB
 B11111100, // 0
 B11000100, // 1
 B11111010, // 2
 B11101110, // 3
 B11000111, // 4
 B10101111, // 5
 B10111111, // 6
 B11100100, // 7
 B11111111, // 8
 B11100111, // 9
};

const int dp = 5;
int value;

void setup() {

  // initialize the digital pins as outputs
	for (int i=0; i<8; i++) {
		pinMode(ledPins[i], OUTPUT);
	}
}

void loop() {

	for (int c=0; c<10; c++) {
		for(int i=0; i<8; i++)   {
			outputCharacter(c);
		}
		delay(1000);
		eras(true);
		delay(500);
	}

}

// http://www.arduino.cc/playground/Code/BitMath
void outputCharacter(int c){

	// This loop looks at every bit in the array item
	// Starting with the 8th bit
	for(int i=0; i<8; i++)   {

		// This gets the bit we're looking for
		if ( (1 << 7-i)  & data[i])
			digitalWrite(ledPins[i], HIGH);
		else
			digitalWrite(ledPins[i], LOW);
	}
}

void eras(boolean v)	{
	for (int i=0; i<7; i++)	{
		digitalWrite(ledPins[i], v);
	}
}