Don't like ads? Help support the Mercs by becoming a Supporter or Auxiliary Member today!
Official Members also get to use the forum ad-free - so kit up and join us!

 Arduino Ammo Counter

  • 2 Replies
  • 3418 Views

Kuu


    *
  • *
  • *
  • *
  • *
  • *
  • 3352
  • As Haran Mhi Nu'Lise' - Ja'haili' Ni
  • Awards 15+ clan level invasions Order of the Ori'Ramikad Poster Boy - Official Member appears in costume in the most photos Award for 10 official invasions. Hard Contact - Clan with the most recorded invasions
Arduino Ammo Counter
« on: May 02, 2015, 01:53 AM »
YouTube Vid of Test 1
https://youtu.be/KFhcRn7FD6w

Ideas for making the code more efficient welcome, I'm only just started with Arduino.

Part : http://www.ebay.com/itm/2pcs-MAX7219-MCU-Control-LED-Display-Dot-Matrix-Module-For-Arduino-DIY-Kit-/181221282262

Wiring of that particular board:
MAX7219 MCUArduino UNO
VCC5v
GNDGND
DIN12
CS10
CLK11

Switches to 13 and 9

Code: [Select]


// MAX7219 8x8 Matrix 2 button Ammo Counter
// by Matt "Kuu" Ragan
// Arduino 1.6.3

const int buttonPin= 13;
const int buttonReloadPin=9;
int buttonState = 0;
int buttonReloadState = 0;
int currentRound = 64;

#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);

void setup() {             
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,5);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
 
  pinMode(buttonPin,INPUT);
  pinMode(buttonReloadPin,INPUT);
  Serial.begin(9600);
}

void  MAX7219Write(byte digit) {
  switch (digit)
  {
   case 64:
     lc.setRow(0,0,B11111111);
     lc.setRow(0,1,B11111111);
     lc.setRow(0,2,B11111111);
     lc.setRow(0,3,B11111111);
     lc.setRow(0,4,B11111111);
     lc.setRow(0,5,B11111111);
     lc.setRow(0,6,B11111111);
     lc.setRow(0,7,B11111111);
     break;
  case 63:
     lc.setRow(0,0,B11111110);
     break;
  case 62:
     lc.setRow(0,0,B11111100);
     break;
  case 61:
     lc.setRow(0,0,B11111000);
     break;
  case 60:
     lc.setRow(0,0,B11110000);
     break;
  case 59:
     lc.setRow(0,0,B11100000);
     break;
  case 58:
     lc.setRow(0,0,B11000000);
     break;
  case 57:
     lc.setRow(0,0,B10000000);
     break;
  case 56:
     lc.setRow(0,0,B00000000);
     break;
  case 55:
     lc.setRow(0,1,B11111110);
     break;
  case 54:
     lc.setRow(0,1,B11111100);
     break;
  case 53:
     lc.setRow(0,1,B11111000);
     break;
  case 52:
     lc.setRow(0,1,B11110000);
     break;
  case 51:
     lc.setRow(0,1,B11100000);
     break;
  case 50:
     lc.setRow(0,1,B11000000);
     break;
  case 49:
     lc.setRow(0,1,B10000000);
     break;
  case 48:
     lc.setRow(0,1,B00000000);
     break;
  case 47:
     lc.setRow(0,2,B11111110);
     break;
  case 46:
     lc.setRow(0,2,B11111100);
     break;
  case 45:
     lc.setRow(0,2,B11111000);
     break;
  case 44:
     lc.setRow(0,2,B11110000);
     break;
  case 43:
     lc.setRow(0,2,B11100000);
     break;
  case 42:
     lc.setRow(0,2,B11000000);
     break;
  case 41:
     lc.setRow(0,2,B10000000);
     break;
  case 40:
     lc.setRow(0,2,B00000000);
     break;
  case 39:
     lc.setRow(0,3,B11111110);
     break;
  case 38:
     lc.setRow(0,3,B11111100);
     break;
  case 37:
     lc.setRow(0,3,B11111000);
     break;
  case 36:
     lc.setRow(0,3,B11110000);
     break;
  case 35:
     lc.setRow(0,3,B11100000);
     break;
  case 34:
     lc.setRow(0,3,B11000000);
     break;
  case 33:
     lc.setRow(0,3,B10000000);
     break;
  case 32:
     lc.setRow(0,3,B00000000);
     break;
  case 31:
     lc.setRow(0,4,B11111110);
     break;
  case 30:
     lc.setRow(0,4,B11111100);
     break;
  case 29:
     lc.setRow(0,4,B11111000);
     break;
  case 28:
     lc.setRow(0,4,B11110000);
     break;
  case 27:
     lc.setRow(0,4,B11100000);
     break;
  case 26:
     lc.setRow(0,4,B11000000);
     break;
  case 25:
     lc.setRow(0,4,B10000000);
     break;
  case 24:
     lc.setRow(0,4,B00000000);
     break;
  case 23:
     lc.setRow(0,5,B11111110);
     break;
  case 22:
     lc.setRow(0,5,B11111100);
     break;
  case 21:
     lc.setRow(0,5,B11111000);
     break;
  case 20:
     lc.setRow(0,5,B11110000);
     break;
  case 19:
     lc.setRow(0,5,B11100000);
     break;
  case 18:
     lc.setRow(0,5,B11000000);
     break;
  case 17:
     lc.setRow(0,5,B10000000);
     break;
  case 16:
     lc.setRow(0,5,B00000000);
     break;
  case 15:
     lc.setRow(0,6,B11111110);
     break;
  case 14:
     lc.setRow(0,6,B11111100);
     break;
  case 13:
     lc.setRow(0,6,B11111000);
     break;
  case 12:
     lc.setRow(0,6,B11110000);
     break;
  case 11:
     lc.setRow(0,6,B11100000);
     break;
  case 10:
     lc.setRow(0,6,B11000000);
     break;
  case 9:
     lc.setRow(0,6,B10000000);
     break;
  case 8:
     lc.setRow(0,6,B00000000);
     break;
  case 7:
     lc.setRow(0,7,B11111110);
     break;
  case 6:
     lc.setRow(0,7,B11111100);
     break;
  case 5:
     lc.setRow(0,7,B11111000);
     break;
  case 4:
     lc.setRow(0,7,B11110000);
     break;
  case 3:
     lc.setRow(0,7,B11100000);
     break;
  case 2:
     lc.setRow(0,7,B11000000);
     break;
  case 1:
     lc.setRow(0,7,B10000000);
     break;
  case 0:
     lc.setRow(0,0,B11111111);
     lc.setRow(0,1,B11000011);
     lc.setRow(0,2,B10100101);
     lc.setRow(0,3,B10011001);
     lc.setRow(0,4,B10011001);
     lc.setRow(0,5,B10100101);
     lc.setRow(0,6,B11000011);
     lc.setRow(0,7,B11111111);
     break;
  }
}

