Fluctuations 74HC165 data

Hi everyone, I’m new to electronics and in the world of Arduino, I’ve just 1 month.

My question is this, I have a 74HC165 connected to four buttons, but the data IC, when the show in the Serial monitor, I get data fluctuations. Does anyone know why?.
Thank you.

//Define pins
int pinDataIC = 9;
int pinClockIC = 10;
int pinLanchIC = 1;
int tempo = 60;
byte button = 0;
int show = 1;

void setup()
{
  //initialize the pin
  pinMode(pinDataIC, INPUT);
  pinMode(pinClockIC, OUTPUT);
  pinMode(pinLanchIC, OUTPUT);
  
  //iniciate serial comminication
  Serial.begin(9600);
}

void loop()
{
  //some variables
  byte oldbutton;
  int pausenote;
  String notearray[] = {"Redonda", "Blanca", "Negra", "Corchea", "Tresillo", "Semicorchea", "Seisillo"};
  int numarray[] = {0, 0, 0, 1, 2, 3, 5};
  int index = 2;
  String note;
  int numero;
  int i = 0;
  
  //parallel load
  digitalWrite(pinLanchIC, HIGH);
  delayMicroseconds(20);
  //serial shift
  digitalWrite(pinLanchIC, LOW);
  
  //Read 74HC165
  button = shiftIn(pinDataIC, pinClockIC, MSBFIRST);
  digitalWrite(pinLanchIC, HIGH);
  //Show the result
  Serial.println(button);
  
  if ((button - oldbutton) == 2)
  {
    tempo ++;
    show = 1;
    //beat = beat + 30;
  }
  else if ((oldbutton - button) == 4)
  {
    tempo --;
    show = 1;
    //beat = beat - 30;
  }
  else if ((oldbutton - button) == 8)
  {
    index ++;
    show = 1;
  }
  else if ((oldbutton - button) == 16)
  {
    index --;
    show = 1;
  }
  oldbutton = button;
  numero = numarray[index];
  note = notearray[index];
  if (show == 1)
  {
    Serial.print("TEMPO: ");
    Serial.print(tempo);
    Serial.print(" NOTE: "); 
    Serial.println(note);
    show = 0;
  }
  delay (1000);
}

Hi Zhorton1972 and welcome,

I'm not sure I understand what you mean by "fluctuations". Can you post some examples?

I think you should try this: don't use output 1 to connect to the latch pin of the chip. Output 1 is used by the Arduino when sending serial communications to the PC, and during sketch upload. Using it may be giving your unexpected results. Use another output, for example 8 or 11, instead.

Paul

Hi PaulRB, and thanks.

There are several examples. If i don't touch any button, i get these readings in monitor serial: - 160 - 160 - 160 - 176 - 160 - 160

You must not have any of the inputs to the shift register connected to nothing. The ones you are not using should be connected to ground or +5V.

I change the arduino pin to number 8, but it's the same.

Zhorton1972: I change the arduino pin to number 8, but it's the same.

Of course it is the same, read my reply.