Linear Interpolate Seawater Temperatures from Salinity
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* 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("\nEnter 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("\nNew freezing temperature in degrees F: %4.3f \n",f_b);
return 0;
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
Linear Interpolation of Seawater Temperatures in Centigrade
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* 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, f_b_centigrade;
/* 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);
f_b_centigrade = (f_b - 32)/1.8;
/* Print new freezing temperature. */
printf("New freezing temperature in degrees C: %4.3lf \n", f_b_centigrade);
return 0;
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
Linear Interpolation of Salinity
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This program uses linear interpolation to */
/* compute the salinity of seawater at a certain temperature */
#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 temperature values. \n");
printf("Use degrees F for temperatures. \n");
printf("\nEnter first freezing temperature and then salinity: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second freezing temperature and then salinity: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new temperature: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new salinity.
f_b = f_a + (b-a)/(c-a)*(f_c - f_a);
/* Print new salinity. */
printf("\nNew salinity in parts per tenths: %4.2f \n",f_b);
return 0;
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
Bouncy Buttons Lab
const int LED_9 = 9; //The LED is connected to pin 9
const int BUTTON_2 = 2; //The Button is connected to pin 2
const int LED_8 = 8; //The LED is connected to pin 9
const int BUTTON_3 = 3; //The Button is connected to pin 2
boolean lastButton_2 = LOW; //Variable containing the previous button state
boolean currentButton_2 = LOW; //Variable containing the current button state
boolean ledOn_9 = false; //The present state of the LED (on/off)
boolean lastButton_3 = LOW; //Variable containing the previous button state
boolean currentButton_3 = LOW; //Variable containing the current button state
boolean ledOn_8 = false; //The present state of the LED (on/off)
void setup()
{
pinMode (LED_9, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON_2, INPUT); //Set button as input (not required)
pinMode (LED_8, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON_3, INPUT); //Set button as input (not required) }
/* Debouncing Function Pass it the previous button state, * and get back the current debounced button state.*/
boolean debounce_9(boolean last_9)
{
boolean current_9 = digitalRead(BUTTON_2); //Read the button state
if (last_9 != current_9) //if it's different…
{
delay(5); //wait 5ms
current_9 = digitalRead(BUTTON_2); //read it again
return current_9; //return the current value
}
}
boolean debounce_8(boolean last_8)
{
boolean current_8 = digitalRead(BUTTON_3); //Read the button state
if (last_8 != current_8) //if it's different…
{
delay(5); //wait 5ms
current_8 = digitalRead(BUTTON_3); //read it again
return current_8; //return the current value
}
}
void loop()
{
{
currentButton_2 = debounce_9(lastButton_2); //read debounced state
if (lastButton_2 == LOW && currentButton_2 == HIGH) //if it was pressed...
{
ledOn_9 = !ledOn_9; //toggle the LED value }
lastButton_2 = currentButton_2; //reset button value
digitalWrite(LED_9, ledOn_9); //change the LED state
}
{
currentButton_3 = debounce_8(lastButton_3); //read debounced state
if (lastButton_3 == LOW && currentButton_3 == HIGH) //if it was pressed...
{
ledOn_8 = !ledOn_8; //toggle the LED value
}
lastButton_3 = currentButton_3; //reset button value
digitalWrite(LED_8, ledOn_8); //change the LED state
}
}