Up down counter issues

I started this project as a bullet counter because my father and i thought it would be cool to attach to our ARs. I initially started this project hoping to make it use a button count up and a button to count down from a preset number with a low bullet indicator that blinks after the number 10. I thought of using an Arduino to keep the size of the project small. I’ve been working on this counter for a while now and I’m relatively new to Arduino. This is what i have so far I’m sure it has many errors help would be greatly appreciated.

bulletcounter.txt (21.8 KB)

int pin1 = 2;
int pin2 = 3;                                    //                            --6--
int pin3 = 4;                                    //                         5 |     | 7
int pin4 = 5;                                    //                           |--4--|
int pin5 = 6;                                    //                         1 |     | 3
int pin6 = 7;                                    //                            --2--
int pin7 = 8;
int gnd1 = 11;                                 //                          gnd1 is display 1's gnd
int gnd2 = 9;                                   //                          gnd2 is display 2's gnd

These should all have the const qualifier in front of them. Pin numbers should change.

   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);

There are constants defined, HIGH and LOW that should be used here, instead.

   delay(0.5);                                        //          Writes 30 to the display.

Wrong value as argument. The comment is not even in the right galaxy in terms of being close to right.

You are writing a series of values to some pins. Those values could be obtained from a byte using bitRead.

int val1 = B01110111;
int val2 = B00010001;

for (int i=0; i<timer; i++)
{
   digitalWrite(pin1, bitRead(val1, 6));
   digitalWrite(pin2, bitRead(val1, 5));
   digitalWrite(pin3, bitRead(val1, 4));
   digitalWrite(pin4, bitRead(val1, 3));
   digitalWrite(pin5, bitRead(val1, 2));
   digitalWrite(pin6, bitRead(val1, 1));
   digitalWrite(pin7, bitRead(val1, 0));

   digitalWrite(gnd1, LOW);
   digitalWrite(gnd2, HIGH);
   delay(1);

   digitalWrite(pin1, bitRead(val1, 6));
   digitalWrite(pin2, bitRead(val1, 5));
   digitalWrite(pin3, bitRead(val1, 4));
   digitalWrite(pin4, bitRead(val1, 3));
   digitalWrite(pin5, bitRead(val1, 2));
   digitalWrite(pin6, bitRead(val1, 1));
   digitalWrite(pin7, bitRead(val1, 0));

   digitalWrite(gnd1, HIGH);
   digitalWrite(gnd2, LOW);
   delay(1);
}

Now, notice that the two blocks of code could be replaced with loops:

for (int i=0; i<timer; i++)
{
   for(int j=6; j<=0; j--)
   {
     digitalWrite(pin1, bitRead(val1, j));
   }

   digitalWrite(gnd1, LOW);
   digitalWrite(gnd2, HIGH);
   delay(1);

   for(int j=6; j<=0; j--)
   {
     digitalWrite(pin1, bitRead(val2, j));
   }

   digitalWrite(gnd1, HIGH);
   digitalWrite(gnd2, LOW);
   delay(1);
}

Now, notice that val1 and val2 could be put in arrays, and that the entire mess of code in setup() could be done with a loop that cycles through the arrays:

int val1[31] = { B01110111, ... };
int val2[31] = { B00010001, ... };

for(int k=30, k<=1; k--)
{
  for (int i=0; i<timer; i++)
  {
     for(int j=6; j<=0; j--)
     {
       digitalWrite(pin1, bitRead(val1[k], j));
     }

     digitalWrite(gnd1, LOW);
     digitalWrite(gnd2, HIGH);
     delay(1);

     for(int j=6; j<=0; j--)
     {
       digitalWrite(pin1, bitRead(val2[k], j));
     }

     digitalWrite(gnd1, HIGH);
     digitalWrite(gnd2, LOW);
     delay(1);
  }
}

Well, I'm pretty well not following this, but I am curious about how it turns out. Me, I just quit pulling the trigger when I don't hear the sproinging of the buffer spring. I'm assuming you're aiming for a shot counter here.

