readBit() Not understood

Hello everyone

I'm trying to figure out how bitRead works.

I will be receiving throw serial something that looks like this: 100110
1 means that the LED should be on and 0 off. I have 6 leds therefor 6 digits.

The monitor gives me 1 and 0 for the bitRead() I can't understand it.
Best regards
Morgan

byte led=111111;
    for(int i=1;i<=6;i++)
    {
       int temp = bitRead(led,i);

       Serial.println(temp);
         if(temp==1 && i==1)  digitalWrite(LightRight3, HIGH);
         if(temp==0 && i==1)  digitalWrite(LightRight3, LOW);
         if(temp==1 && i==2)  digitalWrite(LightLeft3, HIGH);
         if(temp==0 && i==2)  digitalWrite(LightLeft3, LOW);
         if(temp==1 && i==3)  digitalWrite(LightRight2, HIGH);
         if(temp==0 && i==3)  digitalWrite(LightRight2, LOW);
         if(temp==1 && i==4)  digitalWrite(LightLeft2, HIGH);
         if(temp==0 && i==4)  digitalWrite(LightLeft2, LOW);
         if(temp==1 && i==5)  digitalWrite(LightRight1, HIGH);
         if(temp==0 && i==5)  digitalWrite(LightRight1, LOW);
         if(temp==1 && i==6)  digitalWrite(LightLeft1, HIGH);
         if(temp==0 && i==6)  digitalWrite(LightLeft1, LOW);
}

If i put in led=111

I have LightRight3 LightLeft3 LightLeft2 LightRight1 LightLeft2 on how is this possible?

Thank you

byte is an 8-bit integer. 11111110 (decimal) is 110110010000001112 (binary). The top bunch will be cut off because it doesn't fit in 8 bits, so your number is now just 1112 or 710. If you wanted led to be 111111 in binary, you can use hex notation: byte led = 0x3f;

Aside from that, your code looks pretty much correct, although could be a lot shorter and easier to understand if you used an array for the pin numbers.

Your setting a byte to 111 where you should be using 0b111 for binary. Also, bit numbers start from 0 not 1 so you never read the least significant bit. The result returned is the state of the requested bit so it will return 1 or 0 depending if the bit was set (1) or clear (0).
Your only supplying a code snippet but from what I can see you could vastly simplify your code by using an array to store LED pin numbers. You could also use if..else as the bit states are either on or off

         if(temp==1 && i==1)  digitalWrite(LightRight3, HIGH);
         if(temp==0 && i==1)  digitalWrite(LightRight3, LOW);
if(temp==1 && i==1) {
  digitalWrite(LightRight3, HIGH);
}
else {
  digitalWrite(LightRight3, LOW);
}

Thank you for your fast replies both of you.

I did put 111 to test I will be receiving data that looks like this 101010 using led = Serial.read();
Will I be able to converte it as binary like you said?
Should I still declare led as Byte?

I could use and array list, i will do so as soon as it works.
With 0x3f i get all the Leds on except for the LightLeft1.
Best regards

 for(int i=0;i<5;i++)

For six LEDs

Should I still declare led as Byte?

Yes

I will be receiving data that looks like this 101010 using led = Serial.read();

What is sending that data? You REALLY need to be clear on what you are expecting. If the data is being sent by a computer, as binary data, the computer will be sending 0x101010. If you are typing the string "101010" into the serial monitor, the you will be reading ONE of those characters per call to Serial.read(), so bitRead() doesn't come into play at all.

Thank you,

I will try to be clearer,

I have a program i've coded with processing running on my mac.
This programme will be sending data using

myPort.write('L');              
myPort.write(1);

I can either send it as an int or a string

myPort.write('101001');              
myPort.write(101001);

If i understand your answer i shoudn't use the string.

Thank you

Both those last two are wrong.
It is not just over one hundred thousand is it?
The single quotes are a char not a string.

At maximum I will have 6 leds on so 111111.
Yes you are right ' ' are for char, i'm not sure I can send string, and i don't think it will be a good idea anyways as it is harder to handle with arduino for what i want to do.

Thank you

