declaring multiple inputs in a scope

int switchState = 0;
void setup(){
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
}
void loop(){ 
  button1 = digitalRead(5);
  button2 = digitalRead(6);
  if(button1 == HIGH) {
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  }
  else { 
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  } 
  if(button2 == HIGH) {
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
  }
  else {
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
    delay(250);
  }
}

it says their is an issue with button1 and button2 how do i fix this issue.

int button1 = digitalRead(5);
int button2 = digitalRead(6);

Or declare them at global scope like you did with switchState.

Or, to save RAM space:-

bool button1 = digitalRead(5);
bool button2 = digitalRead(6);

OldSteve:
Or, to save RAM space:-

bool button1 = digitalRead(5);

bool button2 = digitalRead(6);

Try it and see.

Hint: bool is 16 bits.

To save space use boolean which is custom Arduino and 8 bits or use byte.

Delta_G:
Try it and see.
Hint: bool is 16 bits.
To save space use boolean which is custom Arduino and 8 bits or use byte.

My bad - I though bool was boolean. I almost typed boolean, but 'bool' was faster.
I'd better stop using 'bool'. :slight_smile:

OldSteve:
My bad - I though bool was boolean. I almost typed boolean, but 'bool' was faster.
I'd better stop using 'bool'. :slight_smile:

You'll never see anyone who knows using bool on an Arduino just for that reason. boolean is basically uint8_t via either a #define or a typedef somewhere.

Delta_G:
You'll never see anyone who knows using bool on an Arduino just for that reason.

Neither will I from now on. :slight_smile:
I always used boolean until a couple of weeks ago, then started using 'bool' once I saw Arduino accepted it. Bad move.

boolean is basically uint8_t via either a #define or a typedef somewhere.

Yep - I just read that it's a byte-sized variable here:-
boolean

Delta_G:
Hint: bool is 16 bits.

[citation needed]

oqibidipo:
[citation needed]

Load up some code and try it. That's what I did before I posted to make sure I was right. Gimme a few and I'll rewrite it and post it.

My Micro and Due say sizeof(bool) == 1.

#include <limits.h>

#define DEBUG_VALUE(x) do { Serial.print(#x " == "); Serial.println(x); } while (0)
#define SIZEOF(x) DEBUG_VALUE(sizeof(x))

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println(F("Sketch:   " __FILE__ "\r\n"
                   "Compiled: " __DATE__ " " __TIME__ "\r\n"));

  DEBUG_VALUE(CHAR_BIT);
  SIZEOF(bool);
  SIZEOF(true);
  SIZEOF(false);
  SIZEOF(byte);
  SIZEOF(int);
  SIZEOF(long);
  SIZEOF(long long);
  SIZEOF(float);
  SIZEOF(double);
  SIZEOF(long double);
}

void loop() {

}

Well, when I changed the int to bool the code size stayed the same. But sizeof bool returns 1. So now I'm not sure again.

Looks like we were working on that at the same time.

So, can I now go back to using bool, assuming as I already did that a bool is bitbyte-sized? (Corrected typing error)
This is all very confusing. Before today, I thought I had bool and boolean figured out.

My UNO reports both sizeof(bool) and sizeof(boolean) as 1 byte
So back to bool...
Deja-vu. :slight_smile:

I just use byte.
Example:

// declare at top of sketch
byte bool_flag = 0;

// use in loop
if (boot_flag == 0){
Serial.print ("flag was low");
bool_flag = 1;
} 
else {
Serial.print ("flag was high");
bool_flag = 0;
}

Can use each bit individually too if you want.

if ((bool_flag & 0b00000100) == 0b00000100){
Serial.print ("bit 2 of flag is high");
bool_flag = bool_flag & 0b11111011; // clear bit 2
}
else {
Serial.print ("bit 2 of flag is low");
bool_flag = bool_flag } 0b00000100; // set bit 2
}

