Go Down

Topic: Comparing ShiftIn Values (Read 550 times) previous topic - next topic

Schwabinger

Hi,

I managed  successfully to built a shift in register with 8 push buttons.

The state of the buttons goes into a variable switchVar1:

Code: [Select]
byte switchVar1 = 72;  //01001000
..
switchVar1 = shiftIn(dataPin, clockPin);


I can show the value of the pressed button doing the following:

Code: [Select]
  Serial.println(switchVar1, BIN);



This results in an output between 1 and 10000000.

How can I use the variable switchVar1 in an If- or case-statement?

Thanks in advance,

Schwabinger





PaulS

Quote
How can I use the variable switchVar1 in an If- or case-statement?

Just like any other variable. The source of the value stored in the variable is completely irrelevant.

Or, perhaps I don't understand your question, or you are not asking the right question.

Schwabinger

ok, let me be more precise:

If I do understand correctly, pressing the first button sends a 00000001 to the variable.
Pressing the second sends a 00000010, the third a 00000100 and so on.

I try to evaluate that using the following code:


Code: [Select]

  if (switchVar1 == 00000001){
      Serial.println("treffer: 1");
   
  }
 
if (switchVar1 == 0000010){
      Serial.println("treffer: 2");
   
  }
 
   
if (switchVar1 == 0000100){
      Serial.println("treffer: 3");

if (switchVar1 == 00001000){
      Serial.println("treffer: 4");
   
  }

if (switchVar1 == 00010000){
      Serial.println("treffer: 5");
   
  }
  if (switchVar1 == 00100000){
      Serial.println("treffer: 6");
   
  }
  if (switchVar1 == 01000000){
      Serial.println("treffer: 7");
   
  }

  if (switchVar1 == 10000000){
      Serial.println("treffer: 8");
   
  }



So when I press button 1, i get the result

"treffer 1" and the if-statement does what I expect.

But pressing the second, third ... button, never leads to a result like "treffer 2" or "treffer3".

Any ideas?

Schwabinger








robtillaart

Your numbers look binary but they are just integers.

Code: [Select]

  if (switchVar1 == [color=red]B[/color]00000001)
{
      Serial.println("treffer: 1");
  }
 
if (switchVar1 == [color=red]B[/color]0000010){
      Serial.println("treffer: 2");
  }
 
etc


If more than one button is pressed you even need to do som bitmagic called masking the bit.

Code: [Select]

if ((switchVar1 & B00000010) == B0000010)
{
  Serial.println("treffer: 2");
}

or in hex
if ((switchVar1 & 0x01) == 0x01)
{
  Serial.println("treffer: 2");
}
etc


Or better use a for loop to check all eight with the bitRead() function

Code: [Select]

for (int t=1; t<9; t++) 
{
  if ( bitRead(switchVar1, t) == 1)
  {
    Serial.print("treffer: ");
    Serial.println(t);
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Schwabinger

Hi Robtillaart,

thanks for your help. I did not know about the B for "binary" ....

Now my SlugRadio has eight more station buttons.

Greetings,

Schwabinger

Go Up