İncomig binary conver decimal

In short, I want to do something like this. I'm using 4 74hc165s that will follow the fabric and say the fabric is sliding left and right, it will write to the LCD which direction it is sliding. There are 32 inputs in total and each input has a receiver opposite an IR transmitter.

So ones increase the number, zeroes decrease the number.

This does not jibe with the first part.  Assuming eight bits, six up counts and two down counts results in four.  Explain.

I'm guessing you want a count of how many LEDs are blocked?

Then I would show the "width" and the "position".
For example the width is 30 leds and the position is -2 (because there are 10 on one side and 8 on the other side).

I'm guessing you want a count of how many LEDs are blocked?

Yes...

I really thought I understood it when I saw the photo and you mentioned the sliding to the left and the right. So I thought you wanted to know the "width" of the fabric and "position" to check if it is in the middle.
But now it is counting the number of leds that are blocked, so I'm totally clueless again.

Counting binary 0 and 1 and checking for consecutive inputs for the fabric (there could be holes in the fabric), finding the position and all those things with a binary number is easy, but I can not put in code what I don't understand.

Start counting 0s from one end, if you hit a 1, save the number of 0s, then start a new count of 0s from the other end, stop when you hit a 1 and compare the 2 counts. If different by a certain amount, adjust position.

I think that I know what the miscommunication is, I was not talking in code but about a broader view what the project is about. I don't care about the actual implementation at this moment, so I don't care if inputs have to be counted to find the fabric.

Let's try it in a different way. It is a "line scanner". Suppose it scans red fabric.

Good:
afbeelding

Too much to the left:
afbeelding

Too much to the right:
afbeelding

The fabric has been ripped apart:
afbeelding

The fabric is too small:
afbeelding

The fabric is too wide:
afbeelding

Then I would like to see on a display how many centimeters the fabric is too much to the left or to the right.

that's right, i want to do this

that's right, I want to do this with mutual infrared receiver and transmitter, I want to track the position of the fabric between infrared receiver and transmitter

#include <Wire.h> 
//#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
//LiquidCrystal_I2C lcd(0x20,20,4);

#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);


#define SO         5    // (Pin 9) 
#define SH_LD   6    // (Pin 1) 
#define CLK       7     // (Pin 2) 


unsigned int i; 
unsigned int PinState = 0; 
unsigned int Parallel_data = 0;
int scanspeed = 100;
int rsetup =  0;
int lsetup =  0 ;
int target =  0 ;
int infrared= 64 ;
int incomingir = 0;

void setup() 
{

  Serial.begin(115200);
  pinMode(SH_LD, OUTPUT);
  pinMode(CLK, OUTPUT);  
  pinMode(SO, INPUT);

  lcd.begin(20, 4);
  lcd.clear();

    
}

void loop()
{


   
  digitalWrite(CLK, LOW);
  digitalWrite(SH_LD, LOW); 
  delay(5);
  digitalWrite(SH_LD, HIGH);

  
  for(i=0;i<infrared;i++) // 15 panel için 240 bit değeri girilecek
    {

        PinState = digitalRead(SO);
        digitalWrite(CLK, LOW);
        delay(1);
        digitalWrite(CLK, HIGH);
        delay(1);
        Serial.print(PinState);
        Parallel_data = (Parallel_data<<1)|PinState; 



    }
        Serial.println(); 
        delay(scanspeed);
        
  
  lcd.setCursor(3,0);
  lcd.print("TEST");
  lcd.setCursor(0,1);
  lcd.print("LEFT :");
  lcd.print(lsetup); // Write to eeprom
  lcd.setCursor(10,1);
  lcd.print("RIGH :");
  lcd.print(rsetup);  // Write to eeprom
  lcd.setCursor(0,2);
  lcd.print("TARGET : ");
  lcd.print(target);  // Write to eeprom
  lcd.setCursor(0,3);
  lcd.print("IN Data From Ir : ");
  lcd.print(incomingir);     // Write to eeprom
  lcd.clear();
  delay(250);



}

I want to track only position, not big or small size

That code looks like you're pretty much there - just need to grab the left and right edges as you read.

I couldn't figure out how to do this, I tried several methods. If I can get the incoming data between 0-254, I can achieve this, for example, if the value is 0, -10 shifted to the right, +10 shifted to the left.... because I need to save the incoming data to the eeprom, I will save the eeprom after the 0 point is determined, fabric It will give information on the LCD screen when it slides to the right or left from the 0 point.

This is probably close, although I didn't test it:

int FirstOne=-1;
int LastOne;
  
  digitalWrite(CLK, LOW);
  digitalWrite(SH_LD, LOW);
  delay(5);
  digitalWrite(SH_LD, HIGH);
  for (int i = 0; i < infrared; i++) // 15 panel için 240 bit değeri girilecek
  {
    PinState = digitalRead(SO);
    digitalWrite(CLK, LOW);
    delay(1);
    digitalWrite(CLK, HIGH);
    delay(1);
    Serial.print(PinState);
    if(PinState==HIGH && FirstOne==-1)
      {
      FirstOne=i;  
      }
    if(PinState==HIGH)
      {
      LastOne=i;  
      }
    Parallel_data = (Parallel_data << 1) | PinState;
  }
  LeftMargin=FirstOne;
  RightMargin = infrared-LastOne-1;
1 Like

thanks, i will try

For 6 parallel shift registers just read 6 bytes using the SPI library
it's way faster than trying to bit-bang a clock
Each byte correspond to a shift egister (the closest being the first byte, the last being the furthest)
Do this:

SPI.beginTransaction(spiSet2);
    buttonsreg = SPI.transfer(0x00);
    buttonsreg1 = SPI.transfer(0x00);
    buttonsreg2 = SPI.transfer(0x00);
    SPI.endTransaction();
1 Like

how do i do this? I don't know about 74hc165

wire it as you shown. Data out to MOSI, clock to sck. power and ground respectively.
Daisy chain them (so the next shift register's data out goes to the first register's data in).
Reserve another GPIO for switching between parallel load/serial shift. You cannot shift the bits when parallel loading and vise versa.
165 have a clock enable you can connect it to any regular pin and pull it high (or low) to enable or disable bit shifting

I believe it is said that the 165D (at least the HC version) take up to 26MHz. Currently I use a 8MHz setting
SPISettings spiSet2 = SPISettings(8000000UL, MSBFIRST, SPI_MODE0);
I guess you can change it to LSBFIRST (least significant bit first), depend on which way you want the incoming stream to go.

Then, as you get the 6 bits of the data, you can directly measure their value to determine if any data has been received by a given bank
Then you can extract the bits

switch (pin) {
case 1:
return buttonsreg & 0b10000000;
case 2:
return buttonsreg & 0b01000000;
case 3:
return buttonsreg & 0b00100000;

case 4:
return buttonsreg & 0b00010000;

case 5:
return buttonsreg & 0b00001000;
case 6:
return buttonsreg & 0b00000100;
case 7:
return buttonsreg & 0b00000010;
case 8:
return buttonsreg & 0b00000001;
}

Remember that the 165D don't have pullup/down resistors on their inputs, so you will need to supply your own.
These all come from my XpandShield2 project's library. It use 3 74HC165Ds to read the state of 24 buttons 60 times a second

1 Like

At each 8bit register, as the bits are shifted out, find the difference of 1s and 0s. Send that value to be counted. This lowers the "counting" by 8 x # of registers.