Thursday, November 8, 2018

day 21 hw

/* - */
/* This program stores fingerprint information in a structure. */
/* It then references a function to compute the overall category.*/
#include <stdio.h>
#include <stdlib.h>
/* Define a structure for the fingerprint information. */
/* The order for fingertips is right hand, thumb to pinky, */
/* and left hand, thumb to pinky. The codes are L for loops, */
/* W for whorls, and A for arches. */
struct fingerprint
{
int ID_number;
double overall_category;
char fingertip[10];
};
int main(void)
{
/* Declare and initialize variables. */
struct fingerprint new_print;
double compute_category(struct fingerprint f);
/* Specify information for the new fingerprint. */
new_print.ID_number = 2491009;
new_print.overall_category = 0;
new_print.fingertip[0] = 'W';
new_print.fingertip[1] = 'L';
new_print.fingertip[2] = 'L';
new_print.fingertip[3] = 'W';
new_print.fingertip[4] = 'A';
new_print.fingertip[5] = 'L';
new_print.fingertip[6] = 'L';
new_print.fingertip[7] = 'W';
new_print.fingertip[8] = 'A';
new_print.fingertip[9] = 'L';
/* Reference function to compute overall category. */
new_print.overall_category = compute_category(new_print);
/* Print overall category computed by the function. */
printf("Fingerprint Analysis for ID: %i \n",
new_print.ID_number);
printf("Overall Category: %.2f \n",new_print.overall_category);
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------*/
/* This function computes the overall category */
/* for a fingerprint. */
double compute_category(struct fingerprint f)
{
/* Declare and initialize variables. */
double Rt=0, Ri=0, Rm=0, Rr=0, Rp=0, Lt=0, Li=0, Lm=0, Lr=0,
Lp=0, num, den;
int count = 0;
/* Set values based on whorls. */
if (f.fingertip[0] == 'W')
{
Rt = 16;
}
if (f.fingertip[1] == 'W')
{
Ri = 16;
}
if (f.fingertip[2] == 'W')
{
Rm = 8;
}
if (f.fingertip[3] == 'W')
{
Rr = 8;
}
if (f.fingertip[4] == 'W')
{
Rp = 4;
}
if (f.fingertip[5] == 'W')
{
Lt = 4;
}
if (f.fingertip[6] == 'W')
{
Li = 2;
}
if (f.fingertip[7] == 'W')
{
Lm = 2;
}
if (f.fingertip[8] == 'W')
{
Lr = 0;
}
if (f.fingertip[9] == 'W')
{
Lp = 0;
}
while (f.fingertip != 0)
{
printf("Numbers of whorls: %d", count);
f.fingertip /= 10;
count++;
}
/* Compute the numerator and denominator for overall category. */
num = Ri + Rr + Lt + Lm + Lp + 1;
den = Rt + Rm + Rp + Li + Lr + 1;
return num/den;
}


Tuesday, November 6, 2018

day 21 lab

/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define FILENAME "longstring.txt"
int main(void)
{
FILE *in_file = fopen(FILENAME, "r"); //read the textfile
/* Declare and initialize variables. */
int count=0;
char short_str[3],
c = fgetc(in_file);
/* Print Short String. */
printf("Enter the short_string\n");
scanf("%s", short_str);
if (in_file == NULL)//open file
{

printf("cannot open file \n");
}
printf("Your shortstring: %s\n", short_str);
/*Read data from file*/
c = fgetc(in_file);
while(c > 0)
{
printf ("%c", c);
c = fgetc(in_file);
}
fclose(in_file);
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

Monday, October 15, 2018

day 14 lab

//LCD with I2C
#include <Wire.h>
//Include the library code:
#include <LiquidCrystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int temp_address = 72; //1001000 written as decimal number
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 //Set up the LCDs number of columns and rows:
 lcd.begin(16, 2);
  //Create a Wire object
 Wire.begin();
}
void loop() {
   //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 int c = Wire.read();
 c = map(c, 22, 32, 22, 32);
 c = constrain(c, 22, 32);
 int buttonval = analogRead(A0);// let buttons read as variable
 int f = round(c*9.0/5.0 +32.0);//convert to farenheit
 // read the input on analog pin 0:
Serial.println(buttonval);
delay(1);
 Serial.println(c);
 delay(500); // delay in between reads
  Serial.println(f);
 delay(500); // delay in between reads
if ( buttonval == 481)
{
  switch(c){
  case 23:{
    lcd.setCursor(0,1);
    lcd.print("23C");
    break;
  }
  case 24:{
  lcd.setCursor(0,1);
    lcd.print("24C");
    break;
}
case 25:{
  lcd.setCursor(0,1);
    lcd.print("25C ");
    break;
}
case 26:{
  lcd.setCursor(0,1);
    lcd.print("26C");
    break;
}
}
}
else if ( buttonval == 0){
  switch(f){
  case 73:{
    lcd.setCursor(0,1);
    lcd.print("73F  ");
    break;
  }
  case 75:{
  lcd.setCursor(0,1);
    lcd.print("75F  ");
    break;
}
case 77:{
  lcd.setCursor(0,1);
    lcd.print("77F  ");
    break;
}
case 79:{
  lcd.setCursor(0,1);
    lcd.print("79F  ");
    break;
}
}
}
return;
}

day 14 assignment

/* This program demonstrates the relationship between /
/* variables and addresses. */
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
int a=1, b=2;
/*Print the contents and addresses of a and b. */
printf("a = %d; address of a = %u \n",a,&a);
printf("b = %d; address of b = %u \n",b,&b);
/* Exit program.*/
return 0;

}

Thursday, October 11, 2018

day 13 assignment

/*?????????????????????????????????????????????????????????????*/
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
int numpeaks=0;
int numvalleys=0;
double elevation[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) &&
(elevation[i+1][j]<elevation[i][j]) &&
(elevation[i][j-1]<elevation[i][j]) &&
(elevation[i][j+1]<elevation[i][j])){
numpeaks = numpeaks +1;
numvalleys =
printf("Peak at row: %d column: %d \n",i,j);
}
printf("peak count is %i \n", numpeaks);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */

}

day 13 lab

//Reads Temp from I2C temperature sensor and prints it on the serialport
//Include Wire I2C library
#include <Wire.h>
#include "pitches.h"
const int speaker=9;
int temp_address = 72; //1001000 written as decimal number
int notes[] = { NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1};
int value[] = { 22, 23, 24, 25, 26} ;
int d;
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
}
void loop()
{
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 int c = Wire.read();
 c = map(c, 22, 32, 22, 32);
 c = constrain(c, 22, 32);
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.println(c);
 //Serial.print("C ");
 //Serial.print(f);
 //Serial.println("F");
 delay(500);
 switch ( c )
 {
 case 26:
  {
    tone(speaker, NOTE_DS1);
    break;
  }
  case 25:
  {
    tone(speaker, NOTE_D1);
    break;
  }
  case 24:
 {
  tone(speaker, NOTE_CS1);
  break;
  }
  case 23:
  {
     tone(speaker, NOTE_C1);
     break;
  }
  case 22:
  {
    tone(speaker, NOTE_B0);
    break;
  }
 }
return;
}

Thursday, October 4, 2018

day 12 lab


#include <Servo.h>
const int SERVO =9;
const int IR =0;
Servo myServo;
int pos = 0;    // variable to store the servo position
void setup()
{
  Serial.begin(9600);
  myServo.attach(SERVO); // attach the servo
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myServo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  if (analogRead(IR) > 200)
  {
    delay(500);
  }
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myServo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
   if (analogRead(IR) > 200)
  {
    delay(500);
  }
  }
}

day 11 lab


//Single Character Control of an LED
#include <Servo.h>
const int LED=4;
const int servo=9;
int data; //Holds incoming character
int pos = 0;
Servo myServo;
void setup()
{
 Serial.begin(9600); //Serial Port at 9600 baud
 myServo.attach(servo); // attach servo
 pinMode(LED, OUTPUT); // set led to output
}
void loop()
{
  Serial.print(data);
  delay(100);
 //Only act when data is available in the buffer
while (Serial.available() > 0)
 {
 data = Serial.parseInt(); //Read byte of data
 //Turn LED on
  if (data <= 180)
 {
 myServo.write(data);
 Serial.println(data);
 }
 //Turn on LED if over 180
 else if (data > 180)
 {
 digitalWrite(LED, HIGH);
 Serial.println(data);
 }
 }
}

