IF does not work in code

Hi all,

I am trying to measure the reaction time of moving a foot between two pressure plates. I have taken another code as a template from another user (reaction time with buttons), and modified to code to add two load cells which basically have analog outputs so I simply use a analog read function. In my code I have put a delay of 1 millisec to give a frequency of 1000Hz (assuming the other parts of the code dont take much time), however the if statement doesn’t get activated when the statement is satisfied. This code works correctly when I put a delay of 100 millisec. Is there a way i can write my code to make it work at a delay of 1 millisec. I know the values are measured correctly because of the serial monitor response, however the if statement (if (potstate > LoadP)) still doesnt get activated.

My first code for a project apologies if it ain’t very clear.

// set pin numbers:
const int LoadCell1 = 2;     // the number of the Pedal load Cell
const int LoadCell2 = 4;     // the number of the Floor Load Cell
const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin = 2;        // the number of the LED (stimulus LED)
const int ledPin1 = 7;        // The number for the LoadCell2 indicator LED

//Define loads for reaction time
const int LoadF = 300;   // floor Load Cell target
const int LoadP = 800;   // Pedal Load Cell target


// Variables will change:
int ledState = LOW;         // the current state of the LED
int potstate;             // the current reading from LoadCell1
int buttonState;             // the current reading from button
int lastButtonState = LOW;    // the previous reading from the input button
int lastpot = 5;              // removes any noise at lower end 

int LC2Count = 0;
// the following variables are long's because the time, measured in microseconds,
// will quickly become a bigger number than can be stored in an int.

unsigned long lastDebounceTime = 0;   // the last time the output pin was toggled
long debounceDelay = 20000;           // the debounce time in micro second

unsigned long randNumber;                 // Generated random number
unsigned long minRandomNumber = 2000;     // minimum number used to specify the range of random number
unsigned long maxRandomNumber = 5000;     // maximum number used to specify the range of random number

unsigned long time1,time2,time3,react;
int buttonPressed = LOW;
int printcount = 0;

int takeReading = LOW;

//following variable help in reading buttons pins
int startread;
int reading1;
int reading2;
int reading3;
int start;


void setup()
{
      pinMode(buttonPin, INPUT);
      pinMode(ledPin, OUTPUT);
      pinMode(ledPin1, OUTPUT);
      
      //enabling serial communication
      Serial.begin(115200);
      
      // set initial LED state
      digitalWrite(ledPin, ledState);
      digitalWrite(ledPin1, ledState);
    
      // if analog input pin 0 is unconnected, random analog
      // noise will cause the call to randomSeed() to generate
      // different seed numbers each time the sketch runs.
      // randomSeed() will then shuffle the random function.
      randomSeed(analogRead(0));
}
    
void loop() 
{ 
  startread = analogRead(LoadCell2);

   if (startread > LoadF)
   {
   digitalWrite(ledPin1, HIGH);
   start = 1;
   
   }
   else
   {
    digitalWrite(ledPin1, LOW);
    start = 0;
   }
   //Serial.println(start);
      if( printcount == 0)
      {
          Serial.println("press button when you are ready");
          printcount = 1;
          takeReading = LOW;    // not to read button number 1 and 
          
      }
      
      if( (buttonPressed == HIGH)&& (start == 1))                             // this part is the stimuli control 
      {
          digitalWrite(ledPin, LOW);
          randNumber = random(minRandomNumber,maxRandomNumber);     //in our code we have kept them as 2000 to 5000
          delay(randNumber);
          
              digitalWrite(ledPin, HIGH);
              time1 = micros();
           
          buttonPressed = LOW;
           }

      reading2 = digitalRead(buttonPin);         //read button
      
      if(takeReading == HIGH)
      {
              reading1 = analogRead(LoadCell1);
              reading3 = analogRead(LoadCell2);
              time3 =  micros();
           
              if ((reading3 < LoadF) &&  (LC2Count == 0))
              { react = time3 - time1;
               LC2Count = 1;
              }
              Serial.print((time3-time1)/1000); // change to micro please
              Serial.print("\t"); //tab
              Serial.print(reading1);
              Serial.print("\t"); //tab 
              Serial.println(reading3); 
              delay(1);
      } 
    
      // If the switch changed, due to noise or pressing:             
      if (reading1 < lastpot || reading2 != lastButtonState ) 
      {
        // reset the debouncing timer
        lastDebounceTime = micros();
      }
      
      if ((micros() - lastDebounceTime) > debounceDelay)      
      {
          // whatever the reading is at, it's been there for longer
          // than the debounce delay, so take it as the actual current state:
          // if the button state has changed:
          if (reading1 > lastpot)
          {
                potstate = reading1;
                
                // only toggle the LED if the new button state is HIGH
                if (potstate > LoadP)   //this if statement dont work at 1000HZ
                {
                  Serial.print("Your reaction time is: ");
                  Serial.print(react/1000);
                  Serial.println("ms");
                  Serial.print("Your movement time is: ");
                  Serial.print(((lastDebounceTime - time1)-react)/1000);
                  Serial.println("ms");
                  Serial.print("Your total braking time is: ");
                  Serial.print((lastDebounceTime - time1)/1000);
                  Serial.println("ms");
                  Serial.println();
                  
                  digitalWrite(ledPin, LOW);
                  printcount = 0;
                  LC2Count = 0;
                }
          }
          if (reading2 != buttonState)
          {
                buttonState = reading2;
                
                // only toggle the LED if the new button state is HIGH
                if ((buttonState == HIGH)&& (start == 1))                                 //starts taking reading
                {
                  Serial.println("READY TO GO");
                  buttonPressed = HIGH;
                  takeReading = HIGH;              
                }
          }
      }
    
      // save the reading.  Next time through the loop,
      // it'll be the lastButtonState:
      lastpot = reading1;
      lastButtonState = reading2;
     
}

Thank you in advance,

Zee

Can anyone help? :confused:

First of it is very unreasonable to bump your Thread after just 7 hours. 48 hours would be reasonable.

You need to use the AutoFormat tool to indent your code consistently - it will make it much easier to read.

I can't figure out what delay() you are referring to - that you want to set at 1 msec.

In general if you want a responsive program you should not use delay() at all as it blocks the Arduino from doing other things. Use millis() to manage timing as illustrated in Several Things at a Time.

If all you want to do is figure out the difference in time between the pressing of one sensor and another sensor I suspect a much shorter program would be sufficient. Something like this pseudo code

void loop() {
    read and save SensorA and save the value of micros()
    if sensAvalue > threshold
       that value of micros = startTime
       note that you have a start value
    read and save SensorB and save the value of micros()
    if sensBvalue > threshold
       that value of micros = endTime
       note that you have an endTime
    if you have an endTime and a startTime
       calculate the difference
}

...R

Thank you for your reply Robin,

Apologies for the bump up I shall be more patient next time.

I have now solved the issue I had, I managed to figure out that it was actually a hardware issue rather than a software issue. The code works well now.

Zee