Making a Value from an Array ???

Hi all,
I am a new Arduino IDE user.
So, this is day 3 and starting to get some feel of the language, but I am stuck and need help.

I am reading the value of an input pin.
I am putting that value into an array from a For loop…Lets say x[1 to 15].
Now when I am done with the For Loop, I would like to build this data into
a single integer value.

So, say these are the values of the array 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1
how do I shift those values into a single long that reads 00000000 00000011 ???

I tried using an intermediary
unsigned int x = (unsigned int) y >>1;
z=(unsigned int)y<<1
But I get nothing as far as I can see trying to form a single value. It always stays value of 0.
I read values from array and send them to the display and they are active, but I do not know how to form a single value.

Thanks for any help here.

You will be adding a bitwise or to your code.

I am putting that value into an array from a For loop....Lets say x[1 to 15].

Why? Set the value in the variable, directly, using bitWrite().

bool myArray[15];
unsigned int myVar = 0;

for(byte i = 0; i < 15; i++){
  myVar <<= 1;
  myVar |= myArray[i]; //or 
  //myVar |= myArray[i] & 0x01;  //it myArray isn't a bool
}

@septillion: Isn’t the input string more than 15 chars or does the array already exclude the commas? If it does exclude the commas, shouldn’t the loop be over 16 bits? Would it make sense to use:

for(byte i = 0; i < sizeof(unsigned int); i++){

Of course I’m not sure because there is no code. But I don’t think there is a string or chars. Just a array with 15 members.

Chuck0:
Lets say x[1 to 15].

The size of would be great to use here indeed but I wasn’t sure what would return as size from a bool :roll_eyes: Apparently the compiler just uses a byte for each bool…

PaulS:
Why? Set the value in the variable, directly, using bitWrite().

Paul S., THANK YOU. That is EXACTLY what I needed… It all works now !
I appreciate your help !

for (int bitToRead=0; bitToRead<=15;bitToRead++) {
bitWrite(tc,bitToRead,tcArray[bitToRead]);
}

Awesome !!!

( for the others that are all bent out of shape because I did not use code tags, sorry ! That is because
I did not include any code. Maybe you should go home and beat your dog)

Chuck0:
( for the others that are all bent out of shape because I did not use code tags, sorry ! That is because
I did not include any code. Maybe you should go home and beat your dog)

karma++

:slight_smile:

You not using code tags was fine because you didn't post code :wink:

And I would never beat a dog! Although I'm more of a cat person I like to pet a dog though.

Chuck0:
Maybe you should go home and beat your dog

I have nursed such animals back to health. For suggesting something so offensive you are banned. Good riddance.

Mm, I'm still hoping/thinking petting was just lost in translation...

Translation from "Californian"? That's a stretch.

OP might not be a native Californian, but that doesn't excuse his/her behavior.

This is a bad reason for banning someone.

The OP was not suggesting that the person beat their dog, he was saying that in giving him a rough time for not using code tags when he hadn't posted any code, they were treating him like an animal abuser treats a dog.

A poor and overblown analogy, agreed. But a ban was not appropriate.

...giving him a rough time for not using code tags...

At what point did that happen?

But a ban was not appropriate.

I disagree. There are two possible interpretations for what he wrote. One is highly offensive to me (et al). The other is highly offensive to the phantom that "gave him a rough time". Either way, what he wrote is highly offensive.

If @Chuck0 believes I am being unjust there is a mechanism by which he can be reinstated. Until he rears his ugly head the matter is closed.