Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: possible to define variable as bit?  (Read 659 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd like to switch between 2 states each time a function runs. The best way I can think of doing this is using a bit variable and incrementing it at the end of the function, but it seems one can't define a bit variable in arduino like in C.

Whats the next cleanest way to do this? Incrementing if its 0 and decrementing if its 1 seems pretty cumbersome.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 216
Posts: 13140
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void FlippyFunction( void )
{
  static boolean state = false;  // or "= true;"

  if ( state )
  {
    // Your code goes here
  }
  else
  {
    // Your other code goes here
  }
  state = ! state;
}
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 555
Posts: 27684
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

similarly,

byte toggle = 0;

then
toggle = 1-toggle; // results in 1-0-1-0-1-0...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

neither worked, I'm not sure what exactly is going on but state or toggle were not changing from the value one using serial.println.

I ended up continuously incrementing an unsigned int and using modulus : if (state % 2); state++;
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'd like to switch between 2 states each time a function runs
You are using static or global memory, aren't you?

Quote
The best way I can think of doing this is using a bit variable
The only way of getting a single bit is to use bit fields in a struct. If you've only got one such variable, you may as well use a whole byte.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
neither worked, I'm not sure what exactly is going on
Neither are we, unless you post some code.
Logged

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

Quote
I ended up continuously incrementing an unsigned int and using modulus : if (state % 2); state++;

try: byte state = (state+1) % 2 ;   // 0 <-> 1

Real bitfields are only possible in structs
struct
{
  byte b:1;
} x;

this defines the var  x.b which is 1 bit in size but still uses 8 bits in memory. Advantage is that if you need more you can do:

struct
{
  byte a:1;
  byte b:1;
  byte c:1;
} x;

which still uses 1 byte until the sum of the sizes exceeds 8, then it will allocate 2 bytes

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: