Last Value of a Constantly Changing Variable

How would I set an alarm if the newest value of a variable is 20% less than the previous one?

Just store the value into to another variable like oldValue before updating it. Then you can compare both afterwards.

lg, couka

Or write a function like updateVar(). Instead of assigning directly to the variable ( x = 1;) you call the updateVar function (updateVar(1);).

Inside that function, you can make the comparison before doing the assignment and do whatever it takes to set off your Alarm.

I am sorry, I do not understand. This variable, BPM, is set to update every 2 milliseconds. How could I program the Arduino so that if the newest variable of BPM varies 20% from the previous one it sets off an alarm?

We are not going to do all the work for you. Show us your code, then we can tell you what's wrong.

lg, couka

When I verify the following code, I get the error ‘expected initializer before void()’.

The line void TimerIsr is highlighted.

#include <TimerOne.h>
  


//OPEN ROAD
//This program was designed by x for the Open Road project

int Vibration1 = 8; // Sets the vibration motor's power source to pin 8
int Vibration2 = 12; // Sets the second vibration motor's power source to pin 12
int BuzzerPower = 7; // Sets the positive voltage to the buzzer at pin 7. 
int BuzzerSignal = 13; // Sets the signal to the buzzer at pin 13.
int message = 0; // Sets the port at which the Arduino will receive information from the phone to 0 (RX) and defines the information under the variable 'message'.
int Beat = 100;
int Roll = 0;
int Pitch = 0;
volatile int BPM


void setup() {
  pinMode(BuzzerPower, OUTPUT); // Sets the voltage of the buzzer to an output, which makes the Arduino expect to send energy there rather than receive it
  pinMode(BuzzerSignal,OUTPUT); // Sets the signal to the buzzer to an output, which makes the Arduino expect to send a signal there rather than receive one
  digitalWrite(BuzzerPower,HIGH); // Sets the power to the buzzer at High.
  Serial.begin(38400); // Sets the communication with the Bluetooth module
  Timer1.initialize(200000);
  
}

