Set a Specific Sampling Rate (100Hz)

Hello

I'm trying to keep my sampling rate a set time, right now it is not consistent at 100Hz. I know you somehow have to take 2 time stamps at the start of the loop and at the end of the loop and take the addition of them and then subtract 10 to keep it consistent. I don't know how to do this though. Please Help!!!!!!!!!!!!

Thanks

Thesis.ino (1.15 KB)

Guide for using millis() for timing.

The way you were supposed to post your sketch:

unsigned long time;


//Pin Definition
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin NOT USED
int sensorInterlink =  A0;  // interlink sensor input port
int sensorPeratech = A1;  //peratech sensor input port


//global Definitions
int buttonState = 0;         // variable for reading the pushbutton status




void setup()
{
  Serial.begin(115200); //frequency that it cycles at (sends data to the serial ouput)
  pinMode(ledPin, OUTPUT);// initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}


void loop()
{
  buttonState = digitalRead(buttonPin);


  if (buttonState == 1) //if the button is pushed
  {
    int sensorInterlink = analogRead(A0); //output from the interlink sensor
    int sensorPeratech = analogRead(A1); //output from the peratech sensor
    time = millis(); //time is measured in ms
    Serial.println(), Serial.print(time), Serial.print("\t"), Serial.print(sensorInterlink), Serial.print("\t"), Serial.print(sensorPeratech); //displaying the 2 varibles (time, and output)
    delay(10); //delay 8ms
  }
  else (buttonState == 0);
  {
    ;;
  }
  
}

Note:

  else (buttonState == 0);
  {
    ;;
  }

is functionally equivalent to:

  else
  {
    (buttonState == 0);
  }

  {
    ;;
  }

which, when you get rid of statements that do nothing, is functionally equivalent to:

  else
  {}

  {}

which is functionally equivalent to: