Lab6/ HW 6

Lab 6 (PIR + Light sensor 'Automatic Night Light'):

//Automatic Nightlight
const int RLED=11;     //Red LED on pin 9 (PWM)
const int LIGHT=0;    //Lght Sensor on analog pin 0
const int MIN_LIGHT=10;    //Minimum expected light value
const int MAX_LIGHT=500;    //Maximum Expected Light value
int val = 0;    //variable to hold the analog reading
void setup()
{
 pinMode(RLED, OUTPUT);   //Set LED pin as output
}
void loop()
{
 val = analogRead(LIGHT);    //Read the light sensor
 val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0);    //Map the light reading
 val = constrain(val, 0, 255);  //Constrain light value
 analogWrite(RLED, val);  //Control the LED

}


HW 6 (Great Circle Distance b/w 2 pts.):


/* 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. */
char latd1, latd2, longd1, longd2;
double lat1, long1, lat2, long2;
double gc_distance(double lat1,double long1,
double lat2,double long2);
double angle(double x1, double y1, double z1, double x2, double y2, double z2);
/* Get locations of two points. */
printf("\nEnter latitude for location 1:\n");
scanf("%lf",&lat1);
printf("Is the latitude N or S?\n");
scanf(" %c", &latd1);
if (latd1 == 's')
{
lat1 = -lat1;
}
printf("Enter the longitude for location 1:\n");
scanf("%lf", &long1);
printf("Is the longitude W or E?\n");
scanf(" %c", &longd1);
if ( longd1 == 'e')
{
long1 = 360 - long1;
}
printf("\nEnter latitude for location 2:\n");
scanf("%lf",&lat2);
printf("Is the latitude N or S?\n");
scanf(" %c", &latd2);
if (latd2 == 's')
{
lat2 = -lat2;
}
printf("Enter the longitude for location 2:\n");
scanf("%lf", &long2);
printf("Is the longitude W or E?\n");
scanf(" %c", &longd2);
if ( longd2 == 'e')
{
long2 = 360 - long2;
}
/* Print great circle distance. */
printf("Great Circle Distance: %.0f miles \n",
gc_distance(lat1,long1,lat2,long2));
/* 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 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. */
double angle(double x1, double y1, double z1, double x2, double y2, double z2)
{
double sum1, sum2, magnitude1, magnitude2, dotproduct, angle;

sum1 = x1*x1 + y1*y1 + z1*z1;
sum2 = pow(x2,2) + pow(y2,2) + pow(z2,2);
magnitude1 = sqrt(sum1);
magnitude2 = sqrt(sum2);
dotproduct = x1*x2 + y1*y2 + z1*z2;
angle = acos(dotproduct/(magnitude1*magnitude2));
return angle;
}
gamma = angle(x1, y1, z1, x2, y2, z2);
/* Compute and return great circle distance. */
return gamma*rho;
}

Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21