Anyways, there's lots of stuff on the forum for how to run 7-segment displays. Maybe this search will help you (not restricted to this forum, BTW). arduino stopwatch 7-segment. I realize you aren't building a stopwatch, but the concept of counting down is the same. It's just that your triggering event isn't time, but whatever it is you'll be using to detect shots fired.

You should standardize your use of the 7 segment as well, call them what the rest of the world does. A F B G E C D

you can try and take a look at some code i made (with alot of help from crossroads and others :) )

its on pastebin: http://pastebin.com/rKeKzwSv

the code uses 3 multiplexed common anode Displays. I have started an instructable on how to make it, and it might help you out, and feel free to ask me about my code :D

(the incomplete instructable: http://www.instructables.com/id/Make-an-Arduino-timer)

Thank you guys for your help! I searched online and found a counter code that counts up to 9 so i reversed it to count down from 9 to zero. But, I'm having trouble with double digits. But thank you for all the help! I do not take credit for the code because all i did was tweak it a little but so far this is what I have.

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8
#define GND1 9
#define GND2 10

// Pushbutton connected to pin 9
#define BUTTON 11

// Common anode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW

int count = 10; // current display count
int val = 0;   // digital input from button

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(BUTTON, INPUT);
  ten();
}

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    count--;
    delay(300);
    switch (count) {
      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;
      case 10:
        ten();
      count = -1;
        break;
      }
    }
  }
}

// 0 => ABCDEF
void zero() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, OFF);
  digitalWrite(GND1, OFF);
}

// 1 => BC
void one() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
  digitalWrite(GND1, OFF);
}

// 2 => ABDEG
void two() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
}

// 3 => ABCDG
void three() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
}

// 4 => BCFG
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
}

// 5 => ACDFG
void five() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
}

// 6 => ACDEFG
void six() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
}

// 7 => ABC
void seven() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
  digitalWrite(GND1, OFF);
}

// 8 => ABCDEFG
void eight() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
}

// 9 => ABCDFG
void nine() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  digitalWrite(GND1, OFF);
  digitalWrite(GND2, ON);
}
// 10=> 1.BC 2.ABCDEF
 void ten() { 
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
  digitalWrite(GND1, ON);
  digitalWrite(GND2, OFF);
  delay(5);
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, OFF);
  digitalWrite(GND1, OFF);
  digitalWrite(GND2, ON);
}

I do not take credit for the code

Good for you, because it’s crap code :slight_smile:

Here’s a rough example of a better way to do it (well 1 digit at least)

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8
#define GND1 9
#define GND2 10

// Pushbutton connected to pin 9
#define BUTTON 11

// Common anode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW

int count = 10; // current display count
int val = 0;   // digital input from button

//             A B C D E F G
int pins [] = {2,3,4,5,6,7,8}; // the Arduino pins that drive the segments

byte segments[] = { // you set the correct bits B7 = G, B0 = A
	B00000000,	// 0
	B00110011,	// 1
	B00110011,	// 2
	B00110011,	// 3
	B00110011,	// 4
	B00110011,	// 5
	B00110011,	// 6
	B00110011,	// 7
	B00110011,	// 8
	B00110011	// 9
}
	

void setup() {
  for (int i = 0; i < 7; i++)
	pinMode(pins, OUTPUT);
  
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(BUTTON, INPUT);
  ten();
}

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    count--;
    delay(300);
	set_segments (count);
  }
}

void set_segments (byte val) {

	for (int seg = 0; seg < 7; seg++) {
		digitalWrite(seg, val & 1);	
		val >>= 1;
	}
}

I say “rough” because it’s late and I’ve had a couple of beers, so it won’t work and may not even compile but I’m just trying to show you that whenever you reach for the cut and paste keys that’s a sign that arrays and loops are a better way to do things.


Rob

Hi guys;

Here a code to count-up or count-down, using 1 switch to stop counting and a switch to choose up/down. I made that code. The circuit is a simple transitor to control a segment. ( I don’t like overloading my Arduino pins )

/* Size : 2462

 count and up : 0 to F
 One switch to stop count
 one switch to choose Count Up or Count Down

 Use :
 1 CA 7 segment display
 7 1 K resistor ( at the base of the transistor )
 7 330 ohm resistor ( current limiting )
 7 NPN 2N3904 
 2 SPST switch
 2 1 K resistor for the switch.
 
 By Serge J Desjardins  aka techone
 Toronto, Ontario  Canada

 Compile and Tested
 
*/ 


