Delta_G:
The reason you're seeing seemingly random segments is because you are sending the wrong thing to the shift register.Look, you have an array with the bit patterns for the 7-segment in them. But when you call shiftout, instead of sending the bit pattern that corresponds to the number you want to display, you just send the number itself. So the display lights up the segments in the bit pattern that number represents.
Instead of shifting out data, try shifting out number_array[data].
Ive just tried switching to the array and though it gives returns actual numbers and letters now there completely random.
const byte latchPin = 7; // Pin connected to Pin 12 of 74HC595 (Latch)
const byte dataPin = 4; // Pin connected to Pin 14 of 74HC595 (Data)
const byte clockPin = 8; // Pin connected to Pin 11 of 74HC595 (Clock)
byte button = 13;
byte currentState = 1;
byte previousState = 0;
int counter = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 200;
int buttonState;
int lastButtonState = LOW;
bool b;
int number_array[16] = // 0-9 A-F
{
B11111100,//0
B01100000,//1
B11011010,//2
B11110010,//3
B01100110,//4
B10110110,//5
B10111110,//6
B11100000,//7
B11111110,//8
B11100110,//9
B11101110,//A
B00111110,//B
B10011100,//C
B01111010,//D
B10011110,//E
B10001110,//F
};
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(button, INPUT);
debounce(button, previousState);
writeToRegister(counter);
}
void loop() {
while(digitalRead(button) ==LOW){
debounce(button, previousState);
}
if(counter<16){
if(!b) {counter++;}writeToRegister(counter);}
else if (counter == 16) {counter = 0;}
}
void writeToRegister (byte data){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,number_array[data]);
digitalWrite(latchPin, HIGH);}
void debounce(int pin, byte &previousState){
byte currentState = 0;
byte b = 0;
currentState = digitalRead(pin);
if(currentState && !previousState){
while(b != 0xFF){
delay(1);
b=(b<<1) | digitalRead(pin);
}
}
previousState = currentState;
}
this is what I've got so far.