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;


}

Sunday, October 21, 2018

Day 16 10/18/18

Arduino Homework

1.    Modify your code so that the potentiometer controls the motor speed.


//Simple Motor Speed Control Program
const int MOTOR=9; //Motor on Digital Pin 9
int POT = A0;
void setup()
{
 pinMode (MOTOR, OUTPUT);
}
void loop()
{

 analogWrite(MOTOR, analogRead(POT));
 }

Day 15 10/16/18

Arduino Homework

1.  Modify your program to use the accelerometer as an inclinometer so that it displays the angle that the board is tilted to in degrees. Interface your board so that it displays the output in degrees to your LCD display.


#include <math.h>
#include <LiquidCrystal.h>
#define PI 3.14
unsigned long time;
const int xpin = A2; // x-axis of the accelerometer
const int ypin = A1; // y-axis
const int zpin = A3; // z-axis
int xdata = 0;
int ydata = 0;
float xrad;
float yrad;
float degree;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
 // initialize the serial communications:
 Serial.begin(57600);
  //Set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 //Print a message to the LCD.
 lcd.print("Angle");
}
void loop() {
 time = millis();
 Serial.print(time); //prints time since program started
  Serial.print("\t");
 // print the sensor values:
 xdata = analogRead(xpin);
 Serial.print(xdata);
 // print a tab between values:
 Serial.print("\t");
 ydata = analogRead(ypin);
 Serial.print(ydata);
 // print a tab between values:
 Serial.print("\t");
 Serial.print(analogRead(zpin));
 Serial.println();
 xrad = xdata - 322;
 yrad = ydata - 322;
 degree = (atan2(xrad, yrad) * 180/PI);
  lcd.setCursor(0,7);
 //Print Current Time
 lcd.print(degree);
 // delay before next reading:
 delay(3);
}

Sunday, October 14, 2018

day 14 10/11/18

Arduino LCD with I^2C Temp Sensor
#1. Interface your I2C temperature to the Arduino and have the values of the temperature shown on the screen. Allow the user to press one of the buttons and have it display the temperature in o F and the other button to display it in oC.

//LCD text with incrementing number
//Include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
const int temp_address = 73; //1001100 written as decimal number
int c = 0, f = 0;
//Start the time at 0
int time = 0;
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
  lcd.begin(16,2);
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 Wire.begin();
}
void loop() {
 lcd.setCursor(0,1);
 // read the input on analog pin 0:
 int sensorValue = analogRead(A0);
 // print out the value you read:
 Serial.println(sensorValue);
  //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
 c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.print("F ");
 delay(500);
  if(sensorValue >= 477 && sensorValue < 719){
    lcd.print(f);
    lcd.print("f         ");
    int val = 1;
    while (val == 1){
  sensorValue = analogRead(A0);
  delay(100);
  if(sensorValue >= 719 && sensorValue < 1023){
    val --;
  }
  }
 }
  else if(sensorValue >= 719 && sensorValue <= 1023){
  lcd.print(c);
  lcd.print("c         ");
 }
 delay(1); // delay in between reads
}

Thursday, October 11, 2018

day 13 10/9/18

Elevation problem
Modify the program to determine the following information for a grid of elevation data:
1. Print a count of the number of peaks in the grid.
2. Print the location of valleys instead of peaks. Assume that a valley is a point with an elevation lower than the four surrounding elevations.
3. Find and print the location and elevation of the highest point and the lowest point in the elevation data.
4. Assuming that the distance between points in a vertical and horizontal direction is 100 feet, give the location of the peaks in feet from the lower left corner of the grid.
5. Use all eight neighboring points, instead of only four neighboring points, to determine a peak.

/* 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, sum = 0, sum2 = 0, Max = 0, Min = 0, x = 0, y = 0;
double elevation[N][N], distance = 0.;
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]) && (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])){
printf("Peak at row: %d column: %d \n",i,j);
distance = sqrt((((j+1)*100)*((j+1)*100))+(((nrows-i)*100)*((nrows-i)*100)));
printf("The distance from the peak to the origin is %5.2f feet\n", distance);
sum += 1;
}
}
}
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])){
printf("Valley at row: %d column: %d \n",i,j);
sum2 += 1;
}
}
}
for (i=1; i<=nrows-1; i++){
for (j=0; j<=ncols-1; j++){
if (elevation[i][j] > Max){
Max = elevation[i][j];
x = i;
y = j;
}
}
}
printf("The max elevation is %d at row: %d column: %d\n", Max, x, y);
Min = elevation[1][0];
for (i=1; i<=nrows-1; i++){
for (j=0; j<=ncols-1; j++){
if (Min > elevation[i][j]){
Min = elevation[i][j];
x = i;
y = j;
}
}
}
printf("The min elevation is %d at row: %d column: %d\n", Min, x, y);
printf("The total amount of Peaks is %d\n", sum);
printf("The total amount of Valleys is %d\n", sum2);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */

}

Arduino isquaredc temp sensor hw


Sunday, October 7, 2018

day 12 10/4/18

Sweeping Servo Distance Sensor

#include <Servo.h>
const int SERVO = 9;
const int IR = A0;
Servo myServo;
int dist1 = 0;
int val = 0;
void setup() {
myServo.attach(SERVO);
Serial.begin(9600);
}

void loop() {
  for(int i = 0; i <= 180; i++){
dist1 = readDistance(i);
if (dist1 >= 100){
  val++;
}
while (val == 1){
  dist1 = analogRead(IR);
  dist1 = map(dist1, 50, 500, 0, 255);
  dist1 = constrain(dist1, 0, 255);
  Serial.println(dist1);
  //myServo.write(i);
  if (dist1 <= 100){
    val--;
  }
}
}
delay(50);
  for(int i = 180; i >= 0; i--){
dist1 = readDistance(i);
if (dist1 >= 100){
  val++;
}
while (val == 1){
  dist1 = analogRead(IR);
  dist1 = map(dist1, 50, 500, 0, 255);
  dist1 = constrain(dist1, 0, 255);
  Serial.println(dist1);
  //myServo.write(i);
  if (dist1 <= 100){
    val--;
  }
}
}
delay(50);
}
int readDistance(int pos){
  myServo.write(pos);
  delay(25);
  int dist = analogRead(IR);
  dist = map(dist, 50, 500, 0, 255);
  dist = constrain(dist, 0, 255);
  Serial.println(dist);
  return dist;

}

Wednesday, October 3, 2018

Day 11 10/2/18

Arduino homework

1. Interface your servo with the arduino according to the directions in the previous lab. Allow the user to enter an angle and the servo turns to that angle. If the angle entered is invalid a red light should light on the breadboard.

#include <Servo.h>
const int SERVO = 9;
const int RLED = 10;
const int GLED = 11;
int servoval = 0;
int val = 0;
Servo Servo;
void setup()
{
 Serial.begin(9600);
 pinMode(RLED, OUTPUT);
 pinMode(GLED, OUTPUT);
 Servo.attach(SERVO);
 Serial.println("Enter servo degree value between 0 and 180");
 digitalWrite(GLED, HIGH);
 digitalWrite(RLED, LOW);
}
void loop()
{
 //Keep working as long as data is in the buffer
 while (Serial.available() > 0)
{
 servoval = Serial.parseInt();
 if (Serial.read() == '\n') //Done transmitting
 {
 //set servo
 if (servoval <= 180 && servoval >= 0){
  Serial.println(servoval);
  digitalWrite(GLED, HIGH);
  digitalWrite(RLED, LOW);
  Servo.write(servoval);
 }
 else {
  Serial.println("Not a value between 0 and 180, try again");
  val++;
  while (val == 1){
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, LOW);
  delay(150);
  digitalWrite(RLED, LOW);
  delay(150);
  servoval = Serial.parseInt();
   if (Serial.read() == '\n'){//Done transmitting
     if (servoval <= 180 && servoval >= 0){
  Serial.println(servoval);
  digitalWrite(GLED, HIGH);
  digitalWrite(RLED, LOW);
  Servo.write(servoval);
  val--;
     }
  }
 }
 }
 }
}
}

Balloon programming problems
a. Write a program that will print a table of the altitude and the velocity for this weather balloon using units of meters and meters per second. Let the user enter the start time, the increment in time between lines of the table, and the ending time, where all the time values must be less than 48 hours. Use the program to generate a table showing the weather balloon information every 10 minutes over a 2-hour period, starting 4 hours after the balloon was launched.
b. Modify the program above so that it also prints the peak altitude and its corresponding time.
c. Modify the program so that it will check to be sure that the final time is greater than the initial time. If it is not, ask the user to reenter the complete set of report information.
d. The preceding equations are accurate only for the time from 0 to 48 hours, so modify the program generated above so that it prints a message to the user that specifies an upper bound of 48 hours. Also, check the user input to make sure that it stays within the proper bounds. If there are errors, ask the user to reenter the complete set of report information.
e. Modify the program above so that it stores the time, altitude, and velocity information in a data file named .

#include <stdio.h>
#include <math.h>
#define FILENAME "ballon1.txt"
double starttime, endtime, timeincr, t, altitude1, velocity, maxwave = 0, maxwavetime = 0;
char minorhour;

