Having trouble counting the number of switches while having delays in void loop()

How can I count the number of prox switches and at the same time reference this value for conditionals within loop()? The delays interrupt my cumulative loop counter for prox sensor readings. I need to know the total number of prox changes because this is used to figure out the distance my robot has walked, and I want it to do certain things at certain distances.

// dc motor pins
const int enable1=10;
const int in2=7;
const int ref_high=4;
const int in1=8;

// servo pins
const int ref_high2=12;

const int in1_a=6;
const int in2_a=9;
const int enable1_a=5;

const int in1_b=13;
const int in2_b=2;
const int enable1_b=3;

//prox 
const int proxPin=11;
int proxState=0;
int intitialProxPresses=0;
int totalProxPresses=0;
int PreviousProxState=0;

void walkStraight() {
  digitalWrite(ref_high, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enable1, 50);
}
void stopWalking() {
  digitalWrite(ref_high, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enable1, 0);
}
void turnRight() {
    analogWrite(enable1_a,0);
    digitalWrite(in1_a,LOW);
    analogWrite(in2_a,255);
}


void setup() {
  // put your setup code here, to run once:
  pinMode(enable1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(ref_high, OUTPUT);
  pinMode(in1, OUTPUT);

  pinMode(ref_high2, OUTPUT);
  
  pinMode(in1_a,OUTPUT);
  pinMode(in2_a,OUTPUT);
  pinMode(enable1_a,OUTPUT);
  
  pinMode(in1_b,OUTPUT);
  pinMode(in2_b,OUTPUT);
  pinMode(enable1_b,OUTPUT);

  pinMode(proxPin,INPUT);
  Serial.begin(9600);
//servo power supply
  digitalWrite(in1_b,LOW);
  digitalWrite(in2_b,HIGH); 
  analogWrite(enable1_b,150);
  digitalWrite(ref_high2, HIGH);
  //initial servo state (on startup)
  analogWrite(enable1_a,255);
  digitalWrite(in1_a,HIGH);
  digitalWrite(in2_a,LOW);
}

void loop() {
  proxState=digitalRead(proxPin);
  if (proxState != PreviousProxState) {
    if (proxState==HIGH) {
      totalProxPresses++;
      Serial.println(totalProxPresses);
    }
    }

  if ((totalProxPresses>=1) && (totalProxPresses<=10)) {
    walkStraight();
  }
  if (totalProxPresses==10) {
    stopWalking();
    // delay(1000);
    turnRight();
    // delay(1000);

  }
  if ((totalProxPresses>=10) && (totalProxPresses<20)) {
    walkStraight();
    // delay(1000);
  }
  if (totalProxPresses>=20) {
    stopWalking();
  }

PreviousProxState=proxState;
}

Hello remusconnor

Welcome to the world's best Arduino forum ever.

You can use the delay() function if you are running a single task on the Arduino.

If you have serval tasks with a dedicated timing behaiviour it is usefull to have time slices based on millis() function.

Take the BlinkWithOutDelay example of the IDE and use this example to design your timer() function.

This timer() function might provide the following methodes:

  • start();
  • stop(),
  • event();
  • isRunning();

Have a nice day and enjoy coding in C++.

The BlinkWithOutDelay example is for a set time interval. I am trying to read from a sensor and get the cumulative total while also running other code.

What part of your code is causing the delays?

No, it is for executing the one piece of code while the running another one - absolutely the same as your problem.

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