Help in displaying Sensor Values in Array

Hello, I am trying to make a simple Robot that moves based on the reading from 5 IR Sensors. I don't want the code to be large or time consuming so I wanted the readings to be stored in an Array and that when I call the array it prints the value in Serial Monitor.

int Sensor1 = 2;
int Sensor2 = 3;
int Sensor3 = 4;
int Sensor4 = 5;
int Sensor5 = 6;

void setup() {
Serial.begin(38400);
pinMode(Sensor1,INPUT);
pinMode(Sensor2,INPUT);
pinMode(Sensor3,INPUT);
pinMode(Sensor4,INPUT);
pinMode(Sensor5,INPUT);
}

int s1 = digitalRead(Sensor1);
int s2 = digitalRead(Sensor2);
int s3 = digitalRead(Sensor3);
int s4 = digitalRead(Sensor4);
int s5 = digitalRead(Sensor5);

//Reading Array for Simplification
int SensorRead[3] = {s1,s2};

void loop() {
Serial.print("Sensor Reading: ");
Serial.println(SensorRead[1]);
delay(1000);
}

I know that this code prints the value of Sensor 1 if the code is right.But Serial Monitor is not changing the value.Perhaps my approach is wrong or something.So can anyone help?

PS: I was very curious to find out what happens if I put the Serial Baud Rate Speed (SBRS) @ 38400 BRS.Let me know if that was wrong!

Actually it prints the value of s2 because the array index starts at zero but as you never update the values stored in the array in the loop() function it will not change

int SensorRead[3] = {s1,s2};

does not bind the value of s1 and s2 to the array. You need to update the values explicitly

What value other than a 0 or a 1 are you expecting from digital reads?

I use 115200.

Where in that code is sensor 1 or 2 or 3 is being read into the array?

Like

SensorRead[0] = digitialRead( 0 );
SensorRead[1] = digitialRead( 1 );
SensorRead[2] = digitialRead( 2 );

Then, after the array is populated with data, the array can be printed out. Cool?

What do you mean explicitly? And bind the values as in read the values?

@Idahowalker 1) no other values than 0 and 1.
2)OK I think I should put these readings into the void loop so that they get updated
3) Let me see @Idahowalker.

Your code seems to be written on the assumption that once you have executed

int SensorRead[3] = {s1, s2};

that every time you change the value of s1 or s2 then the corresponding value held in the array will be updated, but this is not the case. In order to update a value held in the array you must explicitly change its value using its position in the array as the array index

Oh Ok. There is no other method to change the value in the array?

I am not sure what you mean by an "other method"

Here is an example of the normal way in which a value in an array would be updated

const byte buttonPins[] = {A3, A2};
const byte NUMBER_OF_BUTTONS = sizeof(buttonPins) / sizeof(buttonPins[0]);
byte values[NUMBER_OF_BUTTONS];

void setup()
{
  Serial.begin(115200);
  for (int b = 0; b < NUMBER_OF_BUTTONS; b++)
  {
    pinMode(buttonPins[b], INPUT_PULLUP);
  }
}

void loop()
{
  readButtons();
  showValues();
  delay(1000);
}

void readButtons()
{
  for (int b = 0; b < NUMBER_OF_BUTTONS; b++)
  {
    values[b] = digitalRead(buttonPins[b]);
  }
}

void showValues()
{
  for (int v = 0; v < NUMBER_OF_BUTTONS; v++)
  {
    Serial.print("button ");
    Serial.print(v);
    Serial.print(" :   value : ");
    Serial.println(values[v]);
  }
  Serial.println();
}

NOTE : the code uses INPUT_PULLUP to ensure that the inputs are always in a known state and pressing an input button takes the input LOW

I do not understand your question.

An array int q[5] = {0}; makes an array holding 5 values filled with 0's.

q[0]=digitairead(pinSpankie) puts the state of the digitial pin into the array cell 0. If you want, you can write assembler code or use DMA. If the concept of q[0]=digitairead(pinSpankie) is proving difficult then assembler or using DMA will be even more difficult.

You can use a for() loop something like or look it up on the internet on how do to a for()

int q[5] = {0};

for( int i = 0; i<5; i++ )
{
q[i]=digitialRead(i+2); // for pins 2 - 6. i=0+2 = pin 2, i=1+2 pin 3,
}

consider

const byte SensorPins [] = { 2, 3, 4, 5, 6 };
#define N_SENSOR    sizeof(SensorPins)

int sensorVals [N_SENSOR];

// -----------------------------------------------------------------------------
void
sensorUpdate (void)
{
    for (unsigned n = 0; n < N_SENSOR; n++)
        sensorVals [n] = digitalRead (SensorPins [n]);
}

// -------------------------------------
void
sensorDisp (void)
{
    Serial.print ("Sensor Reading:");
    for (unsigned n = 0; n < N_SENSOR; n++)  {
        Serial.print (" ");
        Serial.print (sensorVals [n]);
    }
    Serial.println ();
}

// -------------------------------------
void loop() {
    sensorUpdate ();
    sensorDisp ();
    delay(1000);
}

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(38400);

    for (unsigned n = 0; n < N_SENSOR; n++)
        pinMode (SensorPins [n] ,INPUT);
}

OK but I wanted to create an array to reduce typing/updating the values over and over again.Sigh! I have to drop this method.I am really a beginner in Arduino. Thanks so much for all your help!

@gcjr This code doesn't work.Gives value in SerialMonitor like !!5!!?

Pardon me. I was on a different BRS

does your code read the sensor correctly?

should analogRead () be used instead?

No the pins are connected to digitalPins so digitalRead works

Sensor Reading: 0 1 0 1 0
Sensor Reading: 0 1 0 1 0
Sensor Reading: 0 1 0 1 0
Sensor Reading: 0 1 0 1 0
Sensor Reading: 0 1 0 1 0
Sensor Reading: 0 1 0 1 0
Sensor Reading: 0 1 0 1 0

don't doubt that the digitalRead works, but a digital read results in either a 0 or 1, not a 5. i get the above output when i test the code without anything connected to those pins

if you expect a sensor value other that 0/1 wouldn't you need to use analogRead()? what type of sensor is it?

@gcjr the value 5 came because I put the Serial Monitor Baud Rate Speed(BRS) to 9600, while in the code you put 38,400
Reference:

#@gcjr Dec 18 2021 5:33 pm
void setup() {
    Serial.begin(38400);

I did not notice that while pasting your code hence I got the value 5.

@anon1080994 Dec 20 2021:

robin_bijo
(Help in displaying Sensor Values in Array - #13 by anon1080994)
Pardon me. I was on a different BRS

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.