Counter buttons

Hello

I've made a counter on a 3 digit 7 segment display which is multiplexed. This counter works like a stopwatch. I must have 2 buttons.. one for enable and stop and the other one for reset.

I added the button for enable and it's working ... it starts counting when I press it but how to modify the code so when I press again on the enable button the counter stops and shows me a value.. also the same question for the reset button.. what can I do so when I press it it turns back to 000 on the display ?

This is the code I'm using:

#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define f 7
#define G 8
#define DP 9
#define button1 1

#define CC1 10
#define CC2 11
#define CC3 12



#define numbersegments { \
  {1,1,1,1,1,1,0,0},\
  {0,1,1,0,0,0,0,0},\
  {1,1,0,1,1,0,1,0},\
  {1,1,1,1,0,0,1,0},\
  {0,1,1,0,0,1,1,0},\
  {1,0,1,1,0,1,1,0},\
  {1,0,1,1,1,1,1,0},\
  {1,1,1,0,0,0,0,0},\
  {1,1,1,1,1,1,1,0},\
  {1,1,1,1,0,1,1,0}\
}


byte numbers[10][8] = numbersegments;

const int segments[8] = { A, B, C, D, E, f, G, DP };

int buttonState=0;

void setup() {
  Serial.begin(9600);
  pinMode(A, OUTPUT);
  digitalWrite(A,LOW);
  pinMode(B, OUTPUT);
  digitalWrite(B,LOW);
  pinMode(C, OUTPUT);
  digitalWrite(C,LOW);
  pinMode(D, OUTPUT);
  digitalWrite(D,LOW);
  pinMode(E, OUTPUT);
  digitalWrite(E,LOW);
  pinMode(f, OUTPUT);
  digitalWrite(f,LOW);
  pinMode(G, OUTPUT);
  digitalWrite(G,LOW);
  pinMode(DP, OUTPUT);
  digitalWrite(DP,LOW);

  pinMode(CC1, OUTPUT);
  digitalWrite(CC1,HIGH);
  pinMode(CC2, OUTPUT);
  digitalWrite(CC2,HIGH);
  pinMode(CC3, OUTPUT);
  digitalWrite(CC3,HIGH);
  
  pinMode (button1, INPUT);

  
}

void loop() {
 
 buttonState=digitalRead(button1);
 if (buttonState == HIGH){
  for (int digit1=0; digit1 < 10; digit1++) {
    for (int digit2=0; digit2 < 10; digit2++) {
      for (int digit3=0; digit3 < 10; digit3++){
       
          for(int t=0; t<1;t++){
            
            setsegments(digit1,CC1,1000);
             setsegments(digit2,CC2,1000);
             setsegments(digit3,CC3,1000);
          } 
        }
      }
    }
  }
}

The function for setsegments is this:

void setsegments(int number, int digit,int time){
  for (int seg = 0; seg < 8; seg++){
    if (numbers[number][seg]==1) {digitalWrite(segments[seg],HIGH);}
    else {digitalWrite(segments[seg],LOW);}
  }
  digitalWrite(digit,LOW);
  delayMicroseconds (1000);
  digitalWrite(digit,HIGH);

}

I've attached a schematics ... I'm sorry if it's not telling you much .. I'm new at this...

The enable button is connected to pin 1 on arduino and the reset button to pin 2

Also I muse a toggle switch but I'll come to this after I solve this problem

Thank you!

#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define f 7
#define G 8

One letter names are a recipe for disaster. Avoid them like the plague.

  pinMode (button1, INPUT);

Is that the enable/stop pin or the reset pin? Why use such meaningless names? Why not set the mode for the other pin?

How ARE the switches wired? You are using external pulldown resistors, right?

          for(int t=0; t<1;t++){

Why? A loop that iterates once is stupid.

You need to look at the state change detection example. It shows how to determine that a switch has BECOME pressed, rather than IS pressed.

You count the number of presses. An even number means stop. An odd number means start.

You also need to deal with switch bouncing.

You also need to separate reading the switches from dealing with the data obtained from the switches.

And, last, but by no means least, you need to post ALL of your code.

Here is a section of code you can add your sketch to:

//Switch manager skeleton

#include <SwitchManager.h> //http://gammon.com.au/Arduino/SwitchManager.zip
SwitchManager startSwitch;  // create the object
SwitchManager resetSwitch;  // create the object

const byte startButton = 14;
const byte resetButton = 15;

void setup ()
{
  startSwitch.begin (startButton, startButtonManager);
  resetSwitch.begin (resetButton, resetButtonManager); 
  
  //Your other setup stuff   

}

void loop ()
{
  //check to see what's happening with the switches
  startSwitch.check();
  resetSwitch.check();  
  
  //Your other loop stuff 

}

//************* F U N C T I O N S *************

//**************************
void startButtonManager (const byte newState, const unsigned long interval)
{
  //assuming when the switch is pushed the input to the pin is HIGH
  //if it is LOW then use "newState == LOW"
  //has the startButton been pressed for more than 500mS?
  if (newState == HIGH && interval >= 500UL) 
  {
    //do start stuff here
  }  

  return;
}

//**************************
void resetButtonManager (const byte newState, const unsigned long interval)
{
  //assuming when the switch is pushed the input to the pin is HIGH
  //if it is LOW then use "newState == LOW"
  //has the resetButton been pressed for more than 500mS?
  if (newState == HIGH && interval >= 500UL) //has the resetButton been pressed for more than 500mS?
  {
    // do reset stuff here
  }    

  return;
}

I put one letter name because each of those letter corresponds to a segment on a 7 segment display ... it's in its datasheet... and I thought it's better that way .. so I can connect a certain pin to a certain segment.

pinMode (button1, INPUT);

I said that I manage to put the enable button on my scheme so clearly this is the enable button.
For me those names make sense.. aren't meaningless.

The buttons are wired exactly like this: http://arduino.cc/en/uploads/Tutorial/button.png

The enable button is to pin 1 and the reset button is to pin 0

for(int t=0; t<1;t++){

A friend helped me with the code.. like I said .. I'm new on arduino and I don't know how to explain why, but I can clearly tell you that this piece of code doesn't iterate a loop .. it's how faster the counter is working .. 1 being the fastest... and the loop is infinite... doesn't iterates once.

But anyway.. if modifying the program to be more understandable is a necessity I will do that and post it later..

Thank you!

The enable button is to pin 1 and the reset button is to pin 0

These pins are used for your serial communications to the PC.
It's best not to use them.
If you happen to press the switch connected to pin 1 when you download your program you will damage you Arduino!

Use A0 (D14) and A1 (D15)

#define A 2
Why not
#define segA 2

One letter macros will eventually bit you.

What is it you want to do here?
for(int t=0; t<1;t++){

The buttons are wired exactly like this: http://arduino.cc/en/uploads/Tutorial/button.png

I would at least add a 470R resistor from +5V to the switch.

One letter macros will eventually bit you.

They already have. That's why the segment between E and G is named f, not F.