const byte pin[9] = {12,11,10,9,8,7,6,5,4};

boolean button[2] = {LOW, LOW};

byte seg;
int count;

void setup ()
{
 for (seg=0; seg<7;seg++)
 {
  pinMode (pin[seg], OUTPUT);
  digitalWrite (pin[seg], LOW); 
 }  
 for (seg=7; seg<10; seg++)
 {
  pinMode (pin[seg], INPUT);
 } 
 count=0; 
}

void loop ()
{
  button[0]= digitalRead (pin[7]);
  button[1]= digitalRead (pin[8]);
  delay (100);
  if (button[0]==LOW) 
  {
   if (button[1]==LOW)
   { 
    segmentdisplay();
    delay (500);
    count++;
    if (count==16)
     {
     count=0;
     }
   }
  else
  {
   if (count==-1)
   {
   count=15;
   }
  segmentdisplay();
  delay (500);
  count--;  
  }  
 }  
}  

void segmentdisplay()
{
  switch (count)
  {
   case 0:
      for (seg=0; seg<6; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }
       digitalWrite (pin[6], LOW);  
      break;
   
   case 1:
      digitalWrite (pin[0], LOW);
      for (seg=1; seg<3; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }
      for (seg=3; seg<7; seg++)
       {
         digitalWrite (pin[seg], LOW);
       }
      break;
      
   case 2:
     digitalWrite (pin[0], HIGH);
     digitalWrite (pin[1], HIGH);
     digitalWrite (pin[2], LOW); 
     digitalWrite (pin[3], HIGH);  
     digitalWrite (pin[4], HIGH);
     digitalWrite (pin[5], LOW);
     digitalWrite (pin[6], HIGH); 
    break;
    
   case 3:
     for (seg=0; seg<4; seg++)
     {
     digitalWrite (pin[seg], HIGH);
     }
     digitalWrite (pin[4], LOW);
     digitalWrite (pin[5], LOW);
     digitalWrite (pin[6], HIGH);  
    break;
 
   case 4:
     digitalWrite (pin[0], LOW);
     digitalWrite (pin[1], HIGH);
     digitalWrite (pin[2], HIGH); 
     digitalWrite (pin[3], LOW);  
     digitalWrite (pin[4], LOW);
     digitalWrite (pin[5], HIGH);
     digitalWrite (pin[6], HIGH); 
    break;
    
   case 5:
     digitalWrite (pin[0], HIGH);
     digitalWrite (pin[1], LOW);
     digitalWrite (pin[2], HIGH); 
     digitalWrite (pin[3], HIGH);  
     digitalWrite (pin[4], LOW);
     digitalWrite (pin[5], HIGH);
     digitalWrite (pin[6], HIGH); 
    break;
    
   case 6:
      digitalWrite (pin[0], HIGH);
      digitalWrite (pin[1], LOW);
      for (seg=2; seg<7; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }       
    break;
   case 7:
      for (seg=0; seg<3; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }
     for (seg=3; seg<7; seg++)
     {  
     digitalWrite (pin[seg], LOW);
     }      
    break;
   case 8:
     for (seg=0; seg<7; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }
     break;
   case 9:
     for (seg=0; seg<4; seg++)
      {
         digitalWrite (pin[seg], HIGH);
      }
     digitalWrite (pin[4], LOW);
     digitalWrite (pin[5], HIGH);
     digitalWrite (pin[6], HIGH);   
     break;
   case 10:
      for (seg=0;seg<3; seg++)
      {
       digitalWrite (pin[seg], HIGH);
      } 
      digitalWrite (pin[3], LOW);
      for (seg=4; seg<7; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }       
     break;
   case 11:
      digitalWrite (pin[0], LOW);
      digitalWrite (pin[1], LOW);
      for (seg=2; seg<7; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }       
     break;   
   case 12:
      digitalWrite (pin[0], HIGH);
      digitalWrite (pin[1], LOW);
      digitalWrite (pin[2], LOW);
      for (seg=3; seg<6; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }
      digitalWrite (pin[6], LOW); 
     break;
   case 13:
      digitalWrite (pin[0], LOW);
      for (seg=1; seg<5; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }
     digitalWrite (pin[5], LOW);
     digitalWrite (pin[6], HIGH);  
    break;
  case 14:
      digitalWrite (pin[0], HIGH);
      for (seg=1; seg<3; seg++)
       {
         digitalWrite (pin[seg], LOW);
       }
      for (seg=3; seg<7; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       } 
  break;
   case 15:
       digitalWrite (pin[0], HIGH);
      for (seg=1; seg<4; seg++)
       {
         digitalWrite (pin[seg], LOW);
       }
      for (seg=4; seg<7; seg++)
       {
         digitalWrite (pin[seg], HIGH);
       }  
    break;  
  }  
  
}

