Select a value

Hi, i'm here to know if someone could help me with a little problem i'm having in my program
The problem is that i'm using the SHIFT IN program:

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;
int led1 = 4;


void setup() {
  Serial.begin(9600);  // start serial
  
  pinMode(led1, OUTPUT);
  
  pinMode(latchPin, OUTPUT); // setup pin modes
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
}

void loop()
{
  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin, HIGH);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(1000000); // one second delay
  //set it to 0 to transmit data serially 
  digitalWrite(latchPin, LOW);

  for (int i=0; i<16; i++)
  {
    // read data
    int temp = digitalRead(dataPin);
    
    Serial.print(temp); 
        
    // trigger next input (LOW -> HIGH)
      digitalWrite(clockPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(clockPin, LOW);
      delayMicroseconds(10);
  }

  Serial.println();
  delay(200);
  

  
}

and now i needed to add something to make it that if one button from the sixteen is pressed, for example a LED turn on, something like this: (what i did until now)

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;
int led1 = 4;


void setup() {
  Serial.begin(9600);  // start serial
  
  pinMode(led1, OUTPUT);
  
  pinMode(latchPin, OUTPUT); // setup pin modes
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
}

void loop()
{
  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin, HIGH);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(1000000); // one second delay
  //set it to 0 to transmit data serially 
  digitalWrite(latchPin, LOW);

  for (int i=0; i<16; i++)
  {
    // read data
    int temp = digitalRead(dataPin);
    
      if (temp == 0x02){                         //not sure about the 0x02
      digitalWrite(led1, HIGH);
      delay(100);
      digitalWrite(led1, LOW); 
      }
      if (temp == 0x01){
      digitalWrite(led1, HIGH); 
      delay(1000);
      digitalWrite(led1, LOW); 
      }
    Serial.print(temp); 
        
    // trigger next input (LOW -> HIGH)
      digitalWrite(clockPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(clockPin, LOW);
      delayMicroseconds(10);
  }

  Serial.println();
  delay(200);
  

  
}

Does anyone know where i did it wrong ? or if someone knows something that could help i would appreciate it very much

Can you click Additional Options below, browse to your schematic, and Attach it? I can't open the link you provided.

It sounds like you have 2 shift registers - why not use the shiftin command to bring the 2 bytes in and then do something with them?

void loop(){
digitalWrite (latchPin, HIGH);
digitalWrite (latchPin, LOW); // no delay needed in between
incomingByte1 = shiftIn(dataPin, clockPin,MSBFIRST); // check Reference page to confirm syntax
incomingByte2 = shiftIn(dataPin, clockPin,MSBFIRST); // check Reference page to confirm syntax
Serial.print ("byte 1: ");
Serial.print (incomingByte1, BIN); // leading 0's may not show
Serial.print ("byte 2: ");
Serial.print (incomingByte2, BIN); // leading 0's may not show
delay(1000); // keep from spewing data out too quick
// once see good data, then start doing whatever your decision making is
} // end loop

Here it is and true there are 2 shift registers for 16 buttons.
The program part you sent, what does it do ?

It reads the shift registers and prints the value at the parallel inputs.
You don't say what shift register you are using, some additional control signals may need to be wired in.

A lot of information about it was on the link, sorry :confused:

So you're saying by incorporating this part, the program will give me the value of the 2 shift registers separately , and by doing that i can do if clauses which are going to pick up the two values and check them ?

pasted-graphic-2.jpg

yes.

i can only test it on tuesday because i don't have the hardware with me, but i would like to know just one more thing if possible.
On the circuit i have now, if i'm not wrong there is only one output that goes to the arduino, that takes the information of the 16 buttons, with this change will this need two outputs? following this idea, i tried to put everything together and it said "incomingByte1" and "incomingByte2" are not declared, are these two outputs?

The parts are daisychained, yes? So you use just 1 input still.

"incomingByte1" and "incomingByte2" are just byte variables, declare them as such.

Well i have here the hardware and i tested it to see if it worked, the result is positive but when i put a "1" it shows "10" , this last 0 is always there

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;
int incomingByte1;
int incomingByte2;
int led1 = 2 ;

void setup() {
  Serial.begin(9600);  // start serial
  
  pinMode(led1,OUTPUT);

  pinMode(latchPin, OUTPUT); // setup pin modes
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
}

void loop(){

digitalWrite (latchPin, HIGH);
digitalWrite (latchPin, LOW); // no delay needed in between

incomingByte1 = shiftIn(dataPin, clockPin,MSBFIRST); // check Reference page to confirm syntax
incomingByte2 = shiftIn(dataPin, clockPin,MSBFIRST); // check Reference page to confirm syntax

Serial.print ("byte 1: ");
Serial.print (incomingByte1, BIN); // leading 0's may not show
Serial.println();
Serial.print ("byte 2: ");
Serial.print (incomingByte2, BIN);
Serial.println ();// leading 0's may not show

delay(1000); // keep from spewing data out too quick

      if (incomingByte1 == 10){                         //tried to put 1 or 10 because of the extra 0
      digitalWrite(led1, HIGH);
      delay(1000);
      }

} // end loop

P.S. i fixed the if clause " if (incomingByte1 == B10){ " but still there is a 0 too much

Well after a little bit of thinking this solution is already very very good, there is that 0 too much fact but i can get around it, if you know the answer i will thank you very much if you would share it with me, if you don't know, don't worry 8)
Thank you very much for your help in this project, you helped me a lot by helping with this problem :slight_smile: