Arduino Counter with 7 segment LED?

I just got my arduino a week ago and I have been playing around with a 7 segment led and I started coding a push button program for it and I am stuck. I started the program based on the button example but that is about as far as I got. I was wondering if anyone would take a look at my code or maybe just steer me in the right direction it make it count up from 0.

Thanks
Ted

Here is what i have so far (mind you this is the first thing i have ever coded)

int ledpin = 1 and 2 and 3 and 4 and 5 and 6 and 7 and 8; // choose the pin for the LED
int inputPin = 9; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int OFF = LOW;
int ON = HIGH;

void setup() {
pinMode(1, OUTPUT); // declare LED as output
pinMode(9, INPUT);
pinMode(2, OUTPUT); // declare pushbutton as input
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}

void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(1, OFF); // turn LED to 5
digitalWrite(2, ON);
digitalWrite(3, ON);
digitalWrite(4, OFF);
digitalWrite(5, OFF);
digitalWrite(6, ON);
digitalWrite(7, ON);
digitalWrite(8, OFF);
}
else {
digitalWrite(1, OFF); // led OFF
digitalWrite(2, OFF);
digitalWrite(3, OFF);
digitalWrite(4, OFF);
digitalWrite(5, OFF);
digitalWrite(6, OFF);
digitalWrite(7, OFF);
digitalWrite(8, OFF);
}

val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(1, ON); // turn LED OFF
digitalWrite(2, OFF);
digitalWrite(3, ON);
digitalWrite(4, ON);
digitalWrite(5, OFF);
digitalWrite(6, ON);
digitalWrite(7, ON);
digitalWrite(8, OFF);
}
else {
digitalWrite(1, OFF); // turn LED OFF
digitalWrite(2, OFF);
digitalWrite(3, OFF);
digitalWrite(4, OFF);
digitalWrite(5, OFF);
digitalWrite(6, OFF);
digitalWrite(7, OFF);
digitalWrite(8, OFF);
}
}

Hey, Ted. Not too bad for a first try! Just a couple of things:

int ledpin = 1 and 2 and 3 and 4 and 5 and 6 and 7 and 8;             // choose the pin for the LED

This expression doesn't do what you think. (It does a bitwise AND of all the operands, effectively setting ledpin=0. Fortunately, you don't use the value of ledpin anywhere!)

I'm assuming you have everything wired correctly -- pins 2-8 to the segments of the display, pin 1 to the LED (through a resistor) and pin 9 to the button -- and that the button is wired according to the normal convention, i.e. that closing the button circuit brings the pin to GROUND. If so, you need to use the pullup resistor to pull the input pin HIGH (if the button isn't pressed) like this:

pinMode(9, INPUT)
digitalWrite(9, HIGH);

(See playground examples for button inputs).

Since the pin goes low when the button is down, I imagine you'd want to change "if (val == HIGH)" to "if (val == LOW)" (assuming you want the digits to display when the button is down).

Lastly, as the code is written now, it seems to very rapidly switch from one digit (the top half of "loop") to another (the botton half). It might be prudent to add some delay() calls; otherwise things will go so fast that your eye can't see any digit at all.

The best strategy for a program like this is to start small and grow. Make a program that displays a single digit. Then add button support. Then make it change digits, etc. One step at a time.

Mikal

Thank you for the tips, I really need them. I don't think I made it entirely clear what I wanted to do. I wanted it so every time i press the button the counter is advanced by 1. Also I do see understand about the AND command now but how do I set the pins 2-8 to led pins.

Thanks again for you time.

Ted

Ted, when the sample code uses an expression like

int ledPin = 13;

it's not really doing anything, it's just creating a name for the pin, so that later the programmer can write code like

digitalWrite(ledPin, HIGH);

Using names like this helps improve code readability and makes it a lot easier to change pin assignments if you need to later.

For your application, you need to (a) detect when a button is depressed, and (b) increment the counter. To best do this, I recommend introducing 2 state variables, one which keeps track of the button and the other the counter:

int buttonState = HIGH;
int counter = 0;

Now each time through the loop, you check to see if the button is newly depressed using code like this:

int newstate = digitalRead(inputpin);
if (buttonState != newstate) // has the state changed from HIGH to LOW or vice versa?
{
  buttonState = newstate;
  if (newstate == LOW) // was the button pressed?
    ++counter; // then increment the counter
  delay(200); // software debouncing code
}

So now you have code that increments a counter everytime through the loop. All that remains is to display the current value of (the last digit of) counter:

switch(counter % 10)
{
  case 0:
    // code to display the digit 0 on the 7-seg display
    ...
    break;
  case 1:
    // code to display 1
    ...
    break;
    ... etc.
}

That should get you started! Good luck!

Mikal

Another very important thing in an application like yours is to debounce the switch. Switches are mechanic, and when you press and release them they "bounce" i.e. the close and open a number of times very fast. But Arduino is fast enough to discover this, it thinks that you are pressing the button mulitple times. This is called switch bounce, dealing with it is called "debouncing a switch" it's pretty simple to handle. Try to check Lady Ads's Arduino tutorials here:

http://www.ladyada.net/learn/arduino/

You might find it helpfull to go through them from start to end. The part about using a switch and debouncing it is in Lesson 5.

EDIT:

Also you should try to "modularize" your code. Break it up in individual parts dealing with only a small part of the overall problem. in your case try to make one part of the program deal with only reading the button presses. Each press could incremet a counter.

Then make a function you can call that displays the correct digit depending on the value of the counter.

This way you isolate the different parts of the program and make it MUCH easyer for yourself when you have to debug it

Thank you for taking time to help me with this. This has help a great deal.

Ted