/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#include <math.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
double elevation[N][N], Pcount = 0, Vcount = 0, valley, peak = 0, temp_peak, Peak1Dis;
FILE *grid;
/* Read information from a data file. */
grid = fopen("grid1.txt","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");
printf("Bottom left point(BLF) defined as row 5, column 0 \n\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]))
{
++Pcount;
printf("Peak at row: %d column: %d \n",i,j);
Peak1Dis = 100* sqrt(((5-i)*(5-i))+(j*j));
printf("Distance from BLP: %.2lf ft\n\n", Peak1Dis);
if (elevation[i][j] > peak)
{
peak = elevation[i][j];
}
}
printf("\nNumber of peaks: %lf\n\n", Pcount);
/* Determine and print valley locations. */
valley = elevation[i][j]; //initializes a variable to check lowest elevation against
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])
&& (elevation[i-1][j-1]>elevation[i][j]) && (elevation[i-1][j+1]>elevation[i][j]) && (elevation[i+1][j-1]>elevation[i][j]) && (elevation[i+1][j+1]>elevation[i][j]))
{
++Vcount;
printf("Valley at row: %d column: %d \n",i,j);
if (elevation[i][j] < valley)
{
valley = elevation[i][j];
}
}
printf("\nNumber of valleys: %lf\n", Vcount);
printf("\nHighest elevation: %lf\n", peak);
printf("Lowest elevation: %lf\n", valley);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
Thursday, December 13, 2018
sesimic
/* This program reads a seismic data file and then */
/* determines the times of possible seismic events. */
#include <stdio.h>
#define FILENAME "seismic1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare variables and function prototypes. */
int k, npts, short_window, long_window, counter = 0;
double sensor[MAX_SIZE], time_incr, short_power, threshold = 0,
long_power, ratio;
FILE *file_ptr;
double power_w(double *ptr,int n);
/* Read sensor data file. */
file_ptr = fopen(FILENAME,"r");
if (file_ptr == NULL)
printf("Error opening input file. \n");
else
{
fscanf(file_ptr,"%d %lf",&npts,&time_incr);
if (npts > MAX_SIZE)
printf("Data file too large for array. \n");
else
{
/* Read data into an array. */
for (k=0; k<=npts-1; k++)
fscanf(file_ptr,"%lf",&sensor[k]);
/* Read window sizes from the keyboard. */
printf("Enter number of points for short window: \n");
scanf("%d",&short_window);
printf("Enter number of points for long window: \n");
scanf("%d",&long_window);
while(threshold <= 1)
{
printf("Enter threshold value: \n");
scanf("%lf",&threshold);
}
/* Compute power ratios and search for events. */
for (k=long_window-1; k<=npts-1; k++)
{
short_power = power_w(&sensor[k],short_window);
long_power = power_w(&sensor[k],long_window);
ratio = short_power/long_power;
if (ratio > threshold)
{
printf("Possible event at %f seconds \n", time_incr*k);
counter++;
}
}
printf("Number of events: %d \n", counter);
/* Close file. */
fclose(file_ptr);
}
}
return 0;
}
/*????????????????????????????????????????????????????????????-*/
/* This function computes the average power in a specified */
/* window of a double array. */
double power_w(double *ptr, int n)
{
/* Declare and initialize variables. */
int k;
double xsquare=0;
/* Compute sum of values squared in the array x. */
for (k=0; k<=n-1; k++)
xsquare += *(ptr-k)*(*(ptr-k));
/* Return the average squared value. */
return xsquare/n;
}
/* determines the times of possible seismic events. */
#include <stdio.h>
#define FILENAME "seismic1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare variables and function prototypes. */
int k, npts, short_window, long_window, counter = 0;
double sensor[MAX_SIZE], time_incr, short_power, threshold = 0,
long_power, ratio;
FILE *file_ptr;
double power_w(double *ptr,int n);
/* Read sensor data file. */
file_ptr = fopen(FILENAME,"r");
if (file_ptr == NULL)
printf("Error opening input file. \n");
else
{
fscanf(file_ptr,"%d %lf",&npts,&time_incr);
if (npts > MAX_SIZE)
printf("Data file too large for array. \n");
else
{
/* Read data into an array. */
for (k=0; k<=npts-1; k++)
fscanf(file_ptr,"%lf",&sensor[k]);
/* Read window sizes from the keyboard. */
printf("Enter number of points for short window: \n");
scanf("%d",&short_window);
printf("Enter number of points for long window: \n");
scanf("%d",&long_window);
while(threshold <= 1)
{
printf("Enter threshold value: \n");
scanf("%lf",&threshold);
}
/* Compute power ratios and search for events. */
for (k=long_window-1; k<=npts-1; k++)
{
short_power = power_w(&sensor[k],short_window);
long_power = power_w(&sensor[k],long_window);
ratio = short_power/long_power;
if (ratio > threshold)
{
printf("Possible event at %f seconds \n", time_incr*k);
counter++;
}
}
printf("Number of events: %d \n", counter);
/* Close file. */
fclose(file_ptr);
}
}
return 0;
}
/*????????????????????????????????????????????????????????????-*/
/* This function computes the average power in a specified */
/* window of a double array. */
double power_w(double *ptr, int n)
{
/* Declare and initialize variables. */
int k;
double xsquare=0;
/* Compute sum of values squared in the array x. */
for (k=0; k<=n-1; k++)
xsquare += *(ptr-k)*(*(ptr-k));
/* Return the average squared value. */
return xsquare/n;
}
Weather Ballon
#include <stdio.h>
#include <math.h>
#define FILENAME "balloon1.txt"
int main(void)
{
/* Declare variables. */
double start_time_hour, start_time_minute, end_time_hour, end_time_minute, time_passed_hour, time_passed_minute;
double incre_min, total_time, time_incre, alt, vel, max_alt, max_time_of_alt;
double Ptime, Palt, Pvel, datapoints;
FILE *balloon;
/* Open output file. */
balloon = fopen(FILENAME,"w");
/* Get user input from the keyboard. */
do
{
printf("Enter start time of weather balloon release in hrs then min. \n");
printf("start time: \n");
scanf("%lf %lf", &start_time_hour, &start_time_minute);
printf("\nEnter final time of weather balloon traveling in hrs then min. \n");
printf("final time: \n");
scanf("%lf %lf",&end_time_hour, &end_time_minute);
/* Determine length of time passed based on user inputted times */
time_passed_hour = end_time_hour - start_time_hour;
time_passed_minute = end_time_minute - start_time_minute;
total_time = time_passed_hour + (time_passed_minute / 60);
/* Checks data entered to see if valid */
if (end_time_hour < start_time_hour)
{
printf("\nStart time is greater than final time. Please reenter time values again.\n\n");
}
if (end_time_hour > 48)
{
printf("\nFinal time value cannot be greater than 48 hours. Please reenter time values again.\n\n");
}
} while (end_time_hour < start_time_hour || (end_time_hour > 48));
printf("\nEnter time increment in minutes.\n");
printf("time increment: \n");
scanf("%lf",&incre_min);
double unceil_step = (total_time * 60) / incre_min;
double steps = ceil((total_time * 60) / incre_min);
/* Determine altitudes and velocites of balloon */
//fprintf(balloon,"(time(hrs)) (alitude(m)) (velocity(m/s))\n");
fprintf(balloon,"%lf\n", steps + 1 );
for (double k = 0; k <= steps; k++)
{
double T = (k*(total_time/unceil_step)) + start_time_hour;
alt = -0.12*pow(T,4) + 12*pow(T,3) - 380*pow(T,2) + 4100*T + 220;
vel = -0.48*pow(T,3) + 36*pow(T,2) - 760*T + 4100;
if(alt > max_alt)
{
max_alt = alt;
max_time_of_alt = T;
};
fprintf(balloon,"%.6lf %.6lf %.6lf\n", T, alt, vel/3600);
}
fprintf(balloon, "\nmax alitude: %.3lf m\n", max_alt);
fprintf(balloon, "\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
fclose(balloon);
/* Close file and exit . */
balloon = fopen("balloon1.txt","r");
fscanf(balloon," %lf", &datapoints);
printf("\nTIME ALT VEL\n");
for (double w = 1; w <= datapoints; w++ )
{
fscanf(balloon,"%lf %lf %lf",&Ptime,&Palt,&Pvel);
printf("%1.6lf %.6lf %.6lf\n",Ptime,Palt,Pvel);
}
printf("\nmax alitude: %.3lf m\n", max_alt);
printf("\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
return 0;
}
#include <math.h>
#define FILENAME "balloon1.txt"
int main(void)
{
/* Declare variables. */
double start_time_hour, start_time_minute, end_time_hour, end_time_minute, time_passed_hour, time_passed_minute;
double incre_min, total_time, time_incre, alt, vel, max_alt, max_time_of_alt;
double Ptime, Palt, Pvel, datapoints;
FILE *balloon;
/* Open output file. */
balloon = fopen(FILENAME,"w");
/* Get user input from the keyboard. */
do
{
printf("Enter start time of weather balloon release in hrs then min. \n");
printf("start time: \n");
scanf("%lf %lf", &start_time_hour, &start_time_minute);
printf("\nEnter final time of weather balloon traveling in hrs then min. \n");
printf("final time: \n");
scanf("%lf %lf",&end_time_hour, &end_time_minute);
/* Determine length of time passed based on user inputted times */
time_passed_hour = end_time_hour - start_time_hour;
time_passed_minute = end_time_minute - start_time_minute;
total_time = time_passed_hour + (time_passed_minute / 60);
/* Checks data entered to see if valid */
if (end_time_hour < start_time_hour)
{
printf("\nStart time is greater than final time. Please reenter time values again.\n\n");
}
if (end_time_hour > 48)
{
printf("\nFinal time value cannot be greater than 48 hours. Please reenter time values again.\n\n");
}
} while (end_time_hour < start_time_hour || (end_time_hour > 48));
printf("\nEnter time increment in minutes.\n");
printf("time increment: \n");
scanf("%lf",&incre_min);
double unceil_step = (total_time * 60) / incre_min;
double steps = ceil((total_time * 60) / incre_min);
/* Determine altitudes and velocites of balloon */
//fprintf(balloon,"(time(hrs)) (alitude(m)) (velocity(m/s))\n");
fprintf(balloon,"%lf\n", steps + 1 );
for (double k = 0; k <= steps; k++)
{
double T = (k*(total_time/unceil_step)) + start_time_hour;
alt = -0.12*pow(T,4) + 12*pow(T,3) - 380*pow(T,2) + 4100*T + 220;
vel = -0.48*pow(T,3) + 36*pow(T,2) - 760*T + 4100;
if(alt > max_alt)
{
max_alt = alt;
max_time_of_alt = T;
};
fprintf(balloon,"%.6lf %.6lf %.6lf\n", T, alt, vel/3600);
}
fprintf(balloon, "\nmax alitude: %.3lf m\n", max_alt);
fprintf(balloon, "\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
fclose(balloon);
/* Close file and exit . */
balloon = fopen("balloon1.txt","r");
fscanf(balloon," %lf", &datapoints);
printf("\nTIME ALT VEL\n");
for (double w = 1; w <= datapoints; w++ )
{
fscanf(balloon,"%lf %lf %lf",&Ptime,&Palt,&Pvel);
printf("%1.6lf %.6lf %.6lf\n",Ptime,Palt,Pvel);
}
printf("\nmax alitude: %.3lf m\n", max_alt);
printf("\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
return 0;
}
DNA strings
/*??????????????????????????????????????????????????????????????-*/
/* 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 long_leng 26
#define short_leng 3
int main(void)
{
/* Declare and initialize variables. */
int count=0;
printf("Enter sequence for long string DNA: ");
char long_str[long_leng];
scanf("%s", long_str);
printf("Enter sequence for short string DNA: ");
char short_str[short_leng];
scanf("%s", short_str);
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
printf("\nLong String of DNA: ");
printf(long_str);
printf("\nShort String of DNA: ");
printf(short_str);
/* Exit program. */
return 0;
}
/* 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 long_leng 26
#define short_leng 3
int main(void)
{
/* Declare and initialize variables. */
int count=0;
printf("Enter sequence for long string DNA: ");
char long_str[long_leng];
scanf("%s", long_str);
printf("Enter sequence for short string DNA: ");
char short_str[short_leng];
scanf("%s", short_str);
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
printf("\nLong String of DNA: ");
printf(long_str);
printf("\nShort String of DNA: ");
printf(short_str);
/* Exit program. */
return 0;
}
structures_waves2
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr;
char location[20];
double fatalities, max_height;
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
{
FILE *waves2;
/* Read and print information from the file. */
waves2 = fopen(FILENAME,"r");
if (waves2 == NULL)
printf("Error opening data file. /n");
else
{
while (fscanf(waves2,"%d %d %d %s %lf %lf ",&t[k].mo,&t[k].da,
&t[k].yr, t[k].location, &t[k].fatalities,&t[k].max_height,) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
//ave = sum/npts;
printf("Summary Information for Tsunamis /n");
printf("Maximum Wave Height (in feet): %.2f /n",max*3.28);
printf("Average Wave Height (in feet): %.2f /n",ave*3.28);
printf("Tsunamis with greater than average heights: /n");
for (k=0; k <=npts-1; k++)
printf("%s /n",t[k].location);
fclose(waves2);
}
return 0;
}
}
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr;
char location[20];
double fatalities, max_height;
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
{
FILE *waves2;
/* Read and print information from the file. */
waves2 = fopen(FILENAME,"r");
if (waves2 == NULL)
printf("Error opening data file. /n");
else
{
while (fscanf(waves2,"%d %d %d %s %lf %lf ",&t[k].mo,&t[k].da,
&t[k].yr, t[k].location, &t[k].fatalities,&t[k].max_height,) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
//ave = sum/npts;
printf("Summary Information for Tsunamis /n");
printf("Maximum Wave Height (in feet): %.2f /n",max*3.28);
printf("Average Wave Height (in feet): %.2f /n",ave*3.28);
printf("Tsunamis with greater than average heights: /n");
for (k=0; k <=npts-1; k++)
printf("%s /n",t[k].location);
fclose(waves2);
}
return 0;
}
}
handprint
#include <iostream>
#include <cmath>
using namespace std;
#define FILENAME "handprint.txt"
int main(void)
{
const int N=20;
int k=0, npts=N;
double y[N];
//Declare and initialize variables.
//double unknown[5]={5.4,7.2,7.9,7.4,5.1},
double known[5]={6.2,7.0,8.0,7.4,5.8},
//test[5]={};
double distance(double hand_1[5],double hand_2[5]);
ifstream handprint;
// Open file, read data into an array.
handprint.open(FILENAME);
if (handprint.fail())
cout << "Error opening input file." << endl;
else
{
while (!handprint.eof())
{
handprint >> y[k];
k++;
}
npts = k;
// Find and print the maximum value.
// Compute and print distance.
cout << "Enter distances for each digit starting with the thumb:" << endl;
/*for (int i = 0; i < 5; i++)
{cin >> test[i];}*/
cout << "Distance: " << distance(y,known) << endl;
// Exit program.
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double hand_1[5],double hand_2[5])
{
// Declare variables.
int k;
double sum=0;
// Compute sum of absolute value differences.
for (k=0; k<=4; k++)
sum = sum + fabs(hand_1[k]-hand_2[k]);
// Return distance value.
return sum;
}
#include <cmath>
using namespace std;
#define FILENAME "handprint.txt"
int main(void)
{
const int N=20;
int k=0, npts=N;
double y[N];
//Declare and initialize variables.
//double unknown[5]={5.4,7.2,7.9,7.4,5.1},
double known[5]={6.2,7.0,8.0,7.4,5.8},
//test[5]={};
double distance(double hand_1[5],double hand_2[5]);
ifstream handprint;
// Open file, read data into an array.
handprint.open(FILENAME);
if (handprint.fail())
cout << "Error opening input file." << endl;
else
{
while (!handprint.eof())
{
handprint >> y[k];
k++;
}
npts = k;
// Find and print the maximum value.
// Compute and print distance.
cout << "Enter distances for each digit starting with the thumb:" << endl;
/*for (int i = 0; i < 5; i++)
{cin >> test[i];}*/
cout << "Distance: " << distance(y,known) << endl;
// Exit program.
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double hand_1[5],double hand_2[5])
{
// Declare variables.
int k;
double sum=0;
// Compute sum of absolute value differences.
for (k=0; k<=4; k++)
sum = sum + fabs(hand_1[k]-hand_2[k]);
// Return distance value.
return sum;
}
classes "date"
#include <iostream>
#include <cmath>
using namespace std;
class date
{
// prototypes for public members.
public:
void input();
void print_slash();
void print_comma();
// Declare private data members.
private:
int day, month, year;
};
void date::input()
{
cin >> day >> month >> year;
}
// This function prints the date as month/day/year.
void date::print_slash()
{
cout << month << "/" << day << "/" << year << "\n";
}
// This function prints the date as month day, year.
void date::print_comma()
{
switch(month)
{
case 1:
{cout << "Jan" << " " << day << "," << year << "\n";
break;
}
case 2:
{cout << "Feb" << " " << day << ","<< year << "\n";
break;
}
case 3:
{cout << "Mar" << " " << day << ","<< year << "\n";
break;
}
case 4:
{cout << "Apr" << " " << day << ","<< year << "\n";
break;}
case 5:
{cout << "May" << " " << day << ","<< year << "\n";
break;}
case 6:
{cout << "Jun" << " " << day << ","<< year << "\n";
break;
}
case 7:
{cout << "Jul" << " " << day << ","<< year << "\n";
break;}
case 8:
{cout << "Aug" << " " << day << ","<< year << "\n";
break;
}
case 9:
{cout << "Sep" << " " << day << ","<< year << "\n";
break;
}
case 10:
{cout << "Oct" << " " << day << ","<< year << "\n";
break;
}
case 11:
{cout << "Nov" << " " << day << ","<< year << "\n";
break;
}
case 12:
{cout << "Dec" << " " << day << ","<< year << "\n";
break;
}
}
}
_________________________________________________________________________________
#include <iostream>
#include <cmath>
#include "date.h"
using namespace std;
// Declare and initialize variables.
int main(void)
{
date date;
// Read input date.
cout << "Please enter the date as day, month, year" << endl;
date.input();
// Print date in comma and slash form.
cout << "Date in slash form:" << endl;
date.print_slash();
cout << "Date in comma form:" << endl;
date.print_comma();
// Exit program.
return 0;
}
#include <cmath>
using namespace std;
class date
{
// prototypes for public members.
public:
void input();
void print_slash();
void print_comma();
// Declare private data members.
private:
int day, month, year;
};
void date::input()
{
cin >> day >> month >> year;
}
// This function prints the date as month/day/year.
void date::print_slash()
{
cout << month << "/" << day << "/" << year << "\n";
}
// This function prints the date as month day, year.
void date::print_comma()
{
switch(month)
{
case 1:
{cout << "Jan" << " " << day << "," << year << "\n";
break;
}
case 2:
{cout << "Feb" << " " << day << ","<< year << "\n";
break;
}
case 3:
{cout << "Mar" << " " << day << ","<< year << "\n";
break;
}
case 4:
{cout << "Apr" << " " << day << ","<< year << "\n";
break;}
case 5:
{cout << "May" << " " << day << ","<< year << "\n";
break;}
case 6:
{cout << "Jun" << " " << day << ","<< year << "\n";
break;
}
case 7:
{cout << "Jul" << " " << day << ","<< year << "\n";
break;}
case 8:
{cout << "Aug" << " " << day << ","<< year << "\n";
break;
}
case 9:
{cout << "Sep" << " " << day << ","<< year << "\n";
break;
}
case 10:
{cout << "Oct" << " " << day << ","<< year << "\n";
break;
}
case 11:
{cout << "Nov" << " " << day << ","<< year << "\n";
break;
}
case 12:
{cout << "Dec" << " " << day << ","<< year << "\n";
break;
}
}
}
_________________________________________________________________________________
#include <iostream>
#include <cmath>
#include "date.h"
using namespace std;
// Declare and initialize variables.
int main(void)
{
date date;
// Read input date.
cout << "Please enter the date as day, month, year" << endl;
date.input();
// Print date in comma and slash form.
cout << "Date in slash form:" << endl;
date.print_slash();
cout << "Date in comma form:" << endl;
date.print_comma();
// Exit program.
return 0;
}
Project 3
#include <SD.h>
#include <Wire.h>
#include "BMP180Lib.h"
#include <SoftwareSerial.h>
BMP180lib bmp180; //create an instance of the BMP180 library to able to use functions in BMP180lib.h.
SoftwareSerial ss (9, 8);
//Set by default for the SD card library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
const int CS_PIN = 10;
// make it long enough to hold your longest file name, plus a null terminator
char filename[16];
//manually input desired filename here before each test
char *s = "LOG_";
const int alarm = 3;
const float p0 = 101325; // Pressure at sea level (Pa)
float altitude, apogee = 0;
unsigned int ut = 0;
unsigned long up = 0, previousTime = 0;
long timeStamp = 0, temperature = 0, pressure = 0;
double accel = 0;
int START = 0, x1 = 0, y1 = 0, z1 = 0, interval = 750;
float x_g = 0, y_g = 0, z_g = 0;
const unsigned char UBX_HEADER[] = { 0xB5, 0x62 }; //the first two bits for every UBX message from NEO-6M
struct NAV_POSLLH { //structure to read in values from the NAV_POSLLH UBX message coming from the NEO-6M GPS
unsigned char cls;
unsigned char id;
unsigned short len;
unsigned long iTOW;
long lon;
long lat;
long height;
long hMSL;
unsigned long hAcc;
unsigned long vAcc;
};
NAV_POSLLH posllh;
void setup()
{
pinMode(CS_PIN, OUTPUT);
pinMode(alarm, OUTPUT);
Serial.begin(57600);
ss.begin(9600); //initialize software serial port at 9600 baud
Wire.begin(); //Create a Wire object
bmp180.Calibration(); //BMP180 function needed to be ran each time to make sure sensor is accurate
Serial.println("Initializing Card");
//CS pin is an output
if (SD.begin(CS_PIN))
{
Serial.println("Card Ready");
}
else
{
Serial.println("Card Failed");
return;
}
int n = 0;
snprintf(filename, sizeof(filename), "%s%02d.csv", s, n); // includes a two-digit sequence number in the file name
while (SD.exists(filename)) {
n++;
snprintf(filename, sizeof(filename), "%s%02d.csv", s, n);
}
File dataFile = SD.open(filename, FILE_READ);
Serial.println(n);
Serial.println(filename);
dataFile.close();
//now filename[] contains the name of a file that doesn't exist
if ( processGPS() )
{
// Serial.print("
}
}
void loop()
{
timeStamp = millis();
unsigned long currentTime = millis();
read_ADXL();
read_BMP180(); // function for getting data values from BMP180 sensor. Also detects apogee.
if (START == 1) //once "z_g" is detected above 3,
{
Serial.println(altitude, 2);
Serial.print(x_g);
Serial.print(", ");
Serial.print(y_g);
Serial.print(", ");
Serial.println(z_g);
if ( processGPS() )
{
Serial.print(posllh.lat / 10000000.0f, 4);
Serial.print(",");
Serial.println(posllh.lon / 10000000.0f, 4);
}
Serial.println();
}
else
{
if (currentTime - previousTime >= interval)
{
previousTime = currentTime;
Serial.println("Awaiting launch");
tone(alarm, 466, 200);
delay(50);
}
}
if (altitude <= 250 && apogee > 500)
{
Serial.print("Apogee: ");
Serial.println(apogee);
}
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.print(pressure, DEC);
dataFile.print(",");
dataFile.print(altitude, 2);
dataFile.print(",");
dataFile.print(x_g);
dataFile.print(",");
dataFile.print(y_g);
dataFile.print(",");
dataFile.print(z_g);
dataFile.print(",");
if ( processGPS() )
{
dataFile.print(posllh.lat / 10000000.0f, 4);
dataFile.print(",");
dataFile.print(posllh.lon / 10000000.0f, 4);
}
dataFile.println();
dataFile.close(); //Data isn't actually written until we close the connection!
}
else
{
Serial.println("Couldn't open log file");
}
tone(alarm, 262, 5);
}
void read_ADXL()
{ //the reading, converting and storing of acceleration values happen here. Also, when "START" is set to 1, data streaming and writing commences.
x1 = analogRead(A1);
y1 = analogRead(A2);
z1 = analogRead(A3);
x_g = ( ( ( (double)(x1 * 5) / 1024) - 1.65 ) / 0.330 );
y_g = ( ( ( (double)(y1 * 5) / 1024) - 1.65 ) / 0.330 );
z_g = ( ( ( (double)(z1 * 5) / 1024) - 1.65 ) / 0.330 );
if (z_g > 3)
{
START = 1;
}
}
void read_BMP180()
{ //this groups all the fucntions related to the BMP180 sensor, stores the value of the altitude and apogee.
ut = bmp180.ReadUT();
up = bmp180.ReadUP();
temperature = bmp180.GetTemperature(ut);
pressure = bmp180.GetPressure(up);
altitude = (float)44330 * (1 - pow(((float) pressure / p0), 0.190295));
if (altitude >= apogee)
{
apogee = altitude;
}
}
void calcChecksum(unsigned char* CK) {//this function was written by Youtube Content Creator iforce2d. This is what performs the checksum on an individual bit in the UBX GPS message.
memset(CK, 0, 2);
for (int i = 0; i < (int)sizeof(NAV_POSLLH); i++) {
CK[0] += ((unsigned char*)(&posllh))[i];
CK[1] += CK[0];
}
}
bool processGPS() {//this function was written by Youtube Content Creator iforce2d. It reads the 34-bit binary NAV_POSLLH UBX message coming from the NEO-6M GPS and verifies each bit against the checksum for the message.
static int fpos = 0;
static unsigned char checksum[2];
const int payloadSize = sizeof(NAV_POSLLH);
while ( ss.available() ) {
byte c = ss.read();
if ( fpos < 2 ) {
if ( c == UBX_HEADER[fpos] )
fpos++;
else
fpos = 0;
}
else {
if ( (fpos - 2) < payloadSize )
((unsigned char*)(&posllh))[fpos - 2] = c;
fpos++;
if ( fpos == (payloadSize + 2) ) {
calcChecksum(checksum);
}
else if ( fpos == (payloadSize + 3) ) {
if ( c != checksum[0] )
fpos = 0;
}
else if ( fpos == (payloadSize + 4) ) {
fpos = 0;
if ( c == checksum[1] ) {
return true;
}
}
else if ( fpos > (payloadSize + 4) ) {
fpos = 0;
}
}
}
return false;
}
#include <Wire.h>
#include "BMP180Lib.h"
#include <SoftwareSerial.h>
BMP180lib bmp180; //create an instance of the BMP180 library to able to use functions in BMP180lib.h.
SoftwareSerial ss (9, 8);
//Set by default for the SD card library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
const int CS_PIN = 10;
// make it long enough to hold your longest file name, plus a null terminator
char filename[16];
//manually input desired filename here before each test
char *s = "LOG_";
const int alarm = 3;
const float p0 = 101325; // Pressure at sea level (Pa)
float altitude, apogee = 0;
unsigned int ut = 0;
unsigned long up = 0, previousTime = 0;
long timeStamp = 0, temperature = 0, pressure = 0;
double accel = 0;
int START = 0, x1 = 0, y1 = 0, z1 = 0, interval = 750;
float x_g = 0, y_g = 0, z_g = 0;
const unsigned char UBX_HEADER[] = { 0xB5, 0x62 }; //the first two bits for every UBX message from NEO-6M
struct NAV_POSLLH { //structure to read in values from the NAV_POSLLH UBX message coming from the NEO-6M GPS
unsigned char cls;
unsigned char id;
unsigned short len;
unsigned long iTOW;
long lon;
long lat;
long height;
long hMSL;
unsigned long hAcc;
unsigned long vAcc;
};
NAV_POSLLH posllh;
void setup()
{
pinMode(CS_PIN, OUTPUT);
pinMode(alarm, OUTPUT);
Serial.begin(57600);
ss.begin(9600); //initialize software serial port at 9600 baud
Wire.begin(); //Create a Wire object
bmp180.Calibration(); //BMP180 function needed to be ran each time to make sure sensor is accurate
Serial.println("Initializing Card");
//CS pin is an output
if (SD.begin(CS_PIN))
{
Serial.println("Card Ready");
}
else
{
Serial.println("Card Failed");
return;
}
int n = 0;
snprintf(filename, sizeof(filename), "%s%02d.csv", s, n); // includes a two-digit sequence number in the file name
while (SD.exists(filename)) {
n++;
snprintf(filename, sizeof(filename), "%s%02d.csv", s, n);
}
File dataFile = SD.open(filename, FILE_READ);
Serial.println(n);
Serial.println(filename);
dataFile.close();
//now filename[] contains the name of a file that doesn't exist
if ( processGPS() )
{
// Serial.print("
}
}
void loop()
{
timeStamp = millis();
unsigned long currentTime = millis();
read_ADXL();
read_BMP180(); // function for getting data values from BMP180 sensor. Also detects apogee.
if (START == 1) //once "z_g" is detected above 3,
{
Serial.println(altitude, 2);
Serial.print(x_g);
Serial.print(", ");
Serial.print(y_g);
Serial.print(", ");
Serial.println(z_g);
if ( processGPS() )
{
Serial.print(posllh.lat / 10000000.0f, 4);
Serial.print(",");
Serial.println(posllh.lon / 10000000.0f, 4);
}
Serial.println();
}
else
{
if (currentTime - previousTime >= interval)
{
previousTime = currentTime;
Serial.println("Awaiting launch");
tone(alarm, 466, 200);
delay(50);
}
}
if (altitude <= 250 && apogee > 500)
{
Serial.print("Apogee: ");
Serial.println(apogee);
}
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.print(pressure, DEC);
dataFile.print(",");
dataFile.print(altitude, 2);
dataFile.print(",");
dataFile.print(x_g);
dataFile.print(",");
dataFile.print(y_g);
dataFile.print(",");
dataFile.print(z_g);
dataFile.print(",");
if ( processGPS() )
{
dataFile.print(posllh.lat / 10000000.0f, 4);
dataFile.print(",");
dataFile.print(posllh.lon / 10000000.0f, 4);
}
dataFile.println();
dataFile.close(); //Data isn't actually written until we close the connection!
}
else
{
Serial.println("Couldn't open log file");
}
tone(alarm, 262, 5);
}
void read_ADXL()
{ //the reading, converting and storing of acceleration values happen here. Also, when "START" is set to 1, data streaming and writing commences.
x1 = analogRead(A1);
y1 = analogRead(A2);
z1 = analogRead(A3);
x_g = ( ( ( (double)(x1 * 5) / 1024) - 1.65 ) / 0.330 );
y_g = ( ( ( (double)(y1 * 5) / 1024) - 1.65 ) / 0.330 );
z_g = ( ( ( (double)(z1 * 5) / 1024) - 1.65 ) / 0.330 );
if (z_g > 3)
{
START = 1;
}
}
void read_BMP180()
{ //this groups all the fucntions related to the BMP180 sensor, stores the value of the altitude and apogee.
ut = bmp180.ReadUT();
up = bmp180.ReadUP();
temperature = bmp180.GetTemperature(ut);
pressure = bmp180.GetPressure(up);
altitude = (float)44330 * (1 - pow(((float) pressure / p0), 0.190295));
if (altitude >= apogee)
{
apogee = altitude;
}
}
void calcChecksum(unsigned char* CK) {//this function was written by Youtube Content Creator iforce2d. This is what performs the checksum on an individual bit in the UBX GPS message.
memset(CK, 0, 2);
for (int i = 0; i < (int)sizeof(NAV_POSLLH); i++) {
CK[0] += ((unsigned char*)(&posllh))[i];
CK[1] += CK[0];
}
}
bool processGPS() {//this function was written by Youtube Content Creator iforce2d. It reads the 34-bit binary NAV_POSLLH UBX message coming from the NEO-6M GPS and verifies each bit against the checksum for the message.
static int fpos = 0;
static unsigned char checksum[2];
const int payloadSize = sizeof(NAV_POSLLH);
while ( ss.available() ) {
byte c = ss.read();
if ( fpos < 2 ) {
if ( c == UBX_HEADER[fpos] )
fpos++;
else
fpos = 0;
}
else {
if ( (fpos - 2) < payloadSize )
((unsigned char*)(&posllh))[fpos - 2] = c;
fpos++;
if ( fpos == (payloadSize + 2) ) {
calcChecksum(checksum);
}
else if ( fpos == (payloadSize + 3) ) {
if ( c != checksum[0] )
fpos = 0;
}
else if ( fpos == (payloadSize + 4) ) {
fpos = 0;
if ( c == checksum[1] ) {
return true;
}
}
else if ( fpos > (payloadSize + 4) ) {
fpos = 0;
}
}
}
return false;
}
project 2
#include <LiquidCrystal.h>
#include "VernierLib.h" //library available from Veriner into interface with their sensors
VernierLib Vernier; //create an instance of the VernierLib library to able to use functions in Vernierlib.h
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int selectButton = 4;
const int leftButton = 5;
const int rightButton = 6;
const int CCWpin = 2; //pin for counterclockwise direction
const int CWpin = 3; //pin for clockwise direction
int Bpress = 1; //counter to keep track of button presses
float sensorReading; //use to store analog sensor reading
double displacement = 0; //variable for displacement wheel conversion to millimeters
volatile int counter = 0; //varible for displacement wheel counting
void setup() {
Serial.begin(9600); //start serial communication
Vernier.autoID(); //identify the analog sensor being used. Only one sensor can be detected with Autoint with additional multiplexers to interface multiple with one arduino.
pinMode(CCWpin, INPUT_PULLUP); //setup CCW pin
pinMode(CWpin, INPUT_PULLUP); //setup CW pin
pinMode(selectButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
lcd.begin(16,2); // '16,2' is 16 columns, 2 rows on the display
digitalWrite(CCWpin, LOW); //initialize CCW pin status
digitalWrite(CWpin, HIGH); //initialize CW pin status
attachInterrupt(digitalPinToInterrupt(CCWpin), movingCCW, RISING); //trigger when pin goes from LOW to HIGH
attachInterrupt(digitalPinToInterrupt(CWpin), movingCW, FALLING); //trigger when pin goes from HIGH to LOW
Serial.println("Dist Force Time");
Serial.print("\n");
}
void loop() {
displacement = counter*0.1; //converts angular movement of displacement wheel to linear displacement of tackle
sensorReading = Vernier.readSensor(); //readSensor reads the voltage from BTA sensor and automatically converts to units of newtons
lcd.setCursor(0,0);
lcd.print(millis() / 1000.0, 3); //time since arduino turned/reset
lcd.print(" sec ");
if (digitalRead(leftButton) == HIGH)
{ //this increments through the 3 screens to the left
Bpress++;
if (Bpress > 3) //ensures the user doesnt go past the thrid screen
{
Bpress = 3;
}
}
if (digitalRead(rightButton) == HIGH)
{ //this increments through the 3 screens to the right
Bpress--;
if (Bpress < 0) //ensures user doesnt go past the first screen
{
Bpress = 1;
}
}
switch(Bpress) //each case shows different screen on LCD
{
case 1: //this is default when arduino is powered on
DispToLCD();
break;
case 2: // 2nd screen
ForceToLCD();
break;
case 3: //3rd screen
serialConnection();
break;
}
delay(250); //wait quarter second
}
void ForceToLCD()
{ //function shows force sensor data when called on LCD
lcd.setCursor(0,0);
lcd.print(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
lcd.print(" sec");
lcd.setCursor(0,1);
lcd.print("Force: ");
lcd.print(sensorReading); //print force data value
lcd.print(Vernier.sensorUnits()); //vernier lib function that auto detects correct units for sensor used
lcd.print(" ");
}
void DispToLCD()
{ //function shows displacement data when called on LCD
lcd.setCursor(0,0);
lcd.print(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
lcd.print(" sec");
lcd.setCursor(0,1);
lcd.print("Disp: ");
lcd.print(displacement); //resolution of displacement sensor is 0.1mm.
lcd.print(" mm ");
}
void DataToSerial()
{ //function to print all sensor data to serial when called
Serial.print(displacement); //resolution of displacement sensor is 0.1mm.
Serial.print(" "); //space to make data look nice on serial monitor
Serial.print(sensorReading); //print force data value
Serial.print(" "); //print a space
Serial.println(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
Serial.print("\n");
}
void serialConnection()
{ //function asks user if they want to send data over serial and awaits for select button to be held to initiate
lcd.setCursor(0,0);
lcd.print("send data to pc");
lcd.setCursor(0,1);
lcd.print("over serial? ");
if (digitalRead(selectButton) == HIGH) //when the select button is held down data will be sent to PC over serial bus
{
DataToSerial();
lcd.setCursor(0,0);
lcd.print("serial monitor ");
lcd.setCursor(0,1);
lcd.print("running on pc ");
}
}
void movingCCW()
{ //counts with interrupt as displacement wheel is turned counterclockwise
counter++; //count UP
}
void movingCW()
{ //counts with interrupt as displacement wheel is turned clockwise
counter--; //count DOWN
}
#include "VernierLib.h" //library available from Veriner into interface with their sensors
VernierLib Vernier; //create an instance of the VernierLib library to able to use functions in Vernierlib.h
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int selectButton = 4;
const int leftButton = 5;
const int rightButton = 6;
const int CCWpin = 2; //pin for counterclockwise direction
const int CWpin = 3; //pin for clockwise direction
int Bpress = 1; //counter to keep track of button presses
float sensorReading; //use to store analog sensor reading
double displacement = 0; //variable for displacement wheel conversion to millimeters
volatile int counter = 0; //varible for displacement wheel counting
void setup() {
Serial.begin(9600); //start serial communication
Vernier.autoID(); //identify the analog sensor being used. Only one sensor can be detected with Autoint with additional multiplexers to interface multiple with one arduino.
pinMode(CCWpin, INPUT_PULLUP); //setup CCW pin
pinMode(CWpin, INPUT_PULLUP); //setup CW pin
pinMode(selectButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
lcd.begin(16,2); // '16,2' is 16 columns, 2 rows on the display
digitalWrite(CCWpin, LOW); //initialize CCW pin status
digitalWrite(CWpin, HIGH); //initialize CW pin status
attachInterrupt(digitalPinToInterrupt(CCWpin), movingCCW, RISING); //trigger when pin goes from LOW to HIGH
attachInterrupt(digitalPinToInterrupt(CWpin), movingCW, FALLING); //trigger when pin goes from HIGH to LOW
Serial.println("Dist Force Time");
Serial.print("\n");
}
void loop() {
displacement = counter*0.1; //converts angular movement of displacement wheel to linear displacement of tackle
sensorReading = Vernier.readSensor(); //readSensor reads the voltage from BTA sensor and automatically converts to units of newtons
lcd.setCursor(0,0);
lcd.print(millis() / 1000.0, 3); //time since arduino turned/reset
lcd.print(" sec ");
if (digitalRead(leftButton) == HIGH)
{ //this increments through the 3 screens to the left
Bpress++;
if (Bpress > 3) //ensures the user doesnt go past the thrid screen
{
Bpress = 3;
}
}
if (digitalRead(rightButton) == HIGH)
{ //this increments through the 3 screens to the right
Bpress--;
if (Bpress < 0) //ensures user doesnt go past the first screen
{
Bpress = 1;
}
}
switch(Bpress) //each case shows different screen on LCD
{
case 1: //this is default when arduino is powered on
DispToLCD();
break;
case 2: // 2nd screen
ForceToLCD();
break;
case 3: //3rd screen
serialConnection();
break;
}
delay(250); //wait quarter second
}
void ForceToLCD()
{ //function shows force sensor data when called on LCD
lcd.setCursor(0,0);
lcd.print(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
lcd.print(" sec");
lcd.setCursor(0,1);
lcd.print("Force: ");
lcd.print(sensorReading); //print force data value
lcd.print(Vernier.sensorUnits()); //vernier lib function that auto detects correct units for sensor used
lcd.print(" ");
}
void DispToLCD()
{ //function shows displacement data when called on LCD
lcd.setCursor(0,0);
lcd.print(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
lcd.print(" sec");
lcd.setCursor(0,1);
lcd.print("Disp: ");
lcd.print(displacement); //resolution of displacement sensor is 0.1mm.
lcd.print(" mm ");
}
void DataToSerial()
{ //function to print all sensor data to serial when called
Serial.print(displacement); //resolution of displacement sensor is 0.1mm.
Serial.print(" "); //space to make data look nice on serial monitor
Serial.print(sensorReading); //print force data value
Serial.print(" "); //print a space
Serial.println(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
Serial.print("\n");
}
void serialConnection()
{ //function asks user if they want to send data over serial and awaits for select button to be held to initiate
lcd.setCursor(0,0);
lcd.print("send data to pc");
lcd.setCursor(0,1);
lcd.print("over serial? ");
if (digitalRead(selectButton) == HIGH) //when the select button is held down data will be sent to PC over serial bus
{
DataToSerial();
lcd.setCursor(0,0);
lcd.print("serial monitor ");
lcd.setCursor(0,1);
lcd.print("running on pc ");
}
}
void movingCCW()
{ //counts with interrupt as displacement wheel is turned counterclockwise
counter++; //count UP
}
void movingCW()
{ //counts with interrupt as displacement wheel is turned clockwise
counter--; //count DOWN
}
Subscribe to:
Posts (Atom)