void loop() { 
  MAX7219Write(currentRound);
   
  buttonState = digitalRead(buttonPin);
  buttonReloadState = digitalRead(buttonReloadPin);
 
  if (buttonReloadState==0)
  {
    currentRound=64;
    lc.clearDisplay(0);
     lc.setRow(0,0,B00111100);
     lc.setRow(0,1,B00111100);
     lc.setRow(0,2,B00111100);
     lc.setRow(0,3,B01111110);
     lc.setRow(0,4,B11011011);
     lc.setRow(0,5,B10011001);
     lc.setRow(0,6,B11011011);
     lc.setRow(0,7,B00011000);
     delay(1000);
  };
 
  if (buttonState==0)
  {
   if(currentRound>0)
   {
     --currentRound;
   }
  }
 
  Serial.print("buttonState == ");
  Serial.println(buttonState);
  Serial.print("buttonReloadState == ");
  Serial.println(buttonReloadState);
  Serial.println(currentRound);
  delay(100);

}

« Last Edit: May 02, 2015, 01:59 AM by Kuu » Logged

| aka "Matt R." | Helpful Stuff |Facebook | VRCA #053 | M.V.S. #10 | B.E.A.R.D.S. #77 | S.P.A.R.C.K. #2 | OM: Sept. 20, 2014

Kuu


    *
  • *
  • *
  • *
  • *
  • *
  • 3352
  • As Haran Mhi Nu'Lise' - Ja'haili' Ni
  • Awards 15+ clan level invasions Order of the Ori'Ramikad Poster Boy - Official Member appears in costume in the most photos Award for 10 official invasions. Hard Contact - Clan with the most recorded invasions
Re: Arduino Ammo Counter
« Reply #1 on: May 03, 2015, 12:08 AM »
Tweaked the code, hardware is the same. This one uses the Mando font

https://youtu.be/H2nwUgdG2Rk

I added 2 functions to handle the "out of ammo" graphic and the "reload" graphic.

Code: [Select]
// MAX7219 8x8 Matrix 2 button Ammo Counter in Mandalorian Font
// by Matt "Kuu" Ragan
// Arduino 1.6.3

const int buttonPin= 13;
const int buttonReloadPin=9;
int buttonState = 0;
int buttonReloadState = 0;
int currentRound = 99; //number of rounds at boot

#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);

void setup() {             
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,8);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
 
  pinMode(buttonPin,INPUT);
  pinMode(buttonReloadPin,INPUT);
  Serial.begin(9600);
}

