help with arrays

hi everyone,i have a small problem.in my codes,i want to use array that have only 1 or zero but i assigned them as int.so,when i use array,the variables that filled with those array unit will be byte but either 1 or zero.i only need 1 or zero for my purpose so is this true??:

int sega = {1,0,1,1,0,1,1,1,1,1}; //this is my array
q1=sega[s1]; //s1 is a number between 0 and 9
digitalWrite(0,q1); //i want to write on the pin0

are they true?or i must use bool arrays instead of int??

Can you post your actual code?

Pin zero is used for the serial interface.
Do you think that is wise?

TolpuddleSartre:
Can you post your actual code?

Pin zero is used for the serial interface.
Do you think that is wise?

hi,i don`t need pin0 at all.my actual question is:can we for example write this code:
digitalWrite(pin,variable)//variable can be only a bit.1 or zero

have a look at digitalwrite documentation

You, you can. But why make an array of int's when you want that? :wink:

Also two tips

  • Spacesarefreebutmakecodemorereadable :wink:
  • Use proper variable name, don't try to short it because it's quick to type. Future you will thank you :slight_smile:

when i use array,the variables that filled with those array unit will be byte but either 1 or zero.i only need 1 or zero for my purpose so is this true?]

What a weird question!

If you make an array if int and put values in it, then the values in it will be whatever you put there. If you only put ones and zeros in it, then that's what will be there. If you put something else there, then it will contain something else.

If you want an array of boolean true and false, then you can make a boolean array. If you want an array of HIGH and LOW, then you probably want a byte array, because digitalWrite is defined to take byte as an argument.

PaulMurrayCbr:
...because digitalWrite is defined to take byte as an argument.

but C++'s implicit coercion does allow for other types... and can be useful.

OP should (I agree with you @PaulMurrayCbr) stick to the smallest data type possible if space is an issue (when isn't it?).

Simple question deserves simple answer and not a sermon.
Where are you guys getting such picky attitude?
And some of you even do not read the question - where did the pin 0 came from ?

are they true?or i must use bool arrays instead of int??

You MUST NOT use bool type…

  1. bool is not standard C/C++ type
  2. HIGH or LOW is also not C/C++ standard
    3, You should always use smallest size of variable type to accomplish the task
  3. Modern compilers let you specify the int size / type - such as uint8_t

mrr1995:
hi everyone,i have a small problem.in my codes,i want to use array that have only 1 or zero but i assigned them as int.so,when i use array,the variables that filled with those array unit will be byte but either 1 or zero.i only need 1 or zero for my purpose so is this true??:

int sega = {1,0,1,1,0,1,1,1,1,1}; //this is my array
q1=sega[s1]; //s1 is a number between 0 and 9
digitalWrite(0,q1); //i want to write on the pin0

are they true?or i must use bool arrays instead of int??

With number values, zero is false and not-zero is true. You can use the ! operator to flip that if need be.
edit: example below

Why use a 16-bit int where and 8-bit byte (yes, byte is an Arduino variable type) will do?
Arduino type bool is a byte that can ONLY be 0 or 1.

Then at least you only waste 7 bits instead of 15.
All variables are arrays of bits that you can operate on as individuals or as a group.
You can use the Arduino bit command words, the bitRead(x,n) pages has links to the others, like bitWrite(x,n);
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitread/

edit --- working example of using the ! operator is posted below.

232:
Simple question deserves simple answer and not a sermon.
Where are you guys getting such picky attitude?
And some of you even do not read the question - where did the pin 0 came from ?

are they true?or i must use bool arrays instead of int??

You MUST NOT use bool type...

  1. bool is not standard C/C++ type
  2. HIGH or LOW is also not C/C++ standard
    3, You should always use smallest size of variable type to accomplish the task
  3. Modern compilers let you specify the int size / type - such as uint8_t

You're new to Arduino, aren't you?

232:
Where are you guys getting such picky attitude?
And some of you even do not read the question - where did the pin 0 came from ?

mrr1995:
digitalWrite(0,q1); //i want to write on the pin0

You're new to reading too, I guess, @232.

8-bit MCU code is hardware-specific so often at different levels that portability to PC code is not possible. You can read the logic and use it elsewhere but I sure as hell don’t worry about porting automation code to a system that can’t use it.

Our most-used ‘computer’ is the Uno with 32K flash, 2K RAM and 1K EEPROM. Believing that String use is okay has brought many new-ish members to the forum wondering why their bigger, this time for real project crashes and the answer turns out to be what their Strings do to the < 2K heap.
But but but String is STANDARD and PORTABLE! Not really, it ports like crap to small environments like the Uno.

Respect the hardware or you won’t get far. Arduino is close to the metal C, you can grab some direct for sure but that should not drive any part of Arduino programming unless it is about limiting to portable code what you do.

Please, if that’s your choice then I’ll respect that but don’t tell new people they should stick to Standard C when Arduino is not.

GoForSmoke:
edit --- pseudocode example of using the ! operator

byte x = 7;

if ( x == true ) print( "true" ); // would print "true" since x is not 0 and 0 is false
else print( "false" ); // would NOT print "false"

x = !x; // use of the ! operator, the logical NOT, it makes not-0 into 0 or makes 0 into 1

if ( x == true ) print( "true" ); // would NOT print "true"
else print( "false" ); // would print "false" since x is 0 and 0 is false

Nonsense - both print "false".

Right. This works, tested. True is not false.

void setup() 
{
  Serial.begin( 115200 );

  static byte x = 7;

  Serial.print( x );
  Serial.print( "  " );
  if ( x != false )    Serial.println( "true" );
  else                 Serial.println( "false" );

  x = !x;

  Serial.print( x );
  Serial.print( "  " );
  if ( x != false )    Serial.println( "true" );
  else                 Serial.println( "false" );
}

void loop() {
  // put your main code here, to run repeatedly:

}

can we for example write this code:
digitalWrite(pin,variable)//variable can be only a bit.1 or zero

To answer your specific question, no, you cannot write that and have the variable be only a bit.

You could, however, hold the zeroes and ones as bits of a variable, read a specific bit from that variable and use that as the second parameter in digitalWrite()

digitalWrite(pin, bitRead(variable, bitNumber));

UKHeliBob:
To answer your specific question, no, you cannot write that and have the variable be only a bit.

You could, however, hold the zeroes and ones as bits of a variable, read a specific bit from that variable and use that as the second parameter in digitalWrite()

digitalWrite(pin, bitRead(variable, bitNumber));

You could hold the variable as a bit field in a struct.

232:
You MUST NOT use bool type…

  1. bool is not standard C/C++ type

You are misinformed. bool is a standard C/C++ type. (In C you have to #include <stdbool.h>, but the type is available as _Bool even if stdbool.h is not included. In C++ it’s not necessary to include stdbool.h.)

232:
2. HIGH or LOW is also not C/C++ standard

No, but HIGH and LOW are defined in the Arduino environment, so they’re perfectly valid to use in an Arduino program.

With this old hardware (32-bit PC) running legacy software on an obsolete OS I boot Linux to get online so,

please bear with unchecked work here, I already had to change
if ( x == true )
to
if ( x != false )
but rebootng twice to make sure is a real pain.

Suppose I have 12 true or false choices and I store them in bit 0 to bit 11 of an unsigned int variable.
Arduino unsigned int is named “word”, and unsigned char is named “byte”.

word myBits = 0; // 0 is the default, but I show it here for the humans to be sure.

… and somewhere in the code that initial 0 changes as bits change – see the bitWrite() function.
… and then somewhere I want to show what the bit values are…

for ( byte i = 0; i < 12; i++ ) // justy showing HOW to get to the bits using Arduino bitRead() function.
{
Serial.print( "Bit " );
Serial.print( i );
Serial.print( " is " );
if ( bitRead( myBits, i )) // get the true or false from the bit
{
Serial.println( “1” );
}
else
{
Serial.println( “0” );
}
}

C provides functions to deal with all those bits as a group. But heck, if any bits are not zero then the variable holding them all is also not zero. You can turn the nature of variables to your advantage once you get the commands down and save many lines of code.

christop:
No, but HIGH and LOW are defined in the Arduino environment, so they're perfectly valid to use in an Arduino program.

[hardware idiocy: ON]
But 3-state logic will be coming out any month now and we want to be compatible with the future!
[hardware idiocy: OFF]