Thursday, October 25, 2018

Day 17 10/23/18

Arduino Homework
Extend you program to act under serial control.
int M1_Left = 11; //Motor Input 1
int M1_Right = 10; //Motor Input 2
int POT = A1;
int val = 0;
int value = 0;
char value2 = 0;
int value3 = 0;
int velocity = 75;
void setup()
{
  Serial.begin(9600);
  Serial.println("Begin Motor Control Sequence");
  Serial.println("Enter Number for Control Option:");
  Serial.println("1 for Forward");
  Serial.println("2 for Reverse");
  Serial.println("3 for stop");
  Serial.println("+ for speed increase");
  Serial.println("- for speed decrease");
 pinMode(M1_Left, OUTPUT);
 pinMode(M1_Right , OUTPUT);
}
void loop(){
 while(Serial.available() > 0){
 value2 = Serial.read();
 Serial.println(value2);
 if(value2 == '1'){
  value=0;
  while(value == 0){
 forward(velocity);
 value2 = Serial.read();
 Serial.println(value2);
  if(value2 == '+'){
   velocity+=10;
 }
 if(value2 == '-'){
  velocity-=10;
 }
 if(value2 == '2'){
  value++;
 }
 if(value2 == '3'){
  value++;
  }
 }
 }
 if(value2 == '2'){
    value=0;
  while(value == 0){
 reverse(velocity);
 value2 = Serial.read();
 Serial.println(value2);
  if(value2 == '+'){
   velocity+=10;
 }
 if(value2 == '-'){
  velocity-=10;
 }
  if(value2 == '1'){
  value++;
 }
 if(value2 == '3'){
  value++;
 }
  }
 }
 if(value2 == '3'){
  brake();
 }
 }

 /* val = analogRead(POT);
  if(val > 562){
    velocity = map(val, 563, 1023, 0, 255);
    forward(velocity);
    Serial.println(velocity);
  }
  if(val < 462){
    velocity = map(val, 461, 0, 0, 255);
    reverse(velocity);
    Serial.println(velocity);
  }
  else{
    brake();
  }*/
}
void forward(int direction)
{
 analogWrite(M1_Right, 0);
 analogWrite(M1_Left, direction);
}
void reverse(int direction)
{
 analogWrite(M1_Left, 0);
 analogWrite(M1_Right, direction);
}
void brake(){
 analogWrite(M1_Left, 0);
 analogWrite(M1_Right , 0);


ENSO and Seismic Homework

1. Modify the program to find and print the maximum La Niña conditions.
2. Modify the program to find the conditions closest to zero. These would be the conditions that are closest to normal.
3. Modify the program so that it prints all years and quarters with El Niño conditions.

/* This program reads a data file of ENSO index values and */

/* determines the maximum El Nino condition in the file. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define FILENAME "ENSO1.txt"

#define MAX_SIZE 1000

int main(void)

{

/* Declare variables and function prototypes. */

int k=0,l=0, year[MAX_SIZE], year2[MAX_SIZE], qtr[MAX_SIZE], qtr2[MAX_SIZE], max_k=0, min_k=0, find0=0, count=0;

double index[MAX_SIZE], data[MAX_SIZE];

FILE *enso;

/* Read sensor data file. */

enso = fopen(FILENAME,"r");

if (enso == NULL)

printf("Error opening input file. \n");

else

{

while (fscanf(enso,"%d %d %lf", year+k,qtr+k,index+k)==3){

if (*(index+k) > *(index+max_k)){

max_k = k;}
if (*(index+k) < *(index+min_k)){

min_k = k;}

if(*(index+k) > 0){
data[l] = index[k];
year2[l] = *(year+k);
qtr2[l] = *(qtr+k);
l++;
count++;
}

if (fabs(*(index+k)) >= 0){
if (fabs(*(index+k)) < fabs(*(index+find0))){

find0 = k;}}



k++;

}

/* Print data for maximum El Nino condition. */

printf("Maximum El Nino Conditions in Data File \n");

printf("Year: %d, Quarter: %d \n", *(year+max_k),*(qtr+max_k));
printf("Maximum la Nina Conditions in Data File \n");

printf("Year: %d, Quarter: %d \n", *(year+min_k),*(qtr+min_k));
printf("Conditions closest to 0 in Data File \n");

printf("Year: %d, Quarter: %d \n\n", *(year+find0),*(qtr+find0));
for(l=0;l<count;l++){
printf("YEAR: %d ", year2[l]);
printf("QUARTER: %d ", qtr2[l]);
printf("DATA: %5.1f\n", data[l]);}
/* Close file. */

fclose(enso);

}

/* Exit program. */

return 0;


}

1. Allow the user to enter the threshold value. Check the value to be sure that it is a positive value greater than 1.
2. Print the number of events detected by the program. (Assume that events with contiguous times are all part of the same event. Thus, for the hand example, one event was detected.

/* 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

double THRESHOLD;

int main(void)

{

int val=0;

while(val==0){

printf("Enter the Threshold Number:\n");

scanf("%lf", &THRESHOLD);

if(THRESHOLD<1){

printf("Please Enter a Positive Value above 1\n");

}

else{

val++;

}}

/* Declare variables and function prototypes. */

int k, npts, short_window, long_window, count=0;

double sensor[MAX_SIZE], time[2], 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

{

/* 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){

printf("Possible event at %f seconds \n",time_incr*k);

count++;

time[k]=time_incr*k;}

}

for(int j=long_window-1; j<=npts-1; j++){

if((time[j]-time[j+1])==.01){

count++;

}}

printf("%d Events Occured", count);

/* 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;


}

No comments:

Post a Comment