Go Down

Topic: Looking for help regarding a Counter (Read 3592 times) previous topic - next topic

jyank747

found this. credit to missionduke. will this do what i am looking for as far as the counting part?

#include <SoftwareSerial.h>  //for software serial communication



#define txPin 14  //change to your serial port on Arduino board
#define rxPin 15  //not used but is required

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int buttonPressCount;


const int  buttonPin = 2;    //the pin that the pushbutton is attached to




int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button


void setup()  {
  pinMode(buttonPin, INPUT);  //initialize the button pin as a input
  Serial.begin(9600);  //initialize serial communication

pinMode(txPin, OUTPUT);
//the following resets the board, changes the brightness to 100%, and sets the board to '0000':
mySerial.begin(9600);
mySerial.print(0x7A,BYTE); //special character
mySerial.print(0x00,BYTE); //set brightness to full
mySerial.print(0x76,BYTE); //reset board
mySerial.print(0); //send '0' character
mySerial.print(0); //send '0' character
mySerial.print(0); //send '0' character
mySerial.print(0); //send '0' character
}

void loop(){

  buttonState = digitalRead(buttonPin);  //read the pushbutton input pin

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
     
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'
     
     
    }

  }

  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop

 

}




void updateDisplay(int buttonPushCounter){
String intString = String(buttonPushCounter);  //changes integer to a string
char displayChars[4];  //create array to hold the four numbers
int stringLength = intString.length();  //get length of the string
//the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
//and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
if(stringLength == 4){
  displayChars[0] = intString.charAt(0);
  displayChars[1] = intString.charAt(1);
  displayChars[2] = intString.charAt(2);
  displayChars[3] = intString.charAt(3);
}else if(stringLength == 3){
  displayChars[0] = 0;
  displayChars[1] = intString.charAt(0);
  displayChars[2] = intString.charAt(1);
  displayChars[3] = intString.charAt(2);
}else if(stringLength == 2){
  displayChars[0] = 0;
  displayChars[1] = 0;
  displayChars[2] = intString.charAt(0);
  displayChars[3] = intString.charAt(1);
}else if(stringLength == 1){
  displayChars[0] = 0;
  displayChars[1] = 0;
  displayChars[2] = 0;
  displayChars[3] = intString.charAt(0);
}
mySerial.print(0x76,BYTE); //Reset board
mySerial.print(0x76,BYTE); //Reset board
mySerial.print(displayChars[0]); //Send '0' character
mySerial.print(displayChars[1]); //Send '0' character
mySerial.print(displayChars[2]); //Send '0' character
mySerial.print(displayChars[3]); //Send '0' character

delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'
 
}


CrossRoads

I programmed the top left & top right to allow score from 00 to 99.
The bottom four count down from presets of 10:00, or 3:00, or 1:00, to 00:00 (fencing specific times)

The two smaller digits are not driven by the MAX7219 (but that can easily by used, make sure to get common cathode digits).
One digit shows E,S,F for Epee, Foil, Sabre.
The other shows the period of the bout - 1,2,3 for an individual bout, with score that would end at 15,  and up to 9 for a team bout, with score that would end at 45.
The decimal points from the digits were used as various indicators - penalty, priority, coin toss & result.

This one actually uses two promini's, you can see one at the left edge and one in the middle.  It was built up in stages, initially just to mimic time & score lights (the large blocks) from another source, with score controlled independently, all by the middle card. Then speaker was added (amplifier on the small board on the right and a speaker below the cards), and finally detection of score touches was added with the left promini to replace the external box that provided the time & score lights, and period & weapon indicator.   
I will replace both promini's with a 1284 and combine the 2 programs when I make the next one as my coding has improved a lot since I created this, and my use of SPI has really taken off.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

jyank747


I programmed the top left & top right to allow score from 00 to 99.


Do you still have that code? and are willing to share? If not, would the code I found above do the trick?

CrossRoads

I'll share, score keeping is simple enough.
How are you connected up? I use SPI exclusively to MAX7219. What do you have?
If you don't see me post it, its because I forgot - ping me tonight.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

jyank747


How are you connected up? I use SPI exclusively to MAX7219. What do you have?


crossroads, this is embarassing to admit, but i have no idea what i have. all i know is that i bought the arduino starter kit.

CrossRoads

Does this make sense?  Expand to as many digits as needed.
Adjust connections for your displays.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

jyank747

thanks crossroads, i realy appreciate it. i will take a closer look at it tonight. don't i need a code to program it?

jyank747

#22
Jan 24, 2013, 08:23 pm Last Edit: Jan 24, 2013, 08:30 pm by jyank747 Reason: 1

DS1307 kit that adafruit sells


Looked at this on thier website and they provide the code for it. How can I hook this up to the LED display?

jyank747

with two seperate LED 7-seg displays, i want it hooked up to one push button.

first push turns on a counter and a stop watch

for each push after it will count in 2's (0,2,4,6,8,10...) and stop watch continues

double push will count the last push and stop the timer.

can anyone help, please?

Go Up