Day_15
programming homework:
Initially I had trouble getting the code to even run, the pointers were confusing to understand for me because you know pointers.... David helped me out the next day before class and we figured out that i had a missing bracket and the code ran fine for the first modification.
/* This program reads a data file of ENSO index values and */
/* determines the maximum El Nino condition in the file. */
#include <stdio.h>
#define FILENAME "ENSO1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare variables and function prototypes. */
int k=0, year[MAX_SIZE], qtr[MAX_SIZE], max_k=0,min_k=0,max_k_2,min_k_2;
double index[MAX_SIZE];
for(int i =0;i<MAX_SIZE;i++)
{
year[i]=0;
qtr[i]=0;
index[i]=0;
}
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;
}
k++;
}
while (fscanf(enso,"%d %d %lf",year+k,qtr+k,index+k)>=3)
{
while(index>=0)
{
if(*(index+k) < *(index+max_k))
{
max_k_2 = k;
}
if(*(index+k) > *(index+min_k))
{
min_k_2 = 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("Closest El Nino Conditions in Data File \n");
printf("Year: %d, Quarter: %d \n",
*(year+max_k_2),*(qtr+max_k_2));
printf("Closest LA NINA Conditions in Data File \n");
printf("Year: %d, Quarter: %d \n",
*(year+min_k_2),*(qtr+min_k_2));
}
/* Close file. */
fclose(enso);
/* Exit program. */
return 0;
}
Arduino code for two motor:
Mathew came over to my house on Sunday to work on the code. I personally think we made the code more complicated then it needs to be, since we created functions for foward and reverse, and instead can set the motors equal to the value of the user input. Example analogWrite("motor",val); it did work out in the end and it's always good to have practice with functions especially when it's not my strong suite.
Initially I had trouble getting the code to even run, the pointers were confusing to understand for me because you know pointers.... David helped me out the next day before class and we figured out that i had a missing bracket and the code ran fine for the first modification.
/* This program reads a data file of ENSO index values and */
/* determines the maximum El Nino condition in the file. */
#include <stdio.h>
#define FILENAME "ENSO1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare variables and function prototypes. */
int k=0, year[MAX_SIZE], qtr[MAX_SIZE], max_k=0,min_k=0,max_k_2,min_k_2;
double index[MAX_SIZE];
for(int i =0;i<MAX_SIZE;i++)
{
year[i]=0;
qtr[i]=0;
index[i]=0;
}
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;
}
k++;
}
while (fscanf(enso,"%d %d %lf",year+k,qtr+k,index+k)>=3)
{
while(index>=0)
{
if(*(index+k) < *(index+max_k))
{
max_k_2 = k;
}
if(*(index+k) > *(index+min_k))
{
min_k_2 = 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("Closest El Nino Conditions in Data File \n");
printf("Year: %d, Quarter: %d \n",
*(year+max_k_2),*(qtr+max_k_2));
printf("Closest LA NINA Conditions in Data File \n");
printf("Year: %d, Quarter: %d \n",
*(year+min_k_2),*(qtr+min_k_2));
}
/* Close file. */
fclose(enso);
/* Exit program. */
return 0;
}
Arduino code for two motor:
Mathew came over to my house on Sunday to work on the code. I personally think we made the code more complicated then it needs to be, since we created functions for foward and reverse, and instead can set the motors equal to the value of the user input. Example analogWrite("motor",val); it did work out in the end and it's always good to have practice with functions especially when it's not my strong suite.
int velocity = 255;
char data; //Holds incoming character
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()
{
Serial.begin(9600); //Serial Port at 9600 baud
Serial.println("Begin Motor Control:");
Serial.println("");
Serial.println("Enter number for control options:");
Serial.println("1. Stop");
Serial.println("2. Forward");
Serial.println("3. Reverse");
Serial.println("4. Read Current Speed");
Serial.println("5. Increase Speed");
Serial.println("6. Decrease Speed");
Serial.println("7. Reset Speed");
}
void loop()
{
//Only act when data is available in the buffer
if (Serial.available() > 0)
{
data = Serial.read(); //Read byte of data
//Turn LED on
if (data == '1')
{
stop();
Serial.println("Stop");
// Serial.println("LED ON");
}
//Turn LED off
if (data == '2')
{
forward(velocity);
Serial.println("Forward");
boolean inputState = HIGH;
}
else if (data == '3')
{
reverse(velocity);
Serial.println("Reverse");
boolean inputState = LOW;
}
else if (data == '4')
{
Serial.println("Velocity=");
Serial.println(velocity);
}
else if (data == '5')
{
if(velocity<0)
{
velocity=1;
}
if(velocity>255)
{
velocity=255;
}
else {
velocity++;
}
}
else if (data == '6')
{
if(velocity<0)
{
velocity=1;
}
if(velocity>255)
{
velocity=255;
}
else {
velocity--;
}
}
else if(data=='7')
{
Serial.println("Velocity Reset");
velocity=255;
}
else
{
stop();
}
}
}
void forward (int rate
)
{
//val_left=map(val,0,512,255, 0);
// boolean inPin1 = LOW;
// boolean inPin2 = rate;
int inPin1 = LOW;
int inPin2 = rate;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
// Serial.print("Inpin1: ");
// Serial.print(inPin1);
// Serial.print(" ");
// Serial.print("Inpin2: ");
// Serial.print(inPin2);
// Serial.print(" ");
//
// Serial.print("Rate: ");
// Serial.println(rate);
}
void reverse (int rate)
{
//val_left=map(val,0,512,255, 0);
// boolean inPin1 = rate;
// boolean inPin2 = LOW;
int inPin1 = rate;
int inPin2 = LOW;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
// Serial.print("Inpin1: ");
// Serial.print(inPin1);
// Serial.print(" ");
// Serial.print("Inpin2: ");
// Serial.print(inPin2);
// Serial.print(" ");
//
// Serial.print("Rate: ");
// Serial.println(rate);
}
void stop()
{
// boolean inPin1 = LOW;
// boolean inPin2 = LOW;
int inPin1 = LOW;
int inPin2 = LOW;
digitalWrite(M1_Left, LOW);
digitalWrite(M1_Right , LOW);
digitalWrite(M2_Left, LOW);
digitalWrite(M2_Right , LOW);
// Serial.print("Inpin1: ");
// Serial.print(inPin1);
// Serial.print(" ");
// Serial.print("Inpin2: ");
// Serial.println(inPin2);
}

Comments
Post a Comment