Lab 5/ HW 5
Lab 5 (Potentiometer Reading):
//Potentiometer Reading Program
const int TempSense=0; //Temperature sensor on analog pin 0
const int RLED = 11; //Blue LED on Pin 9
const int GLED = 10; //Green LED on Pin 10
const int BLED = 9; //Red LED on Pin 11
int val = 0; //variable to hold the analog reading from the POT
void setup() {
Serial.begin(9600); //Start Serial Communication
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
void loop() {
val = analogRead(TempSense); //Read one value from the POT
Serial.println(val); //print it to the serial port
delay(700);
if(val>150) //if temp > 146, turn LED Red
{
analogWrite(RLED, 255);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
}
if(val<=145) //if temp <= 145, turn LED Green
{
analogWrite(RLED, 0);
analogWrite(GLED, 255);
analogWrite(BLED, 0);
}
if(val<140) //if temp < 140, turn LED Blue
{
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 255);
}
}
HW (Wavelength):
#include <stdio.h>
#include <math.h>
float period1, period2, wavelength1, wavelength2, waveheight1, waveheight2, newperiod, time, sum, increment, frequency1, frequency2, wave1, wave2, new_height, low_height;
float PI = 3.141593;
int main(void)
{
printf("Enter integer wave period (s) and wave height (ft) for wave 1:\n");
scanf("%f %f", &period1, &waveheight1);
printf("Enter integer wave period (s) and wave height (ft) for wave 2:\n");
scanf("%f %f", &period2, &waveheight2);
wavelength1 = 5.13*pow(period1,2);
wavelength2 = 5.13*pow(period2,2);
newperiod = period1*period2;
increment = newperiod/200;
frequency1 = 1/period1;
frequency2 = 1/period2;
int time = 0;
float wavemax = 0;
int steps = 0;
float sum = 0;
float wavemin = 0;
for (steps=0; steps<= 200; steps++)
{
time = steps*increment;
wave1 = waveheight1*sin(2*PI*frequency1*time);
//printf("%f \n", wave1);
wave2 = waveheight2*sin(2*PI*frequency2*time);
sum = wave1 + wave2;
//printf("%f \n", sum);
if (sum > wavemax)
{wavemax=sum;}
if (sum < wavemin)
{wavemin=sum;}
}
new_height = wavemax;
low_height = wavemin;
printf("Wavelength1: %.4f ft \nWavelength2: %.4f ft \nMax Wave Height(Maximum Crescent): %.4f\nMinimum Trough: %.4f ft", wavelength1, wavelength2, new_height, low_height);
return 0;
}
Comments
Post a Comment