Lab 9/ HW 9

Lab 9 (Sweeping IR):

#include <Servo.h>
const int servo=10;
const int gled=9;
const int rled=8;
Servo myServo;
int val = 0;
int del = 0;
void setup()
{
 pinMode(gled, OUTPUT);
 pinMode(rled, OUTPUT);
 myServo.attach(servo);
 Serial.begin(9600);
}
void loop()
{
 while (Serial.available() > 0)
{
  val = Serial.parseInt();
  if (val <= 180)
  {
    Serial.println(val);
    myServo.write(val); //sets the servo
    digitalWrite(gled, HIGH);
    digitalWrite(rled, LOW);
  }
   if (val > 180)
  {
    Serial.println(val);
    digitalWrite(rled, HIGH);
    digitalWrite(gled, LOW);
  }
}
}


HW 9 (Balloon Data):

#include <stdio.h>
#include <math.h>
#define FILENAME "balloon.txt"

int main(void)
{
/* Declare variables */
double starttime, endtime, meters, velocity, velocity_in_min, k, maxtime;
double max = 0;
FILE *balloon;
/* Open output file. */
balloon = fopen(FILENAME, "w");
/*Obtain user inputs */
printf("Enter the start time in hours:\n");
scanf("%lf", &starttime);
printf("Enter the end time in hours:\n");
scanf("%lf", &endtime);
/*If input is invalid...*/
while (starttime>endtime)
{
printf("Start time cannot be greater than end time, re-enter values.\n");
printf("Enter the start time in hours:\n");
scanf("%lf", &starttime);
printf("Enter the end time in hours:\n");
scanf("%lf", &endtime);
}
while (starttime >= 48 || endtime > 48)
{
printf("Program only computes from 0 to 48 hours, re-enter values.\n");
printf("Enter the start time in hours:\n");
scanf("%.4lf", &starttime);
printf("Enter the end time in hours:\n");
scanf("%.4lf", &endtime);
}
/* Computation */
for (k = starttime; k <= endtime+0.166667; k+=0.166667)
{
meters = -0.12*pow(k,4) + 12*pow(k,3) - 380*pow(k,2) + 4100*(k) + 220;
velocity = -0.48*pow(k,3) +36*pow(k,2) - 760*(k) + 4100;
velocity_in_min = velocity/3600;
printf("%.4lf %.2lf %.2lf \n", k, meters, velocity_in_min);
/*Print to file */
fprintf(balloon,"%.4lf %.2lf %.2lf \n", k, meters, velocity_in_min);
if (max < meters)
{
maxtime = k;
max = meters;
}
}
/*Print Max Altitude and Corresponding Time*/
printf("Max Altitude(m): %.2lf\n",max);
printf("Corresponding Time(s): %.2lf",maxtime);
/* Print to file */
fprintf(balloon,"Max Altitude(m): %.2lf\n",max);
fprintf(balloon,"Corresponding Time(s): %.2lf",maxtime);
/* Close file */
fclose(balloon);
return 0;

}

Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21