Homework Number 6

/*  This program determines the distance between two points    */
/*  that are specified with latitude and longitude values      */
/*  that are in the Northern Hemisphere.                       */
#include <stdio.h>
#include <math.h>
#define PI 3.141593
int main(void)
{
/*  Declare variables and function prototype.  */
double lat1, long1, lat2, long2;
char again = 'y',lat1_d, long1_d, lat2_d, long2_d;
double gc_distance(double lat1,double long1,
double lat2,double long2);


/*  Get locations of two points.  */
while (again == 'y'){
printf("Enter latitude (N/S)and longitude (E/W) ");
printf("for location 1: \n");
scanf("%lf %s %lf %s",&lat1, &lat1_d, &long1, &long1_d);
if (lat1_d == 's'||'S'){
 lat1 = -lat1;
}
if (long1_d == 'w'||'W'){
 long1 = -long1;
}
printf("Enter latitude (N/S) and longitude (E/W) ");
printf("for location 2: \n");
scanf("%lf %s %lf %s",&lat2, &lat2_d, &long2, &long2_d);
if (lat2_d == 's'||'S'){
 lat2 = -lat2;
}
if (long2_d == 'w'||'W'){
 long2 = -long2;
}

/*  Print great circle distance.  */
printf("Great Circle Distance: %.0f miles \n",
gc_distance(lat1,long1,lat2,long2));
printf("Again? (y/n)");
scanf(" %s", &again);
/*  Exit program.  */
}
return 0;
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/*  This function computes the distance between two            */
/*  points using great circle distances.                       */
double gc_distance(double lat1,double long1,
double lat2,double long2)
{
/*  Declare variables.  */
double angle(double x1, double y1, double z1, double x2, double y2,double z2);
double rho, phi, theta, gamma, dot, dist1, dist2,x1, y1, z1, x2, y2, z2;
/*  Convert latitude,longitude to rectangular coordinates.  */
rho = 3960;
phi = (90 - lat1)*(PI/180.0);
theta = (360 - long1)*(PI/180.0);
x1 = rho*sin(phi)*cos(theta);
y1 = rho*sin(phi)*sin(theta);
z1 = rho*cos(phi);
phi = (90 - lat2)*(PI/180.0);
theta = (360 - long2)*(PI/180.0);
x2 = rho*sin(phi)*cos(theta);
y2 = rho*sin(phi)*sin(theta);
z2 = rho*cos(phi);
/*  Compute angle between vectors.  */
gamma = angle(x1, y1, z1, x2, y2, z2);
/*  Compute and return great circle distance.  */
return gamma*rho;
}

double angle(double x1, double y1, double z1, double x2, double y2, double z2){
double cos_gamma, gamma;
cos_gamma = ((x1*x2) + (y1*y2) + (z1*z2))/(sqrt(x1*x1 + y1*y1 + z1*z1)*sqrt(x2*x2 + y2*y2 + z2*z2));
gamma = acos(cos_gamma);
return gamma;
}


ARDUINO PIR AND PHOTO SENSOR SKETCH
const int LED=8;//The LED is connected to pin 9
const int PIR=2;//The PIR is connected to pin 2
const int photo = 0;
int val = 0;
int counts = 0;
void setup()
{
 pinMode(LED, OUTPUT);
 pinMode(PIR, INPUT);
 pinMode(photo, INPUT);
 Serial.begin(9600);
}
void loop()
{
 Serial.println(analogRead(photo));
 if (digitalRead(PIR) == LOW && analogRead(photo) >= 135)
 {
  digitalWrite(LED, LOW);
 }
 if (digitalRead(PIR) == HIGH && analogRead(photo) >= 135){
  digitalWrite(LED, HIGH);
  counts = counts+1;

 }
 if (digitalRead(PIR) == LOW && analogRead(photo) <= 135){
  int steps = 0;
  while (steps < counts){
    digitalWrite(LED, HIGH);
    delay(100);
    digitalWrite(LED, LOW);
    delay(100);
  }
 }
}

Comments

Popular posts from this blog

Seismic Code

Strings

BMP180 Code + Library to SD card