DAY 16 HW
/* */
/* 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
//#define THRESHOLD 1.5
int main(void)
{
/* Declare variables and function prototypes. */
int k,i, npts, short_window, long_window,thrsh,THRESHOLD;
double sensor[MAX_SIZE], time_incr, short_power,
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
{
printf("Enter threshold value:");
scanf("%d",&THRESHOLD);
if(THRESHOLD>1.00)
{
/* 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);
/* 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)
{
i++;
//printf("\nnumber of count:%d\n",i);
printf("Possible event at %f seconds \n",
time_incr*k);
}
}
}
else
{
printf("Enter a value greater then 1 to run the code");
return 0;
}
printf("\nnumber of count:%d\n",i);
/* Close file. */
fclose(file_ptr);
}
}
/* Exit program. */
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;
}
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-*/
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int M1_Left = 12; //Motor Input 1
int M1_Right = 11; //Motor Input 2
int M2_Left = 10; //Motor Input 1
int M2_Right = 9; //Motor Input 2
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_ pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_ pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_ pin));
Serial.print("\n\n");
delay(500);
if (analogRead(X_pin) >520)
{
forward(255);
}
else if (analogRead(X_pin)<470)
{
reverse(255);
}
else if ( analogRead(Y_pin)>500)
{
analogWrite (M2_Left, 0); //Motor Input 1
analogWrite(M2_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else if ( analogRead(Y_pin)<485)
{
analogWrite (M1_Left, 0); //Motor Input 1
analogWrite(M1_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else
stop();
}
void forward (int rate)
{
int inPin1 = LOW;
int inPin2 = rate;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
}
void reverse (int rate)
{
int inPin1 = rate;
int inPin2 = LOW;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
}
void stop()
{
int inPin1 = LOW;
int inPin2 = LOW;
digitalWrite(M1_Left, LOW);
digitalWrite(M1_Right , LOW);
digitalWrite(M2_Left, LOW);
digitalWrite(M2_Right , LOW);
}
Comments
Post a Comment