I often use that to set & clear D10 for direct port manipulation of PORTB (on '328P) for fast SPI transfers

PORTB = PORTB & 0b11111011; // clear D10
SPI.transfer(dataByte);
PORTB = PORTB | 0b00000100; // set D10

CrossRoads:
I often use that to set & clear D10 for direct port manipulation of PORTB (on '328P) for fast SPI transfers

PORTB = PORTB & 0b11111011; // clear D10

SPI.transfer(dataByte);
PORTB = PORTB | 0b00000100; // set D10

Yep. I just used the short form of that to bit-bang a 38kHz IR carrier. (ATtiny85):-

      // Generate one cycle of 38kHz at 27% duty-cycle:-
        PORTB |= 0b000010;                  // Set signal pin
        delayMicroseconds(7);               // high for 7 uS.
        PORTB &= 0b111101;                  // Clear signal pin
        delayMicroseconds(17);              // low for 17uS.
        // Additional 800nS delay for fine-tuning to 38kHz:-
        __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n\tnop\n");

Well there's gotta be more to the story because if you look at the code size and not the dynamic memory usage things are a little different.

This code:

int buttonState1;
int buttonState2;

void setup() {
  Serial.begin(19200);
  delay(1000);
  
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  
  // Print so the compiler doesn't throw away my variables
  Serial.println(buttonState1);
  Serial.println(buttonState2);
}

void loop() {}
Sketch uses 2,690 bytes (8%) of program storage space. Maximum is 32,256 bytes.
Global variables use 186 bytes (9%) of dynamic memory, leaving 1,862 bytes for local variables. Maximum is 2,048 bytes.
bool buttonState1;
bool buttonState2;

void setup() {
  Serial.begin(19200);
  delay(1000);
  
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  
  // Print so the compiler doesn't throw away my variables
  Serial.println(buttonState1);
  Serial.println(buttonState2);
}

void loop() {}
Sketch uses 2,694 bytes (8%) of program storage space. Maximum is 32,256 bytes.
Global variables use 184 bytes (8%) of dynamic memory, leaving 1,864 bytes for local variables. Maximum is 2,048 bytes.
boolean buttonState1;
boolean buttonState2;

void setup() {
  Serial.begin(19200);
  delay(1000);
  
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  
  // Print so the compiler doesn't throw away my variables
  Serial.println(buttonState1);
  Serial.println(buttonState2);
}

void loop() {}
Sketch uses 2,550 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 184 bytes (8%) of dynamic memory, leaving 1,864 bytes for local variables. Maximum is 2,048 bytes.

So bool and boolean both reduce the dynamic memory usage by 2, obviously going from 2 bytes to 1.

But bool made the code size go UP by 4 bytes and boolean resulted in a significant reduction in code size over bool dropping the code size by 144 bytes over the code using bool.

So I wonder where those savings are coming from.

And I wonder what's up with sizeof()?
It's lying to us.

Back to boolean it is. :slight_smile:
My head's spinning.

It appears that its in the print class. bool must be being promoted to int before anything gets done with it while boolean remains uint8_t and calls the smaller print code.

uint8_t buttonState1;
uint8_t buttonState2;

void setup() {
  Serial.begin(19200);
  delay(1000);
  
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  
  // Print so the compiler doesn't throw away my variables
  Serial.println(buttonState1);
  Serial.println(buttonState2);
}

void loop() {}
Sketch uses 2,550 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 184 bytes (8%) of dynamic memory, leaving 1,864 bytes for local variables. Maximum is 2,048 bytes.

Same as the boolean code

OldSteve:
And I wonder what's up with sizeof()?
It's lying to us.

No, sizeof is working. It's pulling the right size for the thing that's stored in memory. It the other code that uses it (Print in this case) that is bigger.

Delta_G:
No, sizeof is working. It's pulling the right size for the thing that's stored in memory. It the other code that uses it (Print in this case) that is bigger.

Yep. Got it. I read your post right after I posted mine.

So we shouldn't print our bools.
I'll just stick with boolean now and be done with it.
An interesting exercise, though, and we've learned something. :slight_smile: