Using the same digitalRead pin for multiple times

Hi,
I want to know how to use the same digitalRead pin for multiple times for my current project.I want to write the program to recognize the digitalRead by the number of times i used it. For example it should recognize second time reading as digitalRead() for the second time.

Try again, please.

You want to count how many times a button was pressed?
Or you want to collect a number of "samples" of the button over time?

Best I could make sense of this requirement as you have written it...

int pin = 2;
int i;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  i = 1;
}

void loop()
{
  bool pinState = digitalRead(pin);
  
  if (i == 1)
  {
    // "First" read - do something?
    i++;    
  } else if (i == 2)
  {
    // "Second" read - do something else?
    i++;
  }
}

You may also want to use a "switch" statement, or perhaps an array....

pcbbc:
You want to count how many times a button was pressed?
Or you want to collect a number of "samples" of the button over time?

Best I could make sense of this requirement as you have written it...

int pin = 2;

int i;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  i = 1;
}

void loop()
{
  bool pinState = digitalRead(pin);
 
  if (i == 1)
  {
    // "First" read - do something?
    i++;   
  } else if (i == 2)
  {
    // "Second" read - do something else?
    i++;
  }
}



You may also want to use a "switch" statement, or perhaps an array....

Hope that's one fast finger. That count is going to go up fast!

The literal answer to your question, is to make a function that both performs a digitalRead(), and also increments a counter variable, so:

long counter = 0;
...
...
int readAndCount(uint8_t pin) {
 counter++;
 return( digitalRead(pin) );
}

Then every time you would call digitalRead, call readAndCount instead.

Thankyou all for the replies :slight_smile:

Hi,
I want to know how to use the same digitalRead pin for multiple times for my current project.I want to write the program to recognize the digitalRead by the number of times i used it. For example it should recognized second time reading as digitalRead() for the second time. And each digital read includes in different functions. I tried to use a counter and return digital read value but it didn’t work.[/center]

int Inset1[] = {46, 47, 48, 49, 50, 51, 52, 53};//Dip switch one pins connected to arduino
int Inset2[] = {7, 6, 5, 4, 3, 2, 14, 15}; //Dip switch two pins connected to arduino
int OutPins[] = {25, 24, 23, 22}; //led pins
int i;//Array element's parameter of Inset1[] array
int j;//Array element's parameter of Inset2[] array
int s;//Array element's parameter of OutPins[] array
int waterlevel;//Indicate water level using dipswitches. If the waterlevel is at dipswitch1, number "1" represents on the serial monitor whether it is increasing or decresing water level.
//Number 2 represents on the serial monitor when the water level is at dipswitch2 and so on.
int h = 1; //Variable of increasing water level
int g = 3; //Variable of decreasing water level
int Inset1Counter = 0;
int Inset2Counter = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);

  setupInset1();//set the pinMode() function to dipswitch one
  setupInset2();//set the pinMode() function to dipswitch two
  setupOutPins();//set the pinMode() function to LEDs

  Inset1[0] = digitalRead(Inset1[0]);
  digitalWrite ( OutPins[0], 1);
}

void setupInset1() {
  for (i = 0; i < 8; i++) {
    pinMode (Inset1[i], INPUT_PULLUP);
    delayMicroseconds (100);
  }
}

void setupInset2 () {
  for (j = 0; j < 8; j++) {
    pinMode (Inset2[j], INPUT_PULLUP);
    delayMicroseconds (100);
  }
}

void setupOutPins() {
  for (s = 0; s < 4; s++) {
    pinMode (OutPins[s], OUTPUT);
    delayMicroseconds (100);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

  int state1=digitalRead(Inset1[0]);
  int state2=digitalRead(Inset2[0]);
  
 state1= Inset1IncRead1 ();//Sense the water level increasing in dip switch one
 state2= Inset2IncRead2 ();//Sense the water level increasing in dip switch two
 state1=Inset1IncRead3 ();//Sense the water level increasing in dip switch three
  //Inset2IncRead4 ();//Sense the water level increasing in dip switch four
  // Inset1IncRead5 ();//Sense the water level increasing in dip switch five
  //Inset2IncRead6 ();//Sense the water level increasing in dip switch six
  //Inset1IncRead7 ();//Sense the water level increasing in dip switch seven
  //Inset2IncRead8 ();//Sense the water level increasing in dip switch eight
  //Inset2DecRead ();//Sense the water level decreasing in dip switch one
  //Inset1DecRead();//Sense the water level decreasing in dip switch one
}

int readAndInset1Counter(uint8_t Inset1[0]) {
  Inset1Counter++;
  digitalRead(Inset1[0])==HIGH;
  return ( digitalRead(Inset1[0]) );
}

int readAndInset2Counter(uint8_t Inset2[0]) {
  Inset2Counter++;
  digitalRead(Inset2[0])==HIGH;
  return ( digitalRead(Inset2[0]) );
}

void Inset1IncRead1 () {

  waterlevel = h;

  if (Inset1Counter = Inset1Counter + 1) {
    Serial.println(waterlevel);
  }

  if (digitalRead (Inset1[6]) == HIGH) {
    digitalWrite(OutPins[1], 1);
  }
}


void Inset2IncRead2 () {

  waterlevel = h + 1;
  lastState = digitalRead(Inset2[0]);

  if (lastState == HIGH) {
    Inset2Counter = Inset2Counter + 1;
  }

  if ( Inset2Counter = Inset2Counter + 1) {
    Serial.println(waterlevel);
  }
  if (digitalRead (Inset2[1]) == HIGH) {
    digitalWrite(OutPins[0], 0);
  }

  if (digitalRead (Inset1[6]) == LOW && digitalRead (Inset2[2]) == HIGH ) {
    digitalWrite (OutPins[1], 1);
  }

  if (digitalRead (Inset2[6]) == HIGH) {
    digitalWrite(OutPins[0], 1);
  }

  delay (200);
}


void Inset1IncRead3 () {

  waterlevel = h + 2;
  lastState = digitalRead(Inset1[0]);
  lastState = state;

  if (lastState == HIGH)  {
    Inset1Counter = Inset1Counter + 2;
  }

  if (Inset1Counter = Inset1Counter + 2) {
    Serial.println(waterlevel);
  }
  if (digitalRead (Inset1[6]) == HIGH) {
    digitalWrite(OutPins[1], 1);
  }
}

In the above code i got an error : void value not ignored as it ought to be

Duplicate threads merged

Why did you start a new thread on the same subject ?

Which line?

Perhaps this one? What’s it supposed to do?

 digitalRead(Inset2[0])==HIGH;

That is only valid in an if...

if (digitalRead(Inset2[0])==HIGH) {
  //do something
}

Edit this code is wrong..

 if (Inset1Counter = Inset1Counter + 2) {

Single = is for assignment, double == is for comparison.
Even if you use == how can a single variable, Inset1Counter, ever be equal to itself + 2?

It’s not clear from your problem description what you are trying to do or why. Seems to be something to do with water levels and dip switches, although I’m not sure how a dip switch measures water levels.

Please state what the purpose of the program is and how it should function. Right now don’t try to explain how you think you are going to implement that in code, you’re getting ahead of yourself if you try.

In the above code i got an error : void value not ignored as it ought to be

The compiler told you exactly where the error is - you cannot assign the value a void function doesn’t return to a variable.

In here there are two dip switches with 8 common elements. So there are 16 digital input pins used in arduino mega to connect these two dip switches. Using these two dip switches I have to measure 8 water levels changing alternately between the two dip switches. When the first digital pin of dip switch one connected to arduino board is HIGH, program should be able to print 1 on serial monitor. When the first digital pin of dip switch two connected to arduino board is HIGH, program should be able to print 2 on serial monitor. And then again the first digital pin of dip switch one connected to arduino board is HIGH, program should be able to print 3 on serial monitor and the first digital pin of dip switch two connected to arduino board is HIGH, program should be able to print 4 on serial monitor and so on.

I did returning state1 and state2 variable values to void functions. Can’t i get return values to void functions?

Raweesha:
I did returning state1 and state2 variable values to void functions. Can’t i get return values to void functions?

That’s correct, you can’t; that’s why they’re “void”

And then again the first digital pin of dip switch one connected to arduino board is HIGH, program should be able to print 3 on serial monitor and the first digital pin of dip switch two connected to arduino board is HIGH, program should be able to print 4 on serial monitor and so on.

Why again the FIRST switches for outputting 3 and 4? This makes no sense for a level meter. If the first switches output 1 and 2, then the SECOND switches should output 3 and 4. The THIRD switches 5 and 6, etc.

Here's how to do that...

uint8_t pins[] = { 46, 7, 47, 6, 48, 5, 49, 4, 50, 3, 51, 2, 52, 14, 53, 15 };

uint8_t n = 16;
while (n > 0 && digitalRead(pins[n-1]) == LOW)
{
  n--;
}
Serial.println(n);

No, 8 elements of one switch set sense one water level. Here I attached schematic of this program.When pin 46 is HIGH program should be able to print 1 on serial monitor. When pin 7 is HIGH program should be able to print 2 on serial monitor. Again when pin 46 is HIGH program should be able to print 3 on serial monitor. When pin 7 is HIGH program should be able to print 4 on serial monitor and this is continue up t0 print 8th water level.
Check the attached schematic.

Raweesha:
No, 8 elements of one switch set sense one water level. Here I attached schematic of this program.When pin 46 is HIGH program should be able to print 1 on serial monitor. When pin 7 is HIGH program should be able to print 2 on serial monitor. Again when pin 46 is HIGH program should be able to print 3 on serial monitor. When pin 7 is HIGH program should be able to print 4 on serial monitor and this is continue up t0 print 8th water level.
Check the attached schematic.

So, when pin 7 is HIGH, it can print either 2 or 4, or both?

Is your intent to have each DIP switch represent an 8-bit binary number?

Your schematic is too low resolution to be able to read the pin numbers or much of the detail. But from what I can see of it all 8 switches on both banks of dip switches are connected to separate inputs.

It does no good just repeating your previous posts when forum members try to help and ask for clarification.

Again I ask....
Why is pin 46 (1st switch in DSW1) responsible for both printing level=1 AND level=3?
Why is pin 7 (1st switch in DSW2) responsible for both printing level=2 AND level=4?
What is the purpose of the other 14 switches on pins 47, 6, 48, 5, 49, 4, 50, 3, 51, 2, 52, 14, 53, 15 if not to detect the other (higher) water levels?

If you write the program the way you have described it (without providing any more information) all that will happen is that:

  • pin 46 HIGH will print an odd number continuously increasing: 1, 3, 5, 7, 9, 11, 13, 15, ....
  • pin 7 HIGH will print an even number continuously increasing: 2, 4, 6, 8, 10, 12, 14, 16, ....
  • no other pins or switches will have any effect whatsoever

I can't help but think you have some kind of confusion about the way dip switches work, and that you somehow have them confused with some kind of shift register. Such that on each successive read of a switch you get the next switch position? That's catagorically NOT the way dip switches work (or at least not in the circuit diagram you have presented without a shift register).

Edit:
You even say yourself...

No, 8 elements of one switch set sense one water level

...so it seems you must be confused because you can not get the state of the other 7 switches just by repeatedly reading the first one.

Have you tried the code I posted in #13 yet?

When pin 7 is HIGH for the first time program should be able to print 2. When it HIGH for the second time program should be able to print 4 only. When it HIGH for the third time program should be able to print 6 only. When it HIGH for the fourth time program should be able to print 8 only.
Same process goes with pin 46. When pin 46 is HIGH for the first time program should be able to print 1. When it HIGH for the second time program should be able to print 3 only. When pin 46 is HIGH for the THIRD time program should be able to print 5 ONLY. When it HIGH for the FOURTH time program should be able to print 7 only.

Other 7 elements of the dip switch is used for LED outputs. Actually, not all the elements. As an example when the 51 pin is HIGH, led 2 is on. I have no problem with setting outputs. What I want is to print the water level as I said before on the serial monotor.