Monday, September 17, 2018

day 6 9/13/18

Great Circle Distance Problems
#1,2,3,4,and5
#include <stdio.h>
#include <math.h>
#define PI 3.141593
double angle(double, double, double, double, double, double);
int main(void)
{
/* Declare variables and function prototype. */
double lat1, long1, lat2, long2;
char direction, direction2, direction3, direction4;
double gc_distance(double lat1,double long1,
double lat2,double long2);
/* Get locations of two points. */
printf("Enter latitude and longitude west ");
printf("for location 1: \n");
scanf("%lf %lf",&lat1,&long1);
printf("is the latitude N or S\n");
scanf(" %c", &direction);
printf("is the longitude W or E\n");
scanf(" %c", &direction3);
printf("Enter latitude and longitude west ");
printf("for location 2: \n");
scanf("%lf %lf",&lat2,&long2);
printf("is the latitude N or S\n");
scanf(" %c", &direction2);
printf("is the longitude W or E\n");
scanf(" %c", &direction4);
if(direction==78){
lat1 = lat1*1;
}
else if(direction==83){
lat1 = lat1*-1;
}
if(direction2==78){
lat2 = lat2*1;
}
else if(direction2==83){
lat2 = lat2*-1;
}
if(direction3==69){
long1 = 360 - long1;
}
else if(direction3==87){
long1 = long1*1;
}
if(direction4==69){
long2 = 360 - long2;
}
else if(direction4==87){
long2 = long2*1;
}
/* 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, x, y, z, X, Y, Z;
/* Convert latitude,longitude to rectangular coordinates. */
rho = 3960;
phi = (90 - lat1)*(PI/180.0);
theta = (360 - long1)*(PI/180.0);
x = rho*sin(phi)*cos(theta);
y = rho*sin(phi)*sin(theta);
z = rho*cos(phi);
phi = (90 - lat2)*(PI/180.0);
theta = (360 - long2)*(PI/180.0);
X = rho*sin(phi)*cos(theta);
Y = rho*sin(phi)*sin(theta);
Z = rho*cos(phi);
/* Compute angle between vectors. */
gamma = angle(x,y,z,X,Y,Z);
/* Compute and return great circle distance. */
return gamma*rho;
}
double angle(double x1, double y1, double z1, double x2, double y2, double z2){
double dot, dist1, dist2, gamma;
dot = x1*x2 + y1*y2 + z1*z2;
dist1 = sqrt(x1*x1 + y1*y1 + z1*z1);
dist2 = sqrt(x2*x2 + y2*y2 + z2*z2);
gamma = acos(dot/(dist1*dist2));
return gamma;
}

Arduino Light and Motion Sensor
CLASSWORK
This program senses the amount of light coming into the sensor and as the sensor is
covered into darkness it illuminates the rgb more and more.
const int RLED=9; //Red LED on pin 9 (PWM) const int LIGHT=A0; //Lght Sensor on analog pin 0 const int MIN_LIGHT=200; //Minimum expected light value const int MAX_LIGHT=900; //Maximum Expected Light value int val = 0; //variable to hold the analog reading void setup() { pinMode(RLED, OUTPUT); //Set LED pin as output Serial.begin(9600); } 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 Serial.println(val); }

HOMEWORK
Connect your PIR sensor and your CdS cell(light sensor). The PIR and CdS sensors will control the LED behavior as follows: When the PIR is triggered the LED is on. When the CdS is uncovered and the PIR sensor is untriggered, the LED is off. When the CdS cell is covered and the PIR sensor is off, the LED blinks.
const int LED=9;
const int PIR=2;
const int LIGHT=A0; //Lght Sensor on analog pin 0
const int MIN_LIGHT=200; //Minimum expected light value
const int MAX_LIGHT=900; //Maximum Expected Light value
int val = 0; //variable to hold the analog 
void setup()
{
 pinMode (LED, OUTPUT);
 pinMode (PIR, INPUT);
 Serial.begin(9600);
}
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
 Serial.println(val);
 if (val == 255){
   digitalWrite(LED, HIGH);
   delay(100);
   digitalWrite(LED, LOW);
   delay(100);
 }
 else{
   digitalWrite(LED, LOW);
 }
 if (digitalRead(PIR) == LOW)
 {
 digitalWrite(LED, LOW);
 }
 else
 {
 digitalWrite(LED, HIGH);
 }

No comments:

Post a Comment