Pages: [1]   Go Down
Author Topic: Comparing ShiftIn Values  (Read 520 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
byte switchVar1 = 72;  //01001000
..
switchVar1 = shiftIn(dataPin, clockPin);

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

Code:
  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




Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

  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







Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your numbers look binary but they are just integers.

Code:
  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:
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:
for (int t=1; t<9; t++) 
{
  if ( bitRead(switchVar1, t) == 1)
  {
    Serial.print("treffer: ");
    Serial.println(t);
  }
}
Logged

Rob Tillaart

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: