This first program is the base program where one needs to simply input salinity values in parts per thousand and enter the corresponding freezing temperatures and the program interpolates a new freezing temperature value that corresponds to the new salinity value entered.
/* This program uses linear interpolation to */
/* compute the freezing temperature of seawater. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new salinity: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
/* Print new freezing temperature. */
printf("New freezing temperature in degrees F: %4.1f \n",f_b);
return 0; /* Exit program. */
}
#1
Use the program to determine the freezing temperatures to go with the following salinity
measurements in ppt: 3 8.5 19 23.5 26.8 30.5
new salinity freezing temp (in Fahrenheit)
3 31.7
8.5 31.2
19 30.2
23.5 29.7
26.8 29.4
30.5 29.1
#2
Modify the program so that it converts and prints the new temperature in degrees Centigrade. (Recall
that TF = 9/5 TC + 32
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c, c_b;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new salinity: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
// convert freezing temp to degrees centigrade
c_b=((f_b-32)*5)/9;
/* Print new freezing temperature. */
printf("New freezing temperature in degrees Centigrade: %4.1f \n",c_b);
return 0; /* Exit program. */
}
#3
Suppose that the data used with the program contained values with the degrees in Centigrade. Would
the program need to be changed? Explain.
ANSWER: Using the base program nothing would need to be changed so long as the units stayed
consistent across the entire program
#4
Modify the program so that it interpolates for a salinity, instead of a new freezing temperature. (You
may want to refer to the graph below, which contains a plot of this data with the freezing temperature
on the x-axis and the salinity on the y-axis.)
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first freezing temperature and salinity: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second freezing temperature and salinity: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new freezing temperature: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
/* Print new freezing temperature. */
printf("New freezing temperature in degrees F: %4.1f \n",f_b);
return 0; /* Exit program. */
}
ARDUINO Lab and stuff
#1
This first program we used in Lab was an introduction to the "for" statement in which we used an LED and
slowed down its blink by a millisecond every loop.
const int LED=3;
void setup() {
pinMode (LED, OUTPUT);
}
void loop() {
for(int i=100; i<=1000; i=i+100){
digitalWrite(LED, HIGH);
delay(i);
digitalWrite(LED, LOW);
delay(i);
}
}
#2
This second program utilizes the "for" statement and introduces the "analogWrite" statement to create a
program that dims an LED and brightens it.
const int LED=3;
void setup() {
pinMode (LED, OUTPUT);
}
void loop() {
for (int i=0; i<256; i++){
analogWrite(LED, i);
delay(10);
}
for (int i=255; i>=0; i--){
analogWrite(LED, i);
delay(10);
}
}
#3
This third program contains an LED and a button, and when you hold down on the button the LED illuminates
and when you let go of the button it turns off.
const int LED=3;
const int Button=2;
void setup() {
pinMode (LED, OUTPUT);
pinMode (Button, INPUT);
}
void loop() {
if (digitalRead(Button) ==LOW){
digitalWrite(LED, LOW);
}
else{
digitalWrite(LED, HIGH);
}
}
#4
Modify your program and breadboard to include two buttons and two LEDs. Bring this to class to
demonstrate.
This final program utilizes three buttons, two LED's, and three separate boolean commands to create a
program where the user presses on one button and one LED illuminates and when pressed again turns off the
LED and the second button does the same with another LED; while the third button illuminates both LEDs when
pressed and turns both off when pressed again.
const int LED=5;
const int LED2=6;
const int Button=2;
const int Button2=3;
const int Button3=4;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean ledOn2 = false;
boolean lastButton3 = LOW;
boolean currentButton3 = LOW;
boolean ledOn3 = false;
void setup() {
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(Button, INPUT);
pinMode(Button2, INPUT);
pinMode(Button3, INPUT);
}
boolean debounce(boolean last){
boolean current = digitalRead(Button);
if(last != current){
delay(5);
current = digitalRead(Button);
}
return current;
}
boolean debounce2(boolean last2){
boolean current2 = digitalRead(Button2);
if(last2 != current2){
delay(5);
current2 = digitalRead(Button2);
}
return current2;
}
boolean debounce3(boolean last3){
boolean current3 = digitalRead(Button3);
if(last3 != current3){
delay(5);
current3 = digitalRead(Button3);
}
return current3;
}
void loop() {
currentButton = debounce(lastButton);
if(lastButton == LOW && currentButton == HIGH){
ledOn = !ledOn;
}
lastButton = currentButton;
digitalWrite(LED, ledOn);
currentButton2 = debounce2(lastButton2);
if(lastButton2 == LOW && currentButton2 == HIGH){
ledOn2 = !ledOn2;
}
lastButton2 = currentButton2;
digitalWrite(LED2, ledOn2);
currentButton3 = debounce3(lastButton3);
if(lastButton3 == LOW && currentButton3 == HIGH){
ledOn3 = !ledOn3;
}
lastButton3 = currentButton3;
digitalWrite(LED, ledOn3);
digitalWrite(LED2, ledOn3);
}



No comments:
Post a Comment