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