Arduino Counter

Posted by Jake Good
on May 25, 08

This one is for my boy Mike... he's trying to build some toys for his air hockey table and I thought I'd help out and do a simple demonstration... of course, with my Arduino...

drop.io: simple private sharing


I'm pretty proud of this one, as I did this entire thing without any help at all... With basic Arduino commands learned from some tutorials, I've put together a simplistic program that displays numbers on the 7 Segment LED

I really wish I could get good with drawing schematics and layouts with Eagle, but it's soo dang hard!

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;

int pinSets[10][7] = {
  {a,b,c,d,e,f,-1}, // 0
  { -1,b,c,-1,-1,-1,-1}, // 1
  {a,b,-1,d,e,-1,g}, // 2 
  {a,b,c,d,-1,-1,g}, // 3
  {-1,b,c,-1,-1,f,g}, // 4
  {a,-1,c,d,-1,f,g}, // 5
  {a,-1,c,d,e,f,g}, // 6
  {a,b,c,-1,-1,-1,-1}, // 7
  {a,b,c,d,e,f,g}, // 8 
  {a,b,c,d,-1,f,g} // 9
};

int switchPin = 9;
int bState;
int val;
int buttonPresses = 0;

void setup() {
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(switchPin, INPUT);
  bState = digitalRead(switchPin);
}

void loop() {
  val = digitalRead(switchPin);

  if ((val != bState) && (val == LOW)){
      buttonPresses++;
      if(buttonPresses == 10)
      {
        buttonPresses = 0;
      }
  }
  clear();
  bState = val;
  displayNumber(buttonPresses);
}

void clear() {
  int i;
  for(i = 2; i < 9; i++){
    digitalWrite(i,LOW);
  }
}

void displayNumber(int n) {
  int i = 0;
  for(i = 0; i < 7; i++) {
    if(pinSets[n][i] != -1) {
      digitalWrite(pinSets[n][i],HIGH);
    }
  }
}
Comments

Leave a response

  1. mikeMay 25 08 @ 10:45AM
    I'm going to have to stop reading your blog so that I don't let you do all the fun research for me :) j/k
Comment