Lab 3/ HW 3 (9/6)

//Program to turn on/off two LEDs with buttons
const int LED1=9;                                    //The LED is connected to pin 9
const int LED2=7;
const int BUTTON1=2;                                 //The Button is connected to pin 2
const int BUTTON2=4;
boolean lastButton = LOW;                           //Variable containing the previous button state
boolean currentButton = LOW;                        //Variable containing the current button state
boolean ledOn = false;                              //The present state of the LED (on/off)
void setup()
{
 pinMode (LED1, OUTPUT);                             //Set the LED pin as an output
 pinMode (LED2, OUTPUT);
 pinMode (BUTTON1, INPUT);                           //Set button as input (not required)
 pinMode (BUTTON2, INPUT);
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON1);      //Read the button state
 boolean current2 = digitalRead(BUTTON2);
 if (last != current)                         //if it's different…
 {
 delay(5);                           //wait 5ms
 current = digitalRead(BUTTON1);     //read it again
 current2 = digitalRead(BUTTON2);
 return current;                    //return the current value
 return current2;
  }
}
void loop()
{
 currentButton = debounce(lastButton);                  //read debounced state
 if (lastButton == LOW && currentButton == HIGH)        //if it was pressed...
 {
 ledOn = !ledOn;                                         //toggle the LED value
 }
 lastButton = currentButton;        //reset button value
 digitalWrite(LED1, ledOn);         //change the LED state
 digitalWrite(LED2, ledOn);
}


/* This program uses linear interpolation to */
/* compute the Salinity of seawater. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double salinity_a, freezing_a, salinity_b, freezing_b, salinity_c, freezing_c, fb_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",&salinity_a,&freezing_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&salinity_c,&freezing_c);
printf("Enter new freezing temp: \n");
scanf("%lf",&freezing_b);

// Use linear interpolation to compute new freezing temperature.
salinity_b = ((freezing_b-freezing_a)/(freezing_c-freezing_a))*(salinity_c-salinity_a) + salinity_a;
/* convert freezing temp to Celsius */
/*fb_C = (f_b-32)*5/9;

/* Print new freezing temperature. */
printf("New Salinity:""%4.1f \n",salinity_b);
return 0; /* Exit program. */
}




Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21