Storing digital sensor values in array

Hi everybody. I build a line follower tank with 3 analog sensors for detecting the line(PID regulated) and it works very good. I use two digital sensors for reading a line code. The line code is necessary when I have two ways to go with tank(left way or right way). With line code I want to choose witch way to go. When sensor1 == 1 then sensor2 starts to read a line code(for example 10101, black-white-black-white-black). This is the theory what I want to implement. Can I store this line code value in array? I tried this:

if(sensor1 == 1){
for(int i=0 ; i<4 ; i++){
val = digitalread(sensor2);
linecode*;*

  • }*
    }
    Probably the code is not good to store the values. Can anyone help me?

I can’t say your application is clear to me (and I’m guessing English isn’t your first language), but if reading that digital input 5 times should result in 10101 then this is how you’d store those 5 values in an array:

boolean linecode[5];
  if(sensor1 == 1){
    for(int i=0 ; i<5 ; i++){
      linecode[i] = digitalRead(sensor2);
    }
  }

The result of this code would {1, 0, 1, 0, 1} being stored in the linecode array.

Since you only have five bits to store, you really don’t need to store them in an array. This code would be more efficient:

byte linecode = 0;
  if(sensor1 == 1){
    for(int i=0 ; i<5 ; i++){
      linecode |= (digitalRead(sensor2) << i);
    }
  }

The result of this code would be 0b10101 being stored in the linecode byte.

I think I’m supposed to tell you to use [/code] tags in your posts to make your example code more readable.