Line follower sensor problem

Hi there,
I have a line follower sensor array with 7 sensors. I wrote the following code to test the output from sensor array.

int d1;
int d2;
int d3;
int d4;
int d5;
int d6;
int d7;

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(14, INPUT);
  pinMode(15, INPUT);
  pinMode(16, INPUT);
  pinMode(17, INPUT);
  pinMode(18, INPUT);
  pinMode(19, INPUT);
}

void loop()
{
  d7 = !digitalRead(2);
  Serial.print(d7);
  Serial.print(" ");
  d1 = !digitalRead(14);
  Serial.print(d1);
  Serial.print(" ");
  d2 = !digitalRead(15);
  Serial.print(d2);
  Serial.print(" ");
  d3 = !digitalRead(16);
  Serial.print(d3);
  Serial.print(" ");
  d4 = !digitalRead(17);
  Serial.print(d4);
  Serial.print(" ");
  d5 = !digitalRead(18);
  Serial.print(d5);
  Serial.print(" ");
  d6 = !digitalRead(19);
  Serial.print(d6);
  Serial.println();
  delay(500);
}

The problem is that when all the sensors are on white or on black it is giving me the same output i.e. all 1s when all the sensors are either on all black or on all white. It is giving me correct output when some sensors are on black and others are on white. I checked the sensor array with multimeter. It is correctly giving 5v on sensing white and 0.70v on sensing black on all the sensor. I don't understand why this is happening. Any help will greatly appreciated.

What sort of sensors are you using ? Can I suggest that you reduce the circuit to just 3 sensors to begin with to help debugging.

By the way, your code is crying out to be simplified by using arrays and a for loop.

Thank you for replying UKHeliBob

I am using following line sensor array

I tried with 3 sensor with same result :frowning:
Voltages across the sensors read correctly.

Simplify your code so it just reads one sensor and see what happens

void loop()
{
  d7 = digitalRead(2);
  Serial.println(d7);
  delay(500);
}

I wonder should you be using pinMode(2, INPUT_PULLUP);

...R

Thanks for replying Robin2

The result is same. I am just reading the value from pin 2.
When all the sensors are on white or black, it is giving me 0 on pin 2. When half the sensors are on black and other are on white it is giving me correct output on pin 2.
it is suppose to give all 1s when all the sensors are on white but it is giving me 0.

alivebj:
When all the sensors are on white or black, it is giving me 0 on pin 2.

Did you try INPUT_PULLUP ?

When you tried the single sensor code did you have the other sensors connected to the Arduino ? What happens if you disconnect all but one of them ?

I think you need to make a pencil drawing showing all the connections and post a photo of the drawing.

Have you a GND connection between the sensor device and the Arduino? I wonder if, when some of the sensors are different, the different outputs are making the circuit work properly in an electrical sense.

...R

Thanks for replying Robin2.

Connecting GND of sensor array to Arduino GND Worked.
Thank you very much. :slight_smile:

Before you go -

please give your variable meaningful names. You'll thank yourself later. And put the pin numbers in named constants rather than embedding them in your code. Bonus points if the names of your constants for pin numbers match the names of the variables in which you put the results of reading them.

Which would you rather debug when you return to this later. This:

  d5 = !digitalRead(18);

or this

  frontLeftState = !digitalRead(frontLeftPin);

?