Hello all,
I've been lurking for a few weeks, and I bought a Duemilanove one week ago. I've also been through a few of the tut programs and reference sections.
This is my first attempt at a program.
I've got a simple program to change a seven segment display in relation to an input pot.
I'm on my fourth rev. of this program and after many beers (thinking fluid) I've managed to clean it up from a mess of IF statements and other goofyness to a Switch statement that calls functions. I was happy....
But now I've moved my functions to another tab for clarity. Everything still worked until I began to use variables for pin names. How do you declare a global variable for multiple tabs? I thought all tabs without an extension were compiled "as one"..?
I've moved the variables from tab to tab, but still no luck.
I've searched the forum and heard terms like, "extern" and other over-my-head concepts. Should I just stay away from using tabs? I'd really like a way to make the functions portable, as they will be handy in future programs.
Also, any criticism on bad coding habits are welcomed, as I am just learning.
First Tab: (Main Program)
/******************************************************************
* Testing analog input & mapping function
* Testing named LED pins for portability
************************ SCHEMATIC *******************************
Center leg of Pot on analog 5, Other legs on +5V & GND
7Seg commCath wired on pins 3-9 through 500ohm R's
******************************************************************/
const int myPot = 5; // Pin number used for the POT
int potVal; // Stores value of pot reading
void setup()
{
// Set LED Pin numbers:
const int segA = 4;
const int segB = 3;
const int segC = 7;
const int segD = 8;
const int segE = 9;
const int segF = 5;
const int segG = 6;
// Set output pins:
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
}
void loop()
{
potVal = analogRead(myPot);
potVal = map(potVal, 0, 1000, 0, 9);
switch (potVal){ // Call the appropriate function to display the digit.
case 0: zero(); break;
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
}
} // Wash, Rinse and repeat...
Second Tab: (Digit Functions: Does not recognize pin names)
void zero() // Function to display # 0
{
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
}
void one() // Function to display # 1
{
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
}
void two() // Function to display # 2
{
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
}
void three() // Function to display # 3
{
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
}
void four() // Function to display # 4
{
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
}
void five() // Function to display # 5
{
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
}
void six() // Function to display # 6
{
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
}
void seven() // Function to display # 7
{
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
}
void eight() // Function to display # 8
{
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
}
void nine() // Function to display # 9
{
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
}