void timerIsr ()
{
  int Beat = BPM
  int Roll = angle_x
  int Pitch = angle_y
  if (angle_x > Roll + 1)
    {
    digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);
    }
   else if (angle_x < Roll - 1)
    {digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);
    }

   if (angle_y > Pitch +1)
    {
    digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);
    }
   else if (angle_y < Pitch -1)
    {
    digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);
    }
    
  delay(2)
  if (BPM < 0.8*Beat)
    digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);
    
  else if (BPM > 1.2*Beat)
    digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);

   

    
    
}
void loop() {
  
    int error;
  double dT;
  accel_t_gyro_union accel_t_gyro;


  error = read_gyro_accel_vals((uint8_t*) &accel_t_gyro);
  
  // Get the time of reading for rotation computations
  unsigned long t_now = millis();

  

  // Print the raw gyro values.


  // Convert gyro values to degrees/sec
  float FS_SEL = 131;
  float gyro_x = (accel_t_gyro.value.x_gyro - base_x_gyro)/FS_SEL;
  float gyro_y = (accel_t_gyro.value.y_gyro - base_y_gyro)/FS_SEL;
  float gyro_z = (accel_t_gyro.value.z_gyro - base_z_gyro)/FS_SEL;
  
  
  // Get raw acceleration values
  //float G_CONVERT = 16384;
  float accel_x = accel_t_gyro.value.x_accel;
  float accel_y = accel_t_gyro.value.y_accel;
  float accel_z = accel_t_gyro.value.z_accel;
  
  // Get angle values from accelerometer
  float RADIANS_TO_DEGREES = 180/3.14159;
//  float accel_vector_length = sqrt(pow(accel_x,2) + pow(accel_y,2) + pow(accel_z,2));
  float accel_angle_y = atan(-1*accel_x/sqrt(pow(accel_y,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES;
  float accel_angle_x = atan(accel_y/sqrt(pow(accel_x,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES;

  float accel_angle_z = 0;
  
  // Compute the (filtered) gyro angles
  float dt =(t_now - get_last_time())/1000.0;
  float gyro_angle_x = gyro_x*dt + get_last_x_angle();
  float gyro_angle_y = gyro_y*dt + get_last_y_angle();
  float gyro_angle_z = gyro_z*dt + get_last_z_angle();
  
  // Compute the drifting gyro angles
  float unfiltered_gyro_angle_x = gyro_x*dt + get_last_gyro_x_angle();
  float unfiltered_gyro_angle_y = gyro_y*dt + get_last_gyro_y_angle();
  float unfiltered_gyro_angle_z = gyro_z*dt + get_last_gyro_z_angle();
  
  // Apply the complementary filter to figure out the change in angle - choice of alpha is
  // estimated now.  Alpha depends on the sampling rate...
  float alpha = 0.96;
  float angle_x = alpha*gyro_angle_x + (1.0 - alpha)*accel_angle_x;
  float angle_y = alpha*gyro_angle_y + (1.0 - alpha)*accel_angle_y;
  float angle_z = gyro_angle_z;  //Accelerometer doesn't give z-angle
  
  set_last_read_angle_data(t_now, angle_x, angle_y, angle_z, unfiltered_gyro_angle_x, unfiltered_gyro_angle_y, unfiltered_gyro_angle_z);
  
  

  
  Serial.print(F("#FIL:"));             //Filtered angle
  Serial.print(angle_x, 2);
  Serial.print(F(","));
  Serial.print(angle_y, 2);
  Serial.print(F(","));
  Serial.print(angle_z, 2);
  Serial.println(F(""));
  
   if(Serial.available() > 0) // If more than 0 bytes of data are being received
    message = Serial.read(); // The variable 'state;
    {
    if (message == 1) // If the phone sends a message to turn on the alarm
    {
    digitalWrite(BuzzerSignal, HIGH); // Set the buzzer to loud
    digitalWrite(Vibration1,HIGH); // Set Motor 1 to high
    digitalWrite(Vibration2,HIGH); // Set Motor 2 to high
    delay(1000); // 
    digitalWrite(BuzzerSignal, HIGH); // Set the buzzer to loud
    digitalWrite(Vibration1,HIGH); // Set Motor 1 to high
    digitalWrite(Vibration2,HIGH); // Set Motor 2 to high
    delay(1000);
    digitalWrite(BuzzerSignal, HIGH); // Set the buzzer to loud
    digitalWrite(Vibration1,HIGH); // Set Motor 1 to high
    digitalWrite(Vibration2,HIGH); // Set Motor 2 to high
    delay(1000);
    digitalWrite(BuzzerSignal, LOW); // Set the buzzer to off
    digitalWrite(Vibration1,LOW); // Set Motor 1 to off
    digitalWrite(Vibration2,LOW); // Set Motor 2 to off
    }
    else if (message == 2) // If the phone sends a message to calibrate
    {
      
    }
    
    else
    {
    digitalWrite(BuzzerSignal, LOW); // Set the buzzer to off
    digitalWrite(Vibration1,LOW); // Set Motor 1 to off
    digitalWrite(Vibration2,LOW); // Set Motor 2 to off
    
    
    }
    }
 
 
}

You are missing semicolons on many lines. For example

volatile int BPM

and

  int Beat = BPM
  int Roll = angle_x
  int Pitch = angle_y

By the way, your use of delay(...) is going to be a problem in the long run. Are you aware that

    delay(4000);

makes the Arduino fairly useless for 4 seconds?

On the other hand,

  delay(2)

is fairly useless and is missing a semicolon.

chessy:
I am sorry, I do not understand. This variable, BPM, is set to update every 2 milliseconds. How could I program the Arduino so that if the newest variable of BPM varies 20% from the previous one it sets off an alarm?

Forget about the program for a minute and think about how you would know if the value had varied more than 20%

What would you need to know ?
How would you calculate the change in value ?
How would you turn this change into a percentage ?
Is the percentage change measured against the previous value or the new value ?

Yes. Thank you for pointing those out.

Would the following work:

int Beat = BPM;
  delay(2)
  if (BPM < 0.8*Beat)
    digitalWrite(BuzzerSignal,HIGH);
    digitalWrite(Vibration1,HIGH);
    digitalWrite(Vibration2,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignal,LOW);
    digitalWrite(Vibration1,LOW);
    digitalWrite(Vibration2,LOW);

The BPM is calculated every 2 milliseconds.

Please post an entire program, not just a snippet.

Edit - you already did in this parallel thread: https://forum.arduino.cc/index.php?topic=403597.0

... and in that one, you actually attempted to calculate a percentage. Stop wasting our time!

Would the following work:

Who knows ? It certainly does not determine that the BPM reading has changed by 20% since last measured.

How could I modify it to do that?

From what I understand, the program sets this variable (BPM) which is updated every 2 milliseconds to another variable (Beats) every 2.1 milliseconds. (+0.1 milliseconds for the time to run the other stuff). Then, it checks whether the new value of BPM differs from the previous value (now represented by Beats).

Is the error in the formatting or in the idea altogether?

aarg:
Edit - you already did in this parallel

Sorry aarg, I meant to understand the issue that was bringing up an error in the program there. Here, I am focusing not on that specific error but on whether I have the right process.

Can I suggest that you ask a moderator to merge the two threads

So, you have chosen the shotgun method of debugging your program.

I have used "Report to moderator" asking for the threads to be merged.

You mean I've got to find which threads to merge?
Any clues?

Sorry !

The link to the other thread is in reply #7 above
https://forum.arduino.cc/index.php?topic=403597.0

Also you can not use delay inside an ISR.

Ok, so I have coupled this with a program which produces values for my sensors.

void Check(){
    int Beat = BPM;
    int Roll = angle_x;
    int Pitch = angle_y;
    if (angle_x > Roll + 1)
    {
    digitalWrite(BuzzerSignalPin,HIGH);
    digitalWrite(Vibration1Pin,HIGH);
    digitalWrite(Vibration2Pin,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignalPin,LOW);
    digitalWrite(Vibration1Pin,LOW);
    digitalWrite(Vibration2Pin,LOW);
    }
   else if (angle_x < Roll - 1)
    {
    digitalWrite(BuzzerSignalPin,HIGH);
    digitalWrite(Vibration1Pin,HIGH);
    digitalWrite(Vibration2Pin,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignalPin,LOW);
    digitalWrite(Vibration1Pin,LOW);
    digitalWrite(Vibration2Pin,LOW);
    }

   if (angle_y > Pitch + 1)
    {
    digitalWrite(BuzzerSignalPin,HIGH);
    digitalWrite(Vibration1Pin,HIGH);
    digitalWrite(Vibration2Pin,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignalPin,LOW);
    digitalWrite(Vibration1Pin,LOW);
    digitalWrite(Vibration2Pin,LOW);
    }
   else if (angle_y < Pitch - 1)
    {
    digitalWrite(BuzzerSignalPin,HIGH);
    digitalWrite(Vibration1Pin,HIGH);
    digitalWrite(Vibration2Pin,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignalPin,LOW);
    digitalWrite(Vibration1Pin,LOW);
    digitalWrite(Vibration2Pin,LOW);
    }
    if (BPM < 0.8*Beat)
    {
    
    digitalWrite(BuzzerSignalPin,HIGH);
    digitalWrite(Vibration1Pin,HIGH);
    digitalWrite(Vibration2Pin,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignalPin,LOW);
    digitalWrite(Vibration1Pin,LOW);
    digitalWrite(Vibration2Pin,LOW);
    }
   else if (BPM > 1.2*Beat)
   {
    digitalWrite(BuzzerSignalPin,HIGH);
    digitalWrite(Vibration1Pin,HIGH);
    digitalWrite(Vibration2Pin,HIGH);
    delay(4000);
    digitalWrite(BuzzerSignalPin,LOW);
    digitalWrite(Vibration1Pin,LOW);
    digitalWrite(Vibration2Pin,LOW);
 }
}

The program is set to run every 2 milliseconds. The code for the timer is attached

 Timer1.initialize(200000);  
  Timer1.attachInterrupt(Check,200000);

However, when I upload, it does not work. What is wrong?