day 11 assignment

Tuesday, October 2, 2018

day 10 assignment

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
/* Declare variables and function prototype. */
//unsigned int seed;
double interval_start, interval_end, k;
double rand_float(double interval_start,double interval_end);
/* Get seed value and interval limits. */
//printf("Enter a positive integer seed value: \n");
//scanf("%u",&seed);
srand(101);
printf("Enter integer limits a and b (a<b): \n");
scanf("%lf %lf",&interval_start,&interval_end);
/* Generate and print ten random numbers. */
printf("Random Numbers: \n");
for (k=1; k<=10; k++)
  printf("%lf \n",rand_float(interval_start,interval_end));
/* Exit program. */
return 0;
}
/* This function generates a random integer */
/* between specified limits a and b (a<b). */
double rand_float(double interval_start,double interval_end)
{
return (rand()%32767/32767.0)*(interval_end - interval_start + 1) + interval_start;
}

Thursday, September 20, 2018

day 7 lab

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

#define melodyPin 8
//Mario main theme melody
int melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo
int tempo[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};
//Underworld melody
int underworld_melody[] = {
  NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  NOTE_AS3, NOTE_AS4, 0,
  0,
  NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  NOTE_AS3, NOTE_AS4, 0,
  0,
  NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  NOTE_DS3, NOTE_DS4, 0,
  0,
  NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  NOTE_DS3, NOTE_DS4, 0,
  0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  NOTE_CS4, NOTE_DS4,
  NOTE_DS4, NOTE_GS3,
  NOTE_G3, NOTE_CS4,
  NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  NOTE_GS4, NOTE_DS4, NOTE_B3,
  NOTE_AS3, NOTE_A3, NOTE_GS3,
  0, 0, 0
};
//Underwolrd tempo
int underworld_tempo[] = {
  12, 12, 12, 12,
  12, 12, 6,
  3,
  12, 12, 12, 12,
  12, 12, 6,
  3,
  12, 12, 12, 12,
  12, 12, 6,
  3,
  12, 12, 12, 12,
  12, 12, 6,
  6, 18, 18, 18,
  6, 6,
  6, 6,
  6, 6,
  18, 18, 18, 18, 18, 18,
  10, 10, 10,
  10, 10, 10,
  3, 3, 3
};

void setup(void)
{
  pinMode(8, OUTPUT);//buzzer
  pinMode(10, OUTPUT);//led indicator when singing a note

}
void loop()
{
  //sing the tunes
  sing(1);
  sing(1);
  sing(2);
}
int song = 0;

void sing(int s) {
  // iterate over the notes of the melody:
  song = s;
  if (song == 2) {
    Serial.println(" 'Underworld Theme'");
    int size = sizeof(underworld_melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000 / underworld_tempo[thisNote];

      buzz(melodyPin, underworld_melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(melodyPin, 0, noteDuration);

    }

  } else {

    Serial.println(" 'Mario Theme'");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000 / tempo[thisNote];

      buzz(melodyPin, melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(melodyPin, 0, noteDuration);

    }
  }
}

void buzz(int targetPin, long frequency, long length) {
  digitalWrite(10, HIGH);
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }
  digitalWrite(10, LOW);

}

Tuesday, September 18, 2018

day 6 lab

