7 segment display - 74HC595 - code help!

So im trying to get one 7 segment display to showcase a 4 digit random generated password! Serial.print gives me the correct outputs but when using my function "displayCode" to show the numbers on the 7 segment display, it skips 3. so if it was supposed to show 0, 1, 2, 3 it will instead show 0, 3...

please help me :smiley:

#include <Keypad.h>
#include <Servo.h>

const byte ROWS = 4; 
const byte COLS = 4; 
const int red = A0;
const int green = A1;

const int button = A2;

const int LATCH = 12;  // Latch pin of 74HC595 is connected to Digital pin 5
const int CLK = 11; // Clock pin of 74HC595 is connected to Digital pin 6
const int DATA = 13;  // Data pin of 74HC595 is connected to Digital pin 4

const byte digit[]= {
  B11111100,  // 0
  B10010000,  // 1
  B01111010,  // 2
  B11011010,  // 3
  B10010110,  // 4
  B11001110,  // 5
  B11101110,  // 6
  B10011000,  // 7
  B11111110,  // 8
  B11011110,  // 9
  B00000000,  // nothing
};

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
Servo myservo;

String password = "";
String givenpassword = "";
int buttonState = 0;

void setup(){
  Serial.begin(9600);
  myservo.attach(10);
  Serial.println("Enter password");
  restartPuzzle();
  Serial.println(password);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(button, INPUT);
  
}
  
void loop(){
  buttonState = digitalRead(button);
  if (buttonState == HIGH){
    for (int i = 0; i < (password.length()); i++){
      int number = (password[i]-48);
      displayCode(number);
      Serial.println(number);
      displayCode(10);
    }
    }
  else{
    keypadfunction();
  }
}

void keypadfunction(){
  char customKey = customKeypad.getKey();
  if (customKey){
    if (String(customKey) != "#"){
      givenpassword += customKey;
      Serial.println(customKey);  
    }
    else{
      if (givenpassword == password){
        Serial.println("you did it");
        unlock();
      }
      else{
        Serial.println("you suck");
        restartPuzzle();
      }
      givenpassword = "";
    }
  }
}

void unlock(){
    myservo.write(0);
    //Green led light
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
  }
  
void lock(){
    myservo.write(95);
    //Red led light
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
  }

void createPassword(){
  password = String(random(1000,9999));
  }

void restartPuzzle(){
  lock();
  createPassword();
  Serial.println(password);
  }

void displayCode(int number) {
      digitalWrite(LATCH, LOW);
      digitalWrite(CLK, LOW);
      shiftOut(DATA, CLK, MSBFIRST, digit[number]);
      delay(500);
}

How many digits can this 7 segment display show at once ?

This is not going to get you very far:

For example, when is the LATCH going to be returned to HIGH ?

sorry uploaded the wrong code. Function looks like this:
its just weird, bc now I can get it show the numbers, but sometimes it just won't show anything, it just sort of skips a number or two in the sequence

void displayCode(int number) {
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLK, MSBFIRST, digit[number]);
      digitalWrite(LATCH, HIGH);
      delay(500);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.