@Graynomad

I say “rough” because it’s late and I’ve had a couple of beers, so it won’t work and may not even compile but I’m just trying to show you that whenever you reach for the cut and paste keys that’s a sign that arrays and loops are a better way to do things.

What type of beer your drinking down in Aussie ? In Canada, it “Canadian” & Labbat’s Blue <–For me. :smiley:

Got to be Foster's, eh Rob?

No not Fosters although I used to drink that when I was in England.

I haven’t bought normal beer for about 10 years, the last “slab” (24 pack) I got was XXXX (A Queensland beer) for $35 up in the Kimberley (northern tropics). That was so expensive to me at the time I started making home brew and have done so ever since.

So why do Queenslanders call their state beer XXXX, because they can’t spell beer. :slight_smile:


Rob

@ Graynomad

Doing home brew..that is pretty good & CHEAP. :grin:

Anyway, so what you think of my Up/Down Counter program ? I hope it help others. I use a "look-up" table and a Switch/Case. It just count on digit. The basic is there. Any room for improvement ?

The code is OK for a start, but the segmentdisplay() func is about 150 lines long and as far as I can see it does the same as my 6-line function.

You've made a start by having pin numbers in an array and using a few loops to set pins, I'd see if you can reduce it even more.


Rob

The code is OK for a start, but the segmentdisplay() func is about 150 lines long and as far as I can see it does the same as my 6-line function.

6 line ? I wonder how... I have 16 hex numbers in the look-up table... I know I am not to experience in programing, but I doing my best. Can you tell me an example ? Anything to cut the program size..

Have a look back at reply #6, I think my set_segments() function (plus the "segments" array) does the same as your segmentdisplay() function.

Just add another 6 entries to segmenst[] as it currently only does 0-9.


Rob

@Graynomad

I copy & paste your code from #6...I run the IDE...and...no compile..full of "bugs" I wil try to fix it.

Must have been the beer :slight_smile:

Here’s a version that compiles

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8
#define GND1 9
#define GND2 10

// Pushbutton connected to pin 11
#define BUTTON 11

// Common anode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW

int count = 10; // current display count
int val = 0;   // digital input from button

//             A B C D E F G
int pins [] = {2,3,4,5,6,7,8}; // the Arduino pins that drive the segments

byte segments[] = { // you set the correct bits B7 = G, B0 = A
	B00000000,	// 0
	B00110011,	// 1
	B00110011,	// 2
	B00110011,	// 3
	B00110011,	// 4
	B00110011,	// 5
	B00110011,	// 6
	B00110011,	// 7
	B00110011,	// 8
	B00110011,      // 9
	B00110011,	// B
	B00110011,	// C
	B00110011,	// D
	B00110011,	// E
	B00110011 	// F
};


void setup() {
  for (int i = 0; i < 7; i++)
	pinMode(pins[i], OUTPUT);
  
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(BUTTON, INPUT);
  
}

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    count--;
    delay(300);
	set_segments (count);
  }
}

void set_segments (byte val) {
	for (int seg = 0; seg < 7; seg++) {
		digitalWrite(pins[seg], val & 1);	
		val >>= 1;
	}
}

Rob

Yep. That is better. I will later try to figure out this program.

Beer & Programing <— Don’t Mix XD