Day 12 Oct 5

For homework we had to generate a table based on user input here's the code
//This program prints the altitude and velocity for a ballon over a given amount of time given in hours.


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

#define FILENAME "balloon1.txt"

int main(void){
double t; //t is in hours
double alt_max,sum,start_time,end_time,alt,v, period;
double time_incr,new_incr;
alt_max=0;

FILE *balloon;

balloon=fopen(FILENAME,"w");

printf("Enter the start time,and end time, in hours:\n Values must be BELOW 48 hours \n");
scanf("%lf %lf",&start_time,&end_time);
if(start_time > end_time)
{
printf("Please choose an end time larger than the start time.");
exit();
}

if(start_time<0 || end_time>48)
{
printf("Please enter values that are between 0 and 48");
exit();
}

printf("Enter the time increment in minutes: \n must be BELOW 48 hours \n");
scanf("%lf",&time_incr);



period=end_time-start_time;
new_incr=time_incr/60;

double set_points;
set_points=period/new_incr;

double k;


fprintf(balloon,"\n %.4f \n",set_points);
fprintf(balloon,"\n meters m/s hrs\n");
for(k=0; k<=set_points;k++)

{
t=(start_time + k* new_incr);
alt=-.12*pow(t,4)+12*pow(t,3)-380*t*t+4100*t+220;
v=-.48*pow(t,3)+36*pow(t,2)-760*t+4100;

fprintf(balloon,"\n %.2f m %.2f m/s %.2f hrs \n",alt,v,t);


}
//printf("the new increment is %.2f \n",new_incr);
//printf("the period is %.4f \n",period);
//printf("the set points are %.4f \n",set_points);
//printf("The peak altitude is 4100 meters & it happens at t=0");
fprintf(balloon," -99 -99 -99");
fclose(balloon);




return 0;

}

Here's it working
This is what happens when you choose incorrect values


A printout of the values


                60 

   meters         m/s      hrs

   220 m   4100 m/s   0 hrs 

   559.03 m   4036.92 m/s   0.08 hrs 

   892.83 m   3974.33 m/s   0.17 hrs 

   1221.44 m   3912.24 m/s   0.25 hrs 

   1544.89 m   3850.65 m/s   0.33 hrs 

   1863.23 m   3789.55 m/s   0.42 hrs 

   2176.49 m   3728.94 m/s   0.50 hrs 

   2484.73 m   3668.82 m/s   0.58 hrs 

   2787.98 m   3609.19 m/s   0.67 hrs 

   3086.27 m   3550.05 m/s   0.75 hrs 

   3379.66 m   3491.39 m/s   0.83 hrs 

   3668.19 m   3433.21 m/s   0.92 hrs 

   3951.88 m   3375.52 m/s   1 hrs 

   4230.79 m   3318.31 m/s   1.08 hrs 

   4504.94 m   3261.57 m/s   1.17 hrs 

   4774.39 m   3205.31 m/s   1.25 hrs 

   5039.18 m   3149.53 m/s   1.33 hrs 

   5299.33 m   3094.22 m/s   1.42 hrs 

   5554.89 m   3039.38 m/s   1.50 hrs 

   5805.91 m   2985.01 m/s   1.58 hrs 

   6052.41 m   2931.11 m/s   1.67 hrs 

   6294.44 m   2877.68 m/s   1.75 hrs 

   6532.03 m   2824.71 m/s   1.83 hrs 

   6765.23 m   2772.20 m/s   1.92 hrs 

   6994.08 m   2720.16 m/s   2 hrs 

   7218.61 m   2668.58 m/s   2.08 hrs 

   7438.86 m   2617.45 m/s   2.17 hrs 

   7654.86 m   2566.78 m/s   2.25 hrs 

   7866.67 m   2516.57 m/s   2.33 hrs 

   8074.30 m   2466.81 m/s   2.42 hrs 

   8277.81 m   2417.50 m/s   2.50 hrs 

   8477.23 m   2368.64 m/s   2.58 hrs 

   8672.60 m   2320.23 m/s   2.67 hrs 

   8863.95 m   2272.27 m/s   2.75 hrs 

   9051.32 m   2224.75 m/s   2.83 hrs 

   9234.75 m   2177.67 m/s   2.92 hrs 

   9414.28 m   2131.04 m/s   3 hrs 

   9589.94 m   2084.85 m/s   3.08 hrs 

   9761.77 m   2039.09 m/s   3.17 hrs 

   9929.80 m   1993.77 m/s   3.25 hrs 

   10094.07 m   1948.89 m/s   3.33 hrs 

   10254.63 m   1904.44 m/s   3.42 hrs 

   10411.49 m   1860.42 m/s   3.50 hrs 

   10564.71 m   1816.83 m/s   3.58 hrs 

   10714.31 m   1773.67 m/s   3.67 hrs 

   10860.33 m   1730.94 m/s   3.75 hrs 

   11002.81 m   1688.63 m/s   3.83 hrs 

   11141.78 m   1646.74 m/s   3.92 hrs 

   11277.28 m   1605.28 m/s   4 hrs 

   11409.34 m   1564.24 m/s   4.08 hrs 

   11538.00 m   1523.61 m/s   4.17 hrs 

   11663.29 m   1483.40 m/s   4.25 hrs 

   11785.24 m   1443.61 m/s   4.33 hrs 

   11903.90 m   1404.23 m/s   4.42 hrs 

   12019.29 m   1365.26 m/s   4.50 hrs 

   12131.45 m   1326.70 m/s   4.58 hrs 

   12240.42 m   1288.55 m/s   4.67 hrs 

   12346.22 m   1250.81 m/s   4.75 hrs 

   12448.90 m   1213.47 m/s   4.83 hrs 

   12548.48 m   1176.53 m/s   4.92 hrs 

   12645 m   1140 m/s   5 hrs 
     -99           -99       -99



For the lab we had to link two buttons to an lcd and ic2 temp sensor, where when a button is pressed it would change the temperature from celsius to fahrehnheit.

Here's the code
//LCD text with incrementing number
//Include the library code:
#include <LiquidCrystal.h>
//Start the time at 0
int time = 0;
//const int POT = 1;
int temp_val;
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{


  Serial.begin(9600);
  //Set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  //Print a message to the LCD.
  lcd.setCursor(3, 0);
  lcd.print("Temp. ");
}
void loop()
{
  //Move cursor to second line, first position
  lcd.setCursor(9, 0);
  //Print Current Time
  int button = analogRead(A0);




  temp_val = analogRead(A1);

  delay(500);


  int temp_f=temp_val+51;
  int temp_c=temp_val+1;
  Serial.println(temp_val);
  Serial.print(" f temp ");
  Serial.println(temp_f);
  Serial.print(" c temp ");
  Serial.println(temp_c);
  if (button == 503)
  {
    lcd.setCursor(9, 0);
//    sensorValue = analogRead(POT);

    lcd.print(temp_f);
    delay(500);
    lcd.print("  F ");


  }
   if(button==0)
  {

    lcd.setCursor(9, 0);
//    sensorValue = analogRead(POT);
    lcd.print(temp_c);
    delay(500);
    lcd.print("  C ");
  }
  else
  {
 
    lcd.setCursor(3, 1);
// 
    lcd.print("l is f r is c");
    delay(500);
  }
//





Serial.println(button);

}






Here's a link to the LCD working.


Comments

Popular posts from this blog

Day 5

Day 22 Nov 14

Day 21 Nov 9