Homework Number 3
//Arduino sketch for de-bouncing 2 buttons, with 1 button being a touch sensor
int touch = 7;
int red = 10;
int button = 2;
int led = 3;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
boolean lastTouch = LOW;
boolean currentTouch= LOW;
boolean touchon = false;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button, INPUT);
pinMode(red, OUTPUT);
pinMode(touch, INPUT);
}
boolean debounce (boolean last){
boolean current = digitalRead(button);
if (last != current){
delay(5);
current = digitalRead(button);
return current;
}
}
boolean debounce_2 (boolean last) {
boolean current = digitalRead(touch);
if (last != current){
delay(5);
current = digitalRead(touch);
return current;
}
}
void loop() {
// put your main code here, to run repeatedly:
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH){
ledOn = !ledOn;
}
lastButton = currentButton; //reset button value
digitalWrite(led, ledOn); // change the led state
currentTouch = debounce_2(lastTouch);
if (lastTouch == LOW && currentTouch == HIGH){
touchon = !touchon;
}
lastTouch = currentTouch; // reset touch sensor value
digitalWrite(red, touchon); // change the red led status
}
int touch = 7;
int red = 10;
int button = 2;
int led = 3;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
boolean lastTouch = LOW;
boolean currentTouch= LOW;
boolean touchon = false;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button, INPUT);
pinMode(red, OUTPUT);
pinMode(touch, INPUT);
}
boolean debounce (boolean last){
boolean current = digitalRead(button);
if (last != current){
delay(5);
current = digitalRead(button);
return current;
}
}
boolean debounce_2 (boolean last) {
boolean current = digitalRead(touch);
if (last != current){
delay(5);
current = digitalRead(touch);
return current;
}
}
void loop() {
// put your main code here, to run repeatedly:
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH){
ledOn = !ledOn;
}
lastButton = currentButton; //reset button value
digitalWrite(led, ledOn); // change the led state
currentTouch = debounce_2(lastTouch);
if (lastTouch == LOW && currentTouch == HIGH){
touchon = !touchon;
}
lastTouch = currentTouch; // reset touch sensor value
digitalWrite(red, touchon); // change the red led status
}
LINEAR INTERPOLATION
/* This program uses linear interpolation to */
/* compute the freezing temperature of seawater. */
/*#3. If the program contained data in Centigrade, the program would be changed so that the input values were converted to Centigrade before interpolating. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
char interpolate, output_method;
double sal_a, fp_a, sal_b, fp_b, sal_c, fp_c;
char more = 'y';
/* Get user input from the keyboard. */
while (more ='y') {
printf("Would you like to interpolate for (f)reezing temperature or (s)alinity?");
scanf(" %c", &interpolate);
printf("Enter first data point (salinity and then freezing temperature): \n");
scanf("%lf %lf",&sal_a,&fp_a);
printf("Enter second data point (salinity and then freezing temperature): \n");
scanf("%lf %lf",&sal_c,&fp_c);
if (interpolate == 'f'){
printf("Enter new salinity: \n");
scanf("%lf",&sal_b);
// Use linear interpolation to compute new freezing temperature.
fp_b = fp_a + (sal_b-sal_a)/(sal_c-sal_a)*(fp_c - fp_a);
/* Print new freezing temperature. */
printf("Enter ouput method: (c)entigrade, (f)ahrenheit, (b)oth");
scanf(" %c", &output_method);
if (output_method == 'f'){
printf("New freezing temperature in degrees F: %4.1f \n",fp_b); }
if (output_method == 'c'){
printf("New freezing temperature in degrees C: %4.1f \n", (fp_b-32)*5/9); }
if (output_method == 'b'){
printf("New freezing temperature in degrees F: %4.1f \n",fp_b);
printf("New freezing temperature in degrees C: %4.1f \n", (fp_b-32)*5/9); }
}
if (interpolate =='s'){
printf("Enter new freezing point: \n");
scanf(" %lf", &fp_b);
sal_b = sal_a + ((fp_b - fp_a)/(fp_c - fp_a))*(sal_c-sal_a);
printf("New Salinity in parts per thousand: %3.1f \n",
sal_b);
}
printf("Would you like to interpolate again? (y/n)");
scanf(" %c", &more);
}
return 0; /* Exit program. */
}
Comments
Post a Comment