using LowByte or bit command for long variable

Hi -

I’m using set of 32 sensors, whenever a sensor goes high, want to change the bit number of a long variable (variable long errorsummary in enclosed code and want to capture this in address loop2), then read and display - summary of sensors going high in one execution cycle. reminding again, i’m a beginner.

I’m using Mega 2560, compiling is not complete(there is some issue with LCD),and not yet included command for bit instruction(do not know where/how begin with it).

Thanks in advance.

Mega_Data_Evaluator_Router_06_08_2017_V8.ino (8.21 KB)

I'm a bit baffled about what you are trying to do with sen[]. You do realize that sen[n] refers to the n'th member of sen, right?

You have a lot of basic confusion to clean up before you go off to set up any kind of "bit number".

want to accumulate the values of sensors that go high in one execution cycle into sen[] and display those numbers on LCD, as mentioned I'm a beginner.

rockwellramesha: want to accumulate the values of sensors that go high in one execution cycle into sen[] and display those numbers on LCD, as mentioned I'm a beginner.

So sen[] holds data? So why is it initialized with pin numbers? What is an execution cycle? Why on earth are you using goto statements?

Edit - your code isn't even close to a real program. Give up and try some easier things and explore the examples that come with the IDE to get up to speed. You can also look at some C++ tutorials online. It will take too long to explain all your mistakes and guesses here.

If you’re a beginner, don’t use goto. It’s a recipe for bugs that are hard to solve.

  if (digitalRead(sen[A1] == HIGH));

  else (digitalRead(sen[A2] == HIGH);
  ...
  ...

So if sen[A1] equals high, do nothing. Further A1 equals 15 (on an Uno, not sure about Mega); so you’re using the 16th element (which by coincedence is A15.

else(digitalRead(sen[4] == HIGH);

Similar comment; however sen[4] is A4, so you read that pin (again).

You also will have to replace most of your ‘else’ by ‘if’; your current (incorrect) implementation will never check any other pins if e.g. A1 is HIGH.

Your good idea was to use an array; the bad idea is the way you used it.

Why do you use LOW for pinMode; although it does the same as using INPUT when used in the pinMode function, it’s less clear what you’re trying to achieve and maybe you’re trying to achieve something completely different. The below proves that both are the same

void setup()
{
  Serial.begin(250000);

  Serial.print("LOW is "); Serial.println(LOW);
  Serial.print("INPUT is "); Serial.println(INPUT);

Assuming you want to set the pins to input, you can loop through the sen array

void setup()
{
  Serial.begin(250000);

  // display number of elements in sen array to demeonstrate the use of 'sizeof(sen) / sizeof(sen[0])'
  Serial.print("Number of elements in sen array is "); Serial.println(sizeof(sen) / sizeof(sen[0]));

  // set all sensor pins to input
  for (int senCnt = 0; senCnt < sizeof(sen) / sizeof(sen[0]); senCnt++)
  {
    Serial.print("Setting pin ");
    Serial.print(sen[senCnt]);
    Serial.println(" to input");

    pinMode(sen[senCnt], INPUT);
  }
}

The above contains some debug info in the hope it makes clear what is happening.

I will continue a little later with loop().

The below will loop through your sensor pins and display their status (high or low); I’ve added a delay so you can read the data on the serial monitor.

void loop()
{
  // read all sensors
  for (int senCnt = 0; senCnt < sizeof(sen) / sizeof(sen[0]); senCnt++)
  {
    Serial.print("Sensor on pin ");
    Serial.print(sen[senCnt]);
    if (digitalRead(sen[senCnt]) == HIGH)
    {
      Serial.println(" is high");
    }
    else
    {
      Serial.println(" is low");
    }
  }

  delay(5000);
}

Next you can add a variable to hold the sensor values

void loop()
{
  // a variable to hold the values (status) of the sensors
  uint32_t sensorValues = 0;

  // check if we have enough space in sensorValues to store all sensors
  // size of uint32_t is 4 bytes, so 4 times 8 bits
  if ((sizeof(sensorValues) * 8) < (sizeof(sen) / sizeof(sen[0])))
  {
    Serial.println("Not enough space to store all sensor values");
    // hang forever
    for (;;);
  }

  ...
  ...

It contains some ‘hardening’ to prevent possible writing outside the variable.

And next you can fill sensorValues with the read values

    if (digitalRead(sen[senCnt]) == HIGH)
    {
      Serial.println(" is high");

      // set the corresponding bit in sensorValues
      sensorValues |= (1 << senCnt);
    }

The full loop()

void loop()
{
  // a variable to hold the values (status) of the sensors
  uint32_t sensorValues = 0;

  // check if we have enough space in sensorValues to store all sensors
  // size of uint32_t is 4 bytes, so 4 times 8 bits
  if ((sizeof(sensorValues) * 8) < (sizeof(sen) / sizeof(sen[0])))
  {
    Serial.println("Not enough space to store all sensor values");
    // hang forever
    for (;;);
  }

  // read all sensors
  for (int senCnt = 0; senCnt < sizeof(sen) / sizeof(sen[0]); senCnt++)
  {
    Serial.print("Sensor on pin ");
    Serial.print(sen[senCnt]);
    if (digitalRead(sen[senCnt]) == HIGH)
    {
      Serial.println(" is high");

      // set the corresponding bit in sensorValues
      sensorValues |= (1 << senCnt);
    }
    else
    {
      Serial.println(" is low");
    }

    Serial.print("sensorValues contains ");
    Serial.println(sensorValues, BIN);
  }

  delay(5000);
}

Note that the printing omits leading zeroes! Compiles but not tested.

Thanks a lot and appreciated Sterretje

Sterretje - just sharing the concept I,had

  • I've used Analog pins for digital inputs, on Mega board it is numbered A0..A15, from all 32 sensors, I'm only taking digital output(input to Mega)
  • I, want to make sure the initially all the pins are set to LOW, before the program is executed
  • Yes, the moment I, get first fault (ex: A0), I do not want to check with other sensor and stop the machine
  • In, Mega digital pins are from 0 to 54(including Tx,Rx, SCL and SDL) etc, hence I've assumed only with digital pins the numbering starts from 0 to 54, so I,thought the when I, use pinMode in the program in Mega it is meant for only digital pins

so, I, do not if in Mega A0 starts with pin"0" and Digital pins start from 17 and end up at 70!, need to check on that

All your pins are inputs, you can not set them low. Only an external device (e.g. your sensor) can make them low or high. The code in loop() that I presented takes care of the initial value by setting sensorValues to zero.

If the sequence in your sen array indicates the priority, you can break from the for loop if needed when a HIGH is read from the pin. I would only do this if timing is very critical; the for loop takes probably 2 or 3 microseconds per iteration when you remove all print statements; you can easily measure this by adding some code to measure the start time and end time of loop and subtract.

If you keep the for loop as is, you can easily test after that if A0 was HIGH.

if ((sensorValues & 1) == 1)
{
  //stop
}

To get the pinNumbers of the analog pins, you can use e.g.

Serial.println(A0);

thanks a lot! Sterretje