Processing of Multiple Digital Inputs

I am into a project which includes processing inputs (digital) from 3 sensors simultaneously. (processing here refers to getting the inputs from sensors and displaying that on a 20X4 LCD display). Can anyone help me with the sketch please. The sensors are 3 Hall effect sensors (A1104) (which have one digital output pin each), and the display i am using is standard 20X4 LCD display.
(Arduino UNO board).

You cannot read 3 sensors simultaneously, but depending on what type of sensors they are you may be able to read them sequentially so fast that to all intents and purposes they will be read at the same time.

Put the sensor pins in an array and iterate through it with a for loop is the normal method

What type of sensors are they ?

Have you made any attempt at coding the project? If so, post the code and describe what it does and how that differs from what you expect. Also post a schematic of the project. Include all components (and part numbers/values) and power supplies. We won't write the code for you, but will help you get it working.

Read the "how to use this forum-please read" stickies to see how to properly post code and some hints on how to ask an effective question. See #7 & 11.

UKHeliBob:
You cannot read 3 sensors simultaneously, but depending on what type of sensors they are you may be able to read them sequentially so fast that to all intents and purposes they will be read at the same time.

Put the sensor pins in an array and iterate through it with a for loop is the normal method

What type of sensors are they ?

Thanks sir for that info! The sensors I'm using here are digital uni-polar hall effect sensors , that varies their output voltage in response to a magnetic field.
OK, i could give it a try using the for loops! :slight_smile: (I haven't tried that yet).
ps_ i am a noob so this might take a while!

groundFungus:
Have you made any attempt at coding the project? If so, post the code and describe what it does and how that differs from what you expect. Also post a schematic of the project. Include all components (and part numbers/values) and power supplies. We won’t write the code for you, but will help you get it working.

Read the “how to use this forum-please read” stickies to see how to properly post code and some hints on how to ask an effective question. See #7 & 11.

Actually I was working with Interrupt service routine in my program and I noticed that I have UNO board that basically has only 2 pins for interrupt. So, the idea of getting ouput from 3 sensors simultaneously (with the interrupt routine) won’t work with UNO board.
Thanks for the help, anyway.!

Direct port manipulation will a low you to read the state of all the pins on one port at the same time.

groundFungus:
Direct port manipulation will a low you to read the state of all the pins on one port at the same time.

Thank you so much for the reference. Can you help me find out if there's a way we can use more than 2 pins for interrupt cycle on the UNO borad??
A page here says you can only use digital pins 2 and 3 for interrupts.
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
Thanks for the help!

You can use pin change interrupts PinChangeInterrupt which give you more pins to use than hardware interrupts but first I think you should explain why you think that you need more than 2 interrupts, or indeed any interrupts.

The sensors I’m using here are digital uni-polar hall effect sensors , that varies their output voltage in response to a magnetic field.

Can you please provide a link to the sensors you are using.

I am into this project which uses 3 Hall effect sensors (based on Interrupt Service Routine). I was wondering if I could use these 3 sensors on one board. Although an article here says the interrupt pins available on arduino UNO board are only 2 (Digital pins 2 and 3). Is there anyway I could use other pins for interrupt routine? attachInterrupt() - Arduino Reference

I am using Arduino UNO board and A1104 Hall effect sensors.

The interrupt on change is available on all pins , but is slightly slower.

UKHeliBob:
You can use pin change interrupts PinChangeInterrupt which give you more pins to use than hardware interrupts but first I think you should explain why you think that you need more than 2 interrupts, or indeed any interrupts.
Can you please provide a link to the sensors you are using.

Here's the link to the sensor, although it's not exactly the one I bought.

I'm uploading here a code of how I am using the hall effect sensors by a interrupt routine.

volatile byte half_revolutions;
 unsigned int rpm;
 unsigned long timeold;
 void setup()
 {
   Serial.begin(9600);
   attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
   half_revolutions = 0;
   rpm = 0;
   timeold = 0;
 }
 void loop()//Measure RPM
 {
   if (half_revolutions >= 2) 
   { 
     rpm = 30*1000/(millis() - timeold)*half_revolutions;
     timeold = millis();
     half_revolutions = 0;
     Serial.println(rpm,DEC);
   }
 }
 void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
 {
   half_revolutions++;
 }

using more than 2 pins for interrupt will help me use 3 sensors ( which i'm thinking to use in the same way as the single one i am using above).

Thanks for the help!

What range of RPM are you expecting ?

it is quite likely that you don't need to use interrupts at all if the RPM is relatively small

UKHeliBob:
What range of RPM are you expecting ?

it is quite likely that you don't need to use interrupts at all if the RPM is relatively small

Although I am accurately getting RPM upto 500 but it will be kind of you to tell me if any other way is possible.( so that I won't be required to handle all that interrupt pin mess!!)

it will be kind of you to tell me if any other way is possible.

This is the sort of thing that I had in mind but I have no way of testing it as I don’t have the hardware required.

const byte sensorPins[] = {10, 11, 12};
unsigned long previousTimes[3];
const byte TOTAL_SENSORS = sizeof(sensorPins) / sizeof(sensorPins[0]);

void setup()
{
  Serial.begin(115200);
  for (int s = 0; s < TOTAL_SENSORS; s++)
  {
    pinMode(sensorPins[s], INPUT_PULLUP);
  }
}
void loop()
{
  unsigned long currentTime = millis();
  for (int s = 0; s < TOTAL_SENSORS; s++)
  {
    if (digitalRead(sensorPins[s]) == HIGH)
    {
      unsigned long period = currentTime - previousTimes[s];
      previousTimes[s] = currentTime;
      //code here to calculate RPM for sensor based on the period
    }
  }
}

UKHeliBob:
This is the sort of thing that I had in mind but I have no way of testing it as I don’t have the hardware required.

const byte sensorPins[] = {10, 11, 12};

unsigned long previousTimes[3];
const byte TOTAL_SENSORS = sizeof(sensorPins) / sizeof(sensorPins[0]);

void setup()
{
 Serial.begin(115200);
 for (int s = 0; s < TOTAL_SENSORS; s++)
 {
   pinMode(sensorPins[s], INPUT_PULLUP);
 }
}
void loop()
{
 unsigned long currentTime = millis();
 for (int s = 0; s < TOTAL_SENSORS; s++)
 {
   if (digitalRead(sensorPins[s]) == HIGH)
   {
     unsigned long period = currentTime - previousTimes[s];
     previousTimes[s] = currentTime;
     //code here to calculate RPM for sensor based on the period
   }
 }
}




[/quote

Ok maybe I'll try to go try this code once but can you please let me know what algorithm are you using to detect the output of sensors ( kindly correct me if I'm wrong somewhere cuz I am a noobie to all this stuff).
Secondly, I was a bit confused if there's any difference in pinchangeInterrupt and external interrupt. **Could you please help me know how can I use pinchangeInterrupt to run interrupt routine on more than 2 pins?**

please let me know what algorithm are you using to detect the output of sensors

There is no algorithm. The code reads the state of each input pin.

However, I should point out that the code I posted, whilst showing the principle of reading several inputs in quick succession is flawed. Instead of detecting that an input is HIGH it should actually detect that the input has become HIGH since the last time that it was read. This is not difficult to do but first you need to understand the principle of reading several inputs in quick succession.

I was a bit confused if there's any difference in pinchangeInterrupt and external interrupt

External interrupts are detected on specific pins of the Arduino. Pins 2 and 3 on a Uno, for instance.

Pin Change interrupts can be used on most digital pins but there is more work (programming) required to detect that a pin in a group of pins on a port has changed state and then to determine exactly which pin has changed state. Did you look at the example on the page linked to in reply #7 ?

UKHeliBob:
There is no algorithm. The code reads the state of each input pin.

However, I should point out that the code I posted, whilst showing the principle of reading several inputs in quick succession is flawed. Instead of detecting that an input is HIGH it should actually detect that the input has become HIGH since the last time that it was read. This is not difficult to do but first you need to understand the principle of reading several inputs in quick succession.
External interrupts are detected on specific pins of the Arduino. Pins 2 and 3 on a Uno, for instance.

Pin Change interrupts can be used on most digital pins but there is more work (programming) required to detect that a pin in a group of pins on a port has changed state and then to determine exactly which pin has changed state. Did you look at the example on the page linked to in reply #7 ?

Okkk! A hell lot of thanks there!! I actually read what was on that article regarding the pin change interrupt. As far as the inputs of sensors in succession is considered, I will leave that still at the moment. But I browsed through some ways of using pin change interrupt and I actually got a way by doing slight modifications in the code for basic pin change interrupt (You can say I used a cheat sheet :P).
I was actually looking for any way by which I can use other digital pins in the same way as pins 2 and 3 are used (interrupt 0 and 1). Although it worked out that those two are the only external interrupt pins available but using pin change interrupt worked anyway!
Can you still help me a bit by telling how can I incorporate the code for both external interrupt pins and pin change interrupt in the same program ( so that I can use 2 sensors at the external interrupt pins and 1 at the pin change interrupt).
Thank you so much for your help!

so that I can use 2 sensors at the external interrupt pins and 1 at the pin change interrupt

Why complicate things by doing that ?

I am not convinced that you need to use interrupts in the first place but if you insist on doing it that way using pin change interrupts throughout would be my advice.

Have you got the PinChangeInt library installed ? It comes with examples.

Why are PCINT interrupts slower? They don't deserve attachInterrupt() or other indirection.

DrDiettrich:
Why are PCINT interrupts slower? They don't deserve attachInterrupt() or other indirection.

The interrupts themselves are not slower but you may need code to figure out which pin was triggered and whether it was a rising or falling interrupt.

...R