int main(void){
FILE *balloon;
balloon = fopen(FILENAME,"w");
_Bool keep_looping = 1;
_Bool keep_looping2 = 1;
_Bool keep_looping3 = 1;
printf("This program will print a table of the altitudes and velocities of a weather balloon at a given time interval in hours (within 48)\n");
while (keep_looping == 1){
while (keep_looping2 == 1){
printf("Enter a start time\n");
scanf("%lf",&starttime);
if(starttime < 0){
printf("The start time cannot be negative,\nplease reenter all credentials\n\n");
}
if(starttime >= 48){
printf("The start time cannot be equal to or over 48,\nplease reenter all credentials\n\n");
}
if(starttime >= 0 && starttime < 48){
keep_looping2 = 0;
}
}
printf("\nEnter an end time\n");
scanf("%lf",&endtime);
if(starttime > endtime){
printf("The start time cannot be after the end time,\nplease reenter all credentials\n\n");
keep_looping2 = 1;
}
if(starttime == endtime){
printf("The start time cannot equal the end time,\nplease reenter all credentials\n\n");
keep_looping2 = 1;
}
if(endtime < 0){
printf("the end time cannot be negative,\nplease reenter all credentials\n\n");
}
if(endtime >48){
printf("the end time cannot be over 48 hours,\nplease reenter all credentials\n\n");
}
if(starttime < endtime && endtime > 0 && endtime <= 48){
keep_looping = 0;
}
}
printf("\nEnter a time increment\n");
scanf("%lf",&timeincr);
while (keep_looping3 == 1){
printf("Please specify if it is in minutes or hour (m or h)\n");
scanf(" %c",&minorhour);
if(minorhour != 109 || minorhour != 77 || minorhour != 104 || minorhour != 72){
printf("time interval must be in either minutes or hours (m or h),\nplease reenter all credentials\n\n");
}
if(minorhour == 109 || minorhour == 77 || minorhour == 104 || minorhour == 72){
keep_looping3 = 0;
}
}
printf("Altitudes (in meters):\n");
if(minorhour == 109 || minorhour == 77){
timeincr = timeincr / 60;
}
int timedivider = (endtime-starttime) / timeincr;
int time[100], velocity[100], altitude[100];
fprintf(balloon, "Altitudes:\n");
for (t = starttime; t <= endtime; t = t + timeincr){
int i = 0;
altitude[i] = (-.12 * pow(t,4)) + (12 * pow(t,3)) - (380 * pow(t,2)) + (4100 * t) + 220;
altitude1 = (-.12 * pow(t,4)) + (12 * pow(t,3)) - (380 * pow(t,2)) + (4100 * t) + 220;
printf("%d\n", altitude[i]);
fprintf(balloon, "%d\n", altitude[i]);
if (altitude1 >maxwave){
maxwave = altitude1;
maxwavetime = t;
}
i++;
}
fprintf(balloon, "Velocities:\n");
printf("Velocities (in meters per second):\n");
for (t = starttime; t <= endtime; t = t + timeincr){
int j = 0;
velocity[j] = (-.48 * pow(t,3)) + (36 * pow(t,2)) - (760 * t) + 4100;
printf("%d\n", velocity[j]);
fprintf(balloon, "%d\n", velocity[j]);
j++;
}
fprintf(balloon, "Times:\n");
printf("times:\n");
for (t = starttime; t <= endtime; t = t + timeincr){
int h = 0;
time[h] = t;
printf("%d\n", time[h]);
fprintf(balloon, "%d\n", time[h]);
h++;
}
/*fprintf(balloon, "Altitude Velocity Time\n");
for (int k = 0; k <= timedivider; k++){
fprintf(balloon, "%d %d %d\n", altitude[k], velocity[k], time[k]);
}*/
printf("the peak altitude was %f meters at time %f hours", maxwave, maxwavetime);
fclose(balloon);
return 0;

}

Monday, October 1, 2018

Day 9 9/25/18

1. Complete the random number problem but with all four parts;

a. Write a program that uses the rand_float function developed previously to generate sequences of random floating-point values between 4 and 10. Then compare the computed mean and variance with the computed theoretical values. As you use more and more random numbers, the computed values and the theoretical values should become closer.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define seed 26
#define number 10
#define npts 10
int a=4, b=10, list, sum, sumlist, average, variance, xna, xna2;
float random_float[number];
float rand_float(int a,int b);
int main(void)
{
srand(seed);
printf("list of %d random numbers:\n", number);
for (int i=1; i<=number; i++){
random_float[i] = rand_float(a,b);
list = random_float[i];
printf("%d\n", list);
sum +=list;}
average = sum / number;
for(int i=1; i<=number; i++){
xna = random_float[i] - average;
xna2 = pow(xna, 2);
sumlist += xna2;
}
variance = sumlist / (number-1);
printf("Average is:\n%d\nVariance is:\n%d\n", average, variance);
/* Exit program. */
return 0;
}
float rand_float(int a,int b)
{
return rand()%(b-a+1) + a;
}