Go Down

Topic: Counting with 7 segs and 74HC595 and 74HC4511 (Read 31 times) previous topic - next topic

rosscopicotrain

Hi,
I am working on a project based on the schematic at this address
http://www.kevindarrah.com/wp-content/uploads/2012/05/ARDUINO-7-SEG-DISPLAY-REV1.1.pdf

In my code I have set up a counter and have broken the counter number into its separate digits for displaying on 4 x 7 segment LED displays.

Then I am trying to read each value from the array "digit" into an integer (is this even possible?) using single bit shifts, so that I can send the data serially to the 74HC595 Shift Register and 74HC4511 Display Driver

The output on the serial monitor for the variable "displayVal" is all over the place. I'm pretty sure the source of the problem is line 54, but I have no idea how to make it work.

Any pointers would be most welcome, thanks in advance!


Code: [Select]


// The HIGH nibble is sent to the 4511 BCD to 7 Seg
// The LOW nibble is used to turn on each digit

int displayValue = 0; // value to be displayed
int place[4]; // array for storing the place values
int digit[4]; // array for storing 4 digit readings

////Pin connected to DS of 74HC595
int dataPin = 4;
//Pin connected to SH_CP of 74HC595
int clockPin = 3;
//Pin connected to ST_CP of 74HC595
int latchPin = 2;

int pulses, pulsesOld, A_SIG=0, B_SIG=1, displayVal, startTime, grindTime, count;
int buttonPin = 2; // tactile button in rotary encoder // Digital Pin 2 = PIN 4
int buttonPinState; // state for button pin
int lastButtonPinState;
int grindPin;   // define pin driving the grinder here

int intervalPeriod = 100;  // 100ms interval between display updates




void setup() {
  //attachInterrupt(0, A_RISE, RISING);
  //attachInterrupt(1, B_RISE, RISING);
  Serial.begin(115200);
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); // 74HC595 Pins
  pinMode(dataPin, OUTPUT);
 
  pinMode(buttonPin, INPUT); // button in rotary encoder
  pinMode(grindPin, OUTPUT); // Output to control grinder
}

void loop() {
  // count from 0 to 9999 and display the number   
  for (int counter = 0; counter <= 9999; counter++) {
    // break the number into its digits
    digit[3] = counter/ 1000;          // thousands place
    digit[2] = (counter- (digit[0]*1000)) / 100;    // hundreds place
    digit[1] = (counter- (digit[0]*1000) - (digit[1]*100)) / 10;      // tens place
    digit[0] = (counter- (digit[0]*1000) - (digit[1]*100) - (digit[2]*10));  // units place
    Serial.println(counter);
   
    for (int i=3; i>=0; i--)
    {
    place[i] = i; // assign place names to match
    displayValue = digit[i] << 1; // start with MSB, shift in each element of digit array to an integer type variable
    }
   
    Serial.print("Display Val = ");
    Serial.println(displayValue);
    // take the latchPin low so
    // the LEDs don't change while you're sending in bits:
    //digitalWrite(latchPin, LOW);
    // shift out the bits:
    //shiftOut(dataPin, clockPin, MSBFIRST, displayValue); 

    //take the latch pin high so the LEDs will light up:
//    digitalWrite(latchPin, HIGH);
    // pause before next value:
 //   delay(30);
   
  } // end 0-9999 counting loop
} // end main loop


 
 int readEncoder(void)
{
  if (pulses > pulsesOld){    // if we are going forwards
    count ++;              // increment by 0.1

  }
  else if(pulses < pulsesOld){    // same for backwards
    count --;
  }
  else{
    displayVal = count;
  }
  pulsesOld = pulses;
  displayVal = count;
  return displayVal;
}

Go Up