7 Segment Display (I'm a Beginner)

Hey all, I'm new to the Arduino (and programming) so any help would be appreciated.

A while back I had gotten a Basic Stamp bundle (by Parallax) that included some electrical components and the "What's a Microcontroller?" curriculum. The book is good, but it only covers the Basic Stamp. I've been going through the lessons and trying to recreate some of the projects on my lovely Arduino, mostly with success. One of the chapters is about controlling a 7 segment display.

Variables for each pin were labeled following this chart: http://i38.tinypic.com/o79b7m.jpg

Their setup: http://i38.tinypic.com/zk62yg.jpg

My setup (identical to theirs, but using different pins): http://i34.tinypic.com/6443f5.jpg

I wrote some really ugly code to test each individual segment of the display:

/*
;;7 Segment Display Test
;;Tests a single 7 segment display (with decimal point) connected to pins 12-5.
;;Should individually light up each section of the display (including the DP).
*/
int b=5, a=6, f=7, g=8, dp=9, c=10, d=11, e=12;


void setup() {
  
  pinMode(b, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  
}

void loop() {
  digitalWrite(b, HIGH);
  delay(250);
  digitalWrite(a, HIGH);
  delay(250);
  digitalWrite(f, HIGH);
  delay(250);
  digitalWrite(g, HIGH);
  delay(250);
  digitalWrite(dp, HIGH);
  delay(250);
  digitalWrite(c, HIGH);
  delay(250);
  digitalWrite(d, HIGH);
  delay(250);
  digitalWrite(e, HIGH);
  delay(250);
  
  
   digitalWrite(b, LOW);
  delay(250);
  digitalWrite(a, LOW);
  delay(250);
  digitalWrite(f, LOW);
  delay(250);
  digitalWrite(g, LOW);
  delay(250);
  digitalWrite(dp, LOW);
  delay(250);
  digitalWrite(c, LOW);
  delay(250);
  digitalWrite(d, LOW);
  delay(250);
  digitalWrite(e, LOW);
  delay(250);
  
  
}

It's ugly and inefficient as hell, but it compiles and works.

I though I could a lot more elegant with some sort of "for" loop, and here is as far as I got:

/*
;;7 Segment Display Test
;;Tests a single 7 segment display (with decimal point) connected to pins 12-5.
;;Should individually light up each section of the display (including the DP).
*/

int b=5, a=6, f=7, g=8, dp=9, c=10, d=11, e=12;
int pins[8] = {b, a, f, g, dp, c, d, e};


void setup() {
  Serial.begin(9600);
  for (int i = 0; i == 7; i++) {
    pinMode(pins[i], OUTPUT);
  }
  
}

void loop() {
  
  for(int i = 0; i == 7; i++) {
    digitalWrite(pins[i], HIGH);
    delay(250);

  }
  
  for(int i = 0; i == 7; i++) {
    digitalWrite(pins[i], LOW);
    delay(250);

  }
  
}

This too compiles, but it doesn't work. This is my first time using arrays, so am I doing something wrong? This is sort of a shot in the dark, so if there is a better way of controlling a 7 segment display, please tell me.

Also, I still haven't figured out how I can do numbers without ridiculous control structures.

Any and all help is greatly appreciated.

-NC

The reason that it isn't working is because in the for loops, you have it only loop if I equals 7. When you initialize I you set it to 0, Since 0 does not equal 7 the loop does not execute.

  for (int i = 0; i == 7; i++) {

What you need to do is use a < then loop (Less-then loop)
 for (int i = 0; i < 8; i++) {
(Less then 8 because the loop will go 0 - 8 otherwise and will go past the array's last index)
This loop will go 0 - 7 and should work properly.
just replace all 3 instances

Wow, I don't feel like a idiot at all. Thanks for such a well explained response. I guess learning is just trial and error? Thanks again!

-NC

Just a quick update, I compiled and uploaded it, and it works beautifully now.

-NC