0

Comparing two values but with a constrain of x amount of deg
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Comparing two values but with a constrain of x amount of deg

by Ganderra on Thu Nov 07, 2019 2:58 am

Hiya doing what I call theoretical programming while I wait for a part to arrive so that I have code ready to test it on. Basically, the essence of what I want to do is take two inputs from the same sensor (Compass) detect if there has been a change between those two values (Which I have). But I want something to trigger if that variable of change is greater than a number of 15.

Eg If the first measurement of the compass was 15 Degrees and the second measurement was 35 Degrees calculate if the change was positive; yes change is positive and the change was by 20 degrees since 20 is above the thresh hold active pin 4. Here is the code so far most of it is from the library demo

Code: Select all | TOGGLE FULL SIZE

void setup(void)
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
 
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
 
  /* Display some basic information on this sensor */
  displaySensorDetails();
}

void loop(void)
{
  /* Get a new sensor event */
  sensors_event_t event;
  mag.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");

  // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading1 = atan2(event.magnetic.y, event.magnetic.x);
 
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.22;
  heading1 += declinationAngle;
 
  // Correct for when signs are reversed.
  if(heading1 < 0)
    heading1 += 2*PI;
   
  // Check for wrap due to addition of declination.
  if(heading1 > 2*PI)
    heading1 -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees1 = heading * 180/M_PI;
 
  Serial.print("Heading 1 (degrees): "); Serial.println(headingDegrees);

  delay(500);

    /* Get a new sensor event */
  sensors_event_t event;
  mag.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");

  // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading2 = atan2(event.magnetic.y, event.magnetic.x);
 
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.22;
  heading2 += declinationAngle;
 
  // Correct for when signs are reversed.
  if(heading2 < 0)
    heading2 += 2*PI;
   
  // Check for wrap due to addition of declination.
  if(heading2 > 2*PI)
    heading2 -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees2 = heading * 180/M_PI;
 
  Serial.print("Heading 2 (degrees): "); Serial.println(headingDegrees);

  if(heading2 > heading1) {

        digitalWrite(4, HIGH);
   
    }

  if( heading2 < heading1 ) {

        digitalWrite(5, HIGH);
     
    }

    delay(500);
   
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);

    delay(100);
   
  }


Ganderra
 
Posts: 10
Joined: Thu Aug 09, 2018 12:55 am

Re: Comparing two values but with a constrain of x amount of

by adafruit_support_bill on Thu Nov 07, 2019 6:54 am

Code: Select all | TOGGLE FULL SIZE
int threshold = 15;  // deviation threshold

if (heading1 - heading2) > threshold)
{
    // code for negative deviation
}
if (heading2 - heading1) > threshold)
{
    // code for positive deviation
}

adafruit_support_bill
 
Posts: 74950
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.