const int LED=9; //Red LED on pin 9 (PWM)
const int LIGHT=0; //Lght Sensor on analog pin 0
const int MIN_LIGHT=200; //Minimum expected light value
const int MAX_LIGHT=900; //Maximum Expected Light value
const int PIR=2;//The PIR is connected to pin 2
const int max_thresh = 10; // Maximum threshold
int val = 0; //variable to hold the analog reading
void setup()
{
 pinMode(LED, OUTPUT); //Set LED pin as output
 pinMode (PIR, INPUT);
 Serial.begin(9600); //Start Serial Communication
}
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); //Print it to the serial port
 delay(500);
 int PIRcount = digitalRead(PIR);
  if (digitalRead(PIR) == LOW && val == 255)
  {
    if( PIRcount < max_thresh);
    {
     digitalWrite(LED,HIGH); //Set the LED On
     delay(400); //Wait for 100 ms
     digitalWrite(LED,LOW); //Set the LED Off
     delay(80); //Wait for 10 second
     PIRcount++;
    }
  }
  else if(digitalRead(PIR) == HIGH)
  {
    digitalWrite(LED,HIGH); //Set the LED on
  }
  else if (digitalRead(PIR) == LOW && val <= 96)
  {
    digitalWrite(LED,LOW); //Set the LED off
  }
 }

day 6 assignment

/* 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;
double gc_distance(double lat1,double long1,
double lat2,double long2);
/* Get locations of two points. */
printf("Enter latitude north and longitude west ");
printf("for location 1: \n");
scanf("%lf %lf",&lat1,&long1);
printf("Enter latitude north and longitude west ");
printf("for location 2: \n");
scanf("%lf %lf",&lat2,&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. */
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));
/* Compute and return great circle distance. */
return gamma*rho;
}

Wednesday, September 12, 2018

day 5 assigment

#include <stdio.h>
#include <math.h>
int main(void)
{
/*Declare variables.*/
double x, t_a, t_b, a, b, f_a, f_b, t_c, f_1, f_2,
t, steps, max, sum, ti, ws1, ws2, Pi= 3.14159, min;
/* User input.*/
printf("Use seconds for period. \n");
printf("Use feet for wave height. \n");
printf("Enter period and wave height for wave 1. \n");
scanf("%lf %lf", &t_a, &a);
printf("Enter period and wave height for wave. \n");
scanf("%lf %lf", &t_b, &b);
printf("Enter desired time. \n");
scanf("%lf", &x);
//* Use given equation to compute wavelength 1
f_a = 5.13*(t_a*t_a);
//* Use given equation to compute wavelength 2
f_b = 5.13*(t_b*t_b);
/* Print Wavelength 1. */
printf("Wavelength 1 in ft: %4.2f \n", f_a);
/* Print Wavelength 2. */
printf("Wavelength 2 in ft: %4.2f \n", f_b);
t_c = t_a*t_b;
ti = t_c/200;
max = 0;
ti = 0;
f_1 = 1/t_a;
f_2 = 1/t_b;
steps = 0;
while (steps <= 199){
t = (steps*x);
ws1 = (0.5 * a)*(sin(2 * Pi * f_1 * t));
ws2 = (0.5 * b)*(sin(2 * Pi * f_2 * t));
sum = ws1 + ws2;
if (sum > max)
max = sum;
if (sum < min)
min = sum;
steps++;}
printf("Wavemax in ft: %4.2f \n",max*2);
printf("maximum crest in ft: %4.2f \n", max);
printf("minimum trough in ft: %4.2f \n", min);
return 0;
}

Tuesday, September 11, 2018

day 5 lab

//Potentiometer Reading Program
const int POT=0; //Pot on analog pin 0
int val = 0; //variable to hold the analog reading from the POT
void setup()
{
 Serial.begin(9600); //Start Serial Communication
}
void loop()
{
 val = analogRead(POT); //Read one value from the POT
 Serial.println(val); //Print it to the serial port
 delay(500);
}

//Temperature Reading LED program
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int POT=0; //Pot on analog pin 0
int val = 0; //variable to hold the analog reading from the POT
void setup()
{
 Serial.begin(9600); //Start Serial Communication
}
void loop()
{
  double mV= val*5,  temp= abs(mV - 500)/10;
  val = analogRead(POT); //Read one value from the POT
  Serial.println("degrees C:" );
  Serial.println(temp); //Print it to the serial port
  delay(500);
 if (val<138)
 {
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, HIGH);
 }
   else if (val>148)
   {
   digitalWrite(RLED, HIGH);
   digitalWrite(GLED, LOW);
   digitalWrite(BLED, LOW);
   }
    else if(val=143)
    {
   digitalWrite(RLED, LOW);
   digitalWrite(GLED, HIGH);
   digitalWrite(BLED, LOW);
 }
 delay(1);
return;}

Day 4 Assigntment

const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
const int BUTTON2=3; //The Button is connected to pin 2
boolean lastButton1 = LOW; //Last Button State
boolean currentButton1 = LOW; //Current Button State
const int BUTTON3=4; //The Button is connected to pin 2
boolean lastButton2 = LOW; //Last Button State
boolean currentButton2 = LOW; //Current Button State
const int BUTTON4=5; //The Button is connected to pin 2
boolean lastButton3 = LOW; //Last Button State
boolean currentButton3 = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
   pinMode (BLED, OUTPUT); //Set Blue LED as Output
 pinMode (GLED, OUTPUT); //Set Green LED as Output
 pinMode (RLED, OUTPUT); //Set Red LED as Output
 pinMode (BUTTON, INPUT); //Set button as input 
  pinMode (BUTTON2, INPUT); //Set button as input 
   pinMode (BUTTON3, INPUT); //Set button as input 
    pinMode (BUTTON4, INPUT); //Set button as input 
}
 boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON); //Read the button state
 if (last != current) //if it's different...
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON); //read it again
 }
 return current; //return the current value
}
boolean debounce1(boolean Last)
{
 boolean current1 = digitalRead(BUTTON2); //Read the button state
 if (Last != current1) //if it's different...
 {
 delay(5); //wait 5ms
 current1 = digitalRead(BUTTON2); //read it again
 }
 return current1; //return the current value
}
boolean debounce2(boolean Last1)
{
 boolean current2 = digitalRead(BUTTON3); //Read the button state
 if (Last1 != current2) //if it's different...
 {
 delay(5); //wait 5ms
 current2 = digitalRead(BUTTON3); //read it again
 }
 return current2; //return the current value
}
boolean debounce3(boolean Last2)
{
 boolean current3 = digitalRead(BUTTON4); //Read the button state
 if (Last2 != current3) //if it's different...
 {
 delay(5); //wait 5ms
 current3 = digitalRead(BUTTON4); //read it again
 }
 return current3; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int button)
{
switch (button)
{
 //teal
 case 1:
analogWrite(RLED, 0);
analogWrite(GLED, 128);
 analogWrite(BLED, 128);
 break;
 //orange
 case 2:
analogWrite(RLED, 255);
 analogWrite(GLED, 165);
analogWrite(BLED, 0);
break;
 //white
 case 3:
 analogWrite(RLED, 255);
 analogWrite(GLED, 255);
 analogWrite(BLED, 255);
 break;
 //OFF (mode = 0)
 default:
 analogWrite(RLED, 0);
 analogWrite(GLED, 0);
 analogWrite(BLED, 0);
}
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 ledMode++; //increment the LED value
 }
 lastButton = currentButton; //reset button value
  if (ledMode == 1) ledMode = 0;
 setMode(ledMode); //change the LED state
 
 currentButton1 = debounce1(lastButton1); //read debounced state
 if (lastButton1 == LOW && currentButton1 == HIGH) //if it was pressed...
 {
 ledMode++; //increment the LED value
 }
 lastButton1 = currentButton1; //reset button value
 //if you've cycled through the different options,
 //reset the counter to 0
 if (ledMode == 2) ledMode = 0;
 setMode(ledMode); //change the LED state
 
currentButton2 = debounce2(lastButton2); //read debounced state
 if (lastButton2 == LOW && currentButton2 == HIGH) //if it was pressed...
 {
 ledMode++; //increment the LED value
 }
 lastButton2 = currentButton2; //reset button value
 //if you've cycled through the different options,
 //reset the counter to 0
 if (ledMode == 3) ledMode = 0;
 setMode(ledMode); //change the LED state
  
currentButton3 = debounce3(lastButton3); //read debounced state
 if (lastButton3 == LOW && currentButton3 == HIGH) //if it was pressed...
 {
 ledMode++; //increment the LED value
 }
 lastButton3 = currentButton3; //reset button value
 //if you've cycled through the different options,
 //reset the counter to 0
 if (ledMode == 4) ledMode = 0;
 setMode(ledMode); //change the LED state
}

Thursday, September 6, 2018

Day3 Lab

const int LED=9; //The LED is connected to pin 9
const int BUTTON=2; //The Button is connected to pin 2
const int LED2=10; //The LED is connected to pin 10
const int BUTTON2=4; //The Button is connected to pin 3
boolean lastButton = LOW; //Variable containing the previous button state
boolean currentButton = LOW; //Variable containing the current button state
boolean ledOn = false; //The present state of the LED (on/off)
boolean lastButton2 = LOW; //Variable containing the previous button state
boolean currentButton2 = LOW; //Variable containing the current button state
boolean led2On = false; //The present state of the LED (on/off)
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
 pinMode (BUTTON, INPUT); //Set button as input (not required)
  pinMode (LED2, OUTPUT); //Set the LED2 pin as an output
 pinMode (BUTTON2, INPUT); //Set button2 as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON);
 if (last != current) //if it's different…
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON); //read it again
 return current; //return the current value
}
}
boolean deboucnce(boolean Last)
{
 boolean current2 = digitalRead(BUTTON2);//Read the button2 state
 if (Last != current2) //if it's different…
 {
  delay(5); // wait 5ms
  current2 = digitalRead(BUTTON2); //read it again
  return current2; //return the current value
}
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 ledOn = !ledOn; //toggle the LED value
 }
 lastButton = currentButton; //reset button value
 digitalWrite(LED, ledOn); //change the LED state
 {
 currentButton2 = debounce(lastButton2); //read debounced state
  if (lastButton2 == LOW && currentButton2 == HIGH) //if it was pressed...
{
  led2On =!led2On; // toggle the LED2 value
  }
 lastButton2 = currentButton2; //reset button value
  digitalWrite(LED2, led2On); //change the LED state
}
}

Wednesday, September 5, 2018

Day 3 Assignment

/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This program uses linear interpolation to */
/* compute the freezing temperature of seawater. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c, t_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new salinity: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
// Use value of new freezing temperature to convert into Centigrade.
t_c = (5*f_b/9) - 32;
/* Print new freezing temperature. */
printf("New freezing temperature in degrees C: %4.1f \n",t_c);
return 0; /* Exit program. */
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/

Thursday, August 30, 2018

Lab Day 2

void setup() {
pinMode(10,OUTPUT); //Initialize Pin
pinMode(9,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
digitalWrite(9,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(9,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(10,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(10,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(11,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(11,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(12,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(12,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(12,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(12,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(11,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(11,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(10,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(10,LOW); //Set the LED Off
delay(10); //Wait for 10 second
digitalWrite(9,HIGH); //Set the LED On
delay(80); //Wait for 100 ms
digitalWrite(9,LOW); //Set the LED Off
delay(10); //Wait for 10 second
}

Wednesday, August 29, 2018

Day 1 Assignment

#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
double meters=25, a=1609.344, miles;
miles = meters/a;
/* Print miles. */
printf("The conversion from 25 meters to miles is"
"%5.2f \n",miles);
/* Exit program. */
return 0;
}
/////////////////////////////////////////////////////////////////////////
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
double b=9/5,a=273.15, Tc=20, rankine;
rankine = (Tc + a )*b;
/* Print rankine. */
printf("The conversion from 20 degrees celsius to rankine is"
"%5.2f \n",rankine);
/* Exit program. */
return 0;
}
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
double a=2,r=5, an=65,Pi,d,radius, area;
Pi= 3.14159;
d= an*Pi;
radius= r*r;
area= radius*(d)/a;
/* Print area. */
printf("The area of sector of a circle when d is 65 degrees equals to "
"%5.2f \n",area);
/* Exit program. */
return 0;
}
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
double a=9,b=12, pi=3.14159, area;
area =pi*a*b;
/* Print area. */
printf("The area of the ellipses with semiaxes a and b equals to "
"%5.2f \n",area);
/* Exit program. */
return 0;
}