void  MAX7219Write(byte digit, byte colstart) {
  switch (digit)
  {
case 9:
     lc.setColumn(0,colstart-3,B11111111);
     lc.setColumn(0,colstart-2,B00000001);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B10000000);
     break;
  case 8:
     lc.setColumn(0,colstart-3,B00000000);
     lc.setColumn(0,colstart-2,B00000000);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B00000001);
     break;
  case 7:
     lc.setColumn(0,colstart-3,B00000001);
     lc.setColumn(0,colstart-2,B11111111);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B00000001);
     break;
  case 6:
     lc.setColumn(0,colstart-3,B00000000);
     lc.setColumn(0,colstart-2,B00000000);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B10000001);
     break;
  case 5:
     lc.setColumn(0,colstart-3,B10000001);
     lc.setColumn(0,colstart-2,B11111111);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B10000001);
     break;
  case 4:
     lc.setColumn(0,colstart-3,B11111111);
     lc.setColumn(0,colstart-2,B10000000);
     lc.setColumn(0,colstart-1,B10000000);
     lc.setColumn(0,colstart,B11111111);
     break;
  case 3:
     lc.setColumn(0,colstart-3,B10000000);
     lc.setColumn(0,colstart-2,B11111111);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B10000000);
     break;
  case 2:
     lc.setColumn(0,colstart-3,B11111111);
     lc.setColumn(0,colstart-2,B00000000);
     lc.setColumn(0,colstart-1,B10000000);
     lc.setColumn(0,colstart,B11111111);
     break;
  case 1:
     lc.setColumn(0,colstart-3,B00000000);
     lc.setColumn(0,colstart-2,B00000000);
     lc.setColumn(0,colstart-1,B11111111);
     lc.setColumn(0,colstart,B00000000);
     break;
  case 0:
     lc.setColumn(0,colstart-3,B11111111);
     lc.setColumn(0,colstart-2,B10000001);
     lc.setColumn(0,colstart-1,B10000001);
     lc.setColumn(0,colstart,B11111111);
     break;
  }
}

void reloadEmblem()
{
     lc.setRow(0,0,B00111100);
     lc.setRow(0,1,B00111100);
     lc.setRow(0,2,B00111100);
     lc.setRow(0,3,B01111110);
     lc.setRow(0,4,B11011011);
     lc.setRow(0,5,B10011001);
     lc.setRow(0,6,B11011011);
     lc.setRow(0,7,B00011000);
}

void outOfAmmoEmblem()
{
     lc.setRow(0,0,B11111111);
     lc.setRow(0,1,B11000011);
     lc.setRow(0,2,B10100101);
     lc.setRow(0,3,B10011001);
     lc.setRow(0,4,B10011001);
     lc.setRow(0,5,B10100101);
     lc.setRow(0,6,B11000011);
     lc.setRow(0,7,B11111111);
}

void loop() { 
  if(currentRound>0)
  {
    MAX7219Write(currentRound/10, 3); //tens place
    MAX7219Write(currentRound%10, 7); //ones place
  }
  else
  {
    outOfAmmoEmblem();
  }
   
  buttonState = digitalRead(buttonPin);
  buttonReloadState = digitalRead(buttonReloadPin);
 
  if (buttonReloadState==0)
  {
    currentRound=99; //set reload number of rounds
    lc.clearDisplay(0);
    reloadEmblem();
    delay(1000);
  };
 
  if (buttonState==0)
  {
   if(currentRound>0)
   {
     --currentRound;
   }
  }
 
  Serial.print("buttonState == ");
  Serial.println(buttonState);
  Serial.print("buttonReloadState == ");
  Serial.println(buttonReloadState);
  Serial.println(currentRound);
  delay(100);

}

« Last Edit: May 04, 2015, 07:57 AM by Kuu » Logged

| aka "Matt R." | Helpful Stuff |Facebook | VRCA #053 | M.V.S. #10 | B.E.A.R.D.S. #77 | S.P.A.R.C.K. #2 | OM: Sept. 20, 2014

Ralin Voth


    *
  • *
  • 3839
  • "The Rancor"
  • Awards Clan with the most recorded invasions in 2016/2017 Award for 25 official invasions. Recruitment - Clan with the largest number of new OM's in 12 months Award for 10 official invasions. Hard Contact - Clan with the most recorded invasions
Re: Arduino Ammo Counter
« Reply #2 on: May 04, 2015, 12:18 AM »
That's really freakin' awesome! Makes me want to put one in my rifle now, if I ever finish it.

Logged
 


Don't like ads? Help support the Mercs by becoming a Supporter or Auxiliary Member today!
Official Members also get to use the forum ad-free - so kit up and join us!




Powered by EzPortal
anything