and i don't think it will be a good idea anyways as it is harder to handle with arduino for what i want to do.

No, it isn't. It simply takes some thought.

Ok but what solution do you recommend then?
I have to say i'm a little confused.

So far I understood that if i send data from my Java to my arduino using

myPort.write(101001);

I Will need to receive the information in HEX ( this is still confused).

Then my code should work.

Thank you guys for your help this is my first arduino code and the first time I use Serial

So far I understood that if i send data from my Java to my arduino using...I Will need to receive the information in HEX

I don't know why you understand that. Perhaps you should simply try using that write() call, and see what the Arduino gets.

I predict that myPort.write(101001); will cause the Arduino to receive '1', '0', '1', '0', '0', and '1'.

Of course, you will have trouble seeing where a packet starts and ends, so perhaps you need to send more data. Like:

myPort.write("<");
myPort.write(101001);
myPort.write(">");

Then, you'd KNOW where a packet starts and ends.

You can, of course, echo what you receive, and have the sender read the serial port, and show what it gets back.

ok thank you,
how come there is a difference between if i use led=serial.read() and if for my tests i write led=111111, this is what blocks me from understanding I guess.

Thank you

how come there is a difference between if i use led=serial.read() and if for my tests i write led=111111, this is what blocks me from understanding I guess.

Because 111111 is an integer value. What is sent over the serial port is a string of characters. There is a big difference between reading a string of characters, one at a time, and having an int.

Hello every one,

Allow me to get back to you.
yesterday I have been working on my processing witch is now working perfectly.

I'n order to test with my arduino and show you simply what i'm sending i've created this portion of code:

byte temp=000000;
temp=(byte)(temp|(byte)12);
println("temp:" +binary(temp));
myPort.write("<");
myPort.write(binary(temp));
myPort.write(">");

I'm my Processing console i'm getting the wanted output: temp: 00001100.
Meaning i want my 5th and 6th LED on.

On the arduino side

This is where i'm having issues, at the moment i'm using an ArduinoMega 32 board with the version 0022 of the arduino develloping tool. I should be receiving my duemilanove tomorrow.

I can't seem to be able to use String: ( "String does not name a type").

The output in the monitor is something like this:

00011000000

1
0000001
00
000

10

0001100000010000001100
0
0
0
0

000

The possition of the one's and zero's is no corresponding to what i'm expecting.

I'm using this:

byte in_byte = Serial.read();

 Serial.println(in_byte);

How do I use the ">" that you recommended earlier?

Thank you very much, best regards

I'm trying to get my 00001100 on the arduino part,

I have tried that:

  if(Serial.available()) // -1 if not data throw Serial
  {
    //char in_byte = Serial.read();
    if(Serial.read()=='<')
    {
     count=0;
     Serial.println("hey");
    while(Serial.read() != '>')
    {
      
      //char temp = bitRead(led,i);
      tab[count]=Serial.read();
      count++;
     }
    
    }
  }
  
  for(int i=0;i<8;i++)
  {
  Serial.print(tab[i]);
  }

Ans i'm sending < as char not string myPort.write('>');

But the output is still not understandable.

temp=(byte)(temp|(byte)12);

Do you really need all these casts? Casting a byte to a byte, in order to store it in a byte seems particularly unnecessary.

    while(Serial.read() != '>')
    {
      
      //char temp = bitRead(led,i);
      tab[count]=Serial.read();
      count++;
     }

You test that there is one byte of data, or more, available to read. Then, you read that character. If it's a '>', you drop into this loop. There may be nothing to read, but you read and store that nothing as fast as you can.

In addition to that being wrong, you are looking at one character. It's not a >, so you read and store the next (possibly non-existent) value.

You need to read and store the character:

int c;
while((c = Serial.read()) != '>')
{

Then, you need to make sure that c is not -1 before you store it.

if(c != -1 && count < ??) // Whatever your array size is (minus 1, of course)
{
      tab[count++]=c;
      tab[count] = '\0'; // In addition you MUST NULL terminate the array
}

But the output is still not understandable.

But, I'm not going to tell you what it was...