Reading data from register 74HC165

Hi everyone,
i'm making a small test by using a register 74HC165, 3 switchers and a led matrix.
The idea is to use the design for Arduino UNO projects where i need many i/o

So the test is simple :
1- Put the wished configuration of the switchers
for exp.:
switcher 1 is on High position
switcher 2 is on low position
switcher 3 is on low position

2- A smile face then is played on the matrix according to the configuration of the switchers chosen previously

right now it's not working. I think i can not read / find away to read the given values from register.

Could you please kindly help. Thanks.
Here the code:

#include <SPI.h>
#include <LedControl.h>

//74HC165 for switchers
const int dataPin2 = 8;   /* Q7 */
const int clockPin2 = 9;  /* CP */
const int latchPin2 = 12;  /* PL */
int switcherstatus = 000;
int DIN = 11;
int CS =  10;
int CLK = 13;
LedControl lc = LedControl(DIN, CLK, CS, 0);

void setup()
{
  Serial.begin(115200);
  pinMode(latchPin2, OUTPUT);
  pinMode(clockPin2, OUTPUT);
  pinMode(dataPin2, INPUT);

  lc.shutdown(0, false);
  lc.setIntensity(0, 15);

}

void loop()
{
  
  digitalWrite(latchPin2, LOW);
  digitalWrite(latchPin2, HIGH);
  for (int i = 0; i < 3; i++)
  {
    switcherstatus = digitalRead(dataPin2);
    digitalWrite(clockPin2, HIGH);
    digitalWrite(clockPin2, LOW);
  }

 Serial.print(switcherstatus);
  if (switcherstatus == 100) {
    smile(); 
  }

}

void smile()
{
  byte SMILE[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
  for (int i = 0; i < 8; i++) {
    lc.setRow(0, i, SMILE[i]);
  }
}

void sad()
{
  byte SAD[8] = {0x3C, 0x42, 0xA5, 0x81, 0x99, 0xA5, 0x42, 0x3C};
  for (int i = 0; i < 8; i++) {
    lc.setRow(0, i, SAD[i]);
  }
}

Below is my wokwi project:

    switcherstatus = digitalRead(dataPin2);

This will set switcherstatus to either 0 or 1 so it can never equal 100

You need to read up on how to set individual bits in a variable using the bitSet() function as you read them in. Once the correct bits have been set to the value that has been read you can test whether the variable equals 0b00000100

1 Like
  • Are you connecting this to real components ?

No
I just testing the code for the moment.

  • Do you understand post #2 ?

You are right
so i tried this

   byte  switcherstatus = 0x00;
   for (int n = 7; n>=5; n--)
   {
   if (digitalRead (dataPin2) ==HIGH)
   bitSet(switcherstatus,n);
   //Serial.println (switcherstatus);
   digitalWrite(clockPin2, HIGH);
   digitalWrite(clockPin2, LOW);
   }

but then i need to call the conbination in my condition in decimal how can i call the combination in binary?
right now i have this:

if (switcherstatus == 224) {
    smile(); 
  }
  if (switcherstatus == 0) {
    sad(); 
  }

May be you can explain more your request.
what is your point by this question.

  • If you had said you didn’t understand, it would be further explained.

  • I don’t respond to simulation only threads, was confirming you were not using real hardware.
    For example, the display being used needs a link for us to see a data sheet.

1 Like

it has nothing to do with the display or anything else from the designed circuit it's purely about the coding.
Any way. Now it's working thanks to the hint from UKHeliBob

Thank you also for your time and your concern.

  • When you convert to real hardware, we can help if needed.

  • Have fun . . .

2 Likes

Note that the value to be tested starts with 0b which indicates that the value is in binary

1 Like

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