Go Down

Topic: Controlling 5 digit 7 segment display (Read 518 times) previous topic - next topic

Hi! I made a program using this tutorial: http://tronixstuff.wordpress.com/2010/05/14/getting-started-with-arduino-chapter-six/.
Using analog read is working good, but after changing this with an algoritm using debounce, to count the number of impulses under x time, it displays only the last digit, and only if serial is started, but in serial monitor I can see the whole number.So please help me! This is my code:
#include <Bounce.h>
#define BUTTON 7
Bounce bouncer = Bounce( BUTTON,50 );

int j=0;

int latchpin = 8; // connect to pin 12 on the '595
int clockpin = 12; // connect to pin 11 on the '595
int datapin = 11; // connect to pin 14 on the '595
int k = 0;
int lhd = 0;
int mhd = 0;
int mhd2 = 0;
int mhd3 = 0;
int rhd = 0;
float a = 0;
int b = 0;
int c= 0;
int d = 0;
int e=0;
int g=0;
int f=0;
int h=0;


int leadingzero = 0; // 0 for no leading zereos, 1 for leading zeroes


int segdisp[10] = {
  252,96,202,242,38,182,190,224,254,246}; // base 10 equivalents for digits 0~9
int posdisp[5] = {
  1,2,4,8,16}; // base 10 equivalents to close cathodes on display 0~3 on module

void setup()
{
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
  pinMode(BUTTON, INPUT);
  Serial.begin(9600);
}


void cleardisplay()
// turns off all segments of all digits
{
  for (int aa=0; aa<5; aa++)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, posdisp[aa]); // sets the digit to address
    shiftOut(datapin, clockpin, MSBFIRST, 0); // clears the digit
    digitalWrite(latchpin, HIGH);
  }
}

void digitdisplay(int digit, int location)
// displays "digit" on display "location" 0~3
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, posdisp[location]); // sets the digit to address
  shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit]); // clears the digit
  digitalWrite(latchpin, HIGH);
}

void digitpointdisplay(int digit, int location)
// displays "digit" on display "location" 0~3 with the decimal point
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, posdisp[location]); // sets the digit to address
  shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit]+1); // clears the digit
  digitalWrite(latchpin, HIGH);
}

void onedigitnumber(int subject)
// displays a one-digit number on the display module with leading zeroes
{
  cleardisplay();
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
    digitdisplay(0,2);
    digitdisplay(0,3);
  }
  digitdisplay(subject,4);
}

void twodigitnumber(int subject)
// displays a two-digit number on the display module with leading zeroes
{
  cleardisplay();
  rhd = subject % 10;
  a = subject/10;
  lhd = int(a);
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
    digitdisplay(0,2);
  }
  digitdisplay(lhd,3);
  digitdisplay(rhd,4);
}

void threedigitnumber(int subject)
// displays a three-digit number on the display module with leading zeroes
{
  cleardisplay();
  a = subject/100;
  lhd = int(a);
  a = subject/10;
  b = int(a);
  mhd = b % 10;
  b=subject%100;
  rhd=b%10;
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
  }
  digitdisplay(lhd,2);
  digitdisplay(mhd,3);
  digitdisplay(rhd,4);
}

void fourdigitnumber(int subject)
// displays a four-digit number on the display module with leading zeros
{
  cleardisplay();
  a = subject/1000;
  lhd = int(a);
  b=lhd*1000;
  c=subject-b;
  a = c/100;
  mhd = int(a);
  a = c/10;
  b = int(a);
  mhd2 = b % 10;
  b=subject%1000;
  c=b%100;
  rhd=c%10;
  if (leadingzero==1)
  {
    digitdisplay(0,0);
   
  }
  digitdisplay(lhd,1);
  digitdisplay(mhd,2);
  digitdisplay(mhd2,3);
  digitdisplay(rhd,4);
}
void fivedigitnumber(int subject)
// displays a four-digit number on the display module with leading zeros
{
  cleardisplay();
  a = subject/10000;
  lhd = int(a);
  b=lhd*10000;
  c=subject-b;
  d = c/1000;
  mhd = int(d);
  b=mhd*1000;
  e=a*10000;
  f=b+e;
  c=subject-f;
  e = c/100;
  mhd2 = int(e);
  a=lhd*10000;
  b=mhd*1000;
  e=mhd2*100;
  g=a+b;
  h=g+e;
  b=subject-h;
  e = b / 10;
  mhd3=int(e);
  a=lhd*10000;
  b=mhd*1000;
  c=mhd2*100;
  d=mhd3*10;
  rhd=subject-a-b-c-d;
  digitdisplay(lhd,0);
  digitdisplay(mhd,1);
  digitdisplay(mhd2,2);
  digitdisplay(mhd3,3);
  digitdisplay(rhd,4);
}

void displaynumber(int rawnumber)
// takes an integer and displays it on our 4-digit LED display module
{
  if (rawnumber>=0 && rawnumber<10)
  {
    onedigitnumber(rawnumber);
  }
  else if (rawnumber>=10 && rawnumber<100)
  {
    twodigitnumber(rawnumber);
  }
  else if (rawnumber>=100 && rawnumber<1000)
  {
    threedigitnumber(rawnumber);
  }
  else if (rawnumber>=1000 && rawnumber<10000)
  {
    fourdigitnumber(rawnumber);
  }
  else if (rawnumber>=10000)
  {
    fivedigitnumber(rawnumber);
  }
}

void loop()
{
 
  for(int x=0; x<=1000;x++)
  {
     bouncer.update();
     
    if(bouncer.risingEdge())
    {
     
      j++;

    }
   
  Serial.println(j);
   
  }

  k=j;
Serial.print("j=");
Serial.println(k);
  displaynumber(k);
j=0;
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy