HW 5
Wave function:
#include <stdio.h>
#include <math.h>
#define PI 3.1415
int main(void)
{
double T1, T2, A1, A2, wl1, wl2, wh1, wh2, wSum=0, wMax=0, wMin=0, t_in, t_fin, t_count, phase;
printf("Input wave 1 period in seconds and wave height in meters: \n");
scanf("%lf", &T1);
scanf("%lf", &wh1);
printf("Input wave 2 period in seconds and wave height in meters: \n");
scanf("%lf", &T2);
scanf("%lf", &wh2);
printf("Input initial time and nubmer of 0.2s increments: \n");
scanf("%lf", &t_in);
scanf("%lf", &t_count);
printf("Input phase shift in multiples of PI: \n");
scanf("%lf", &phase);
A1 = wh1/2;
A2 = wh2/2;
wl1 = (5.13 * pow(T1, 2))*0.3048;
wl2 = (5.13 * pow(T2, 2))*0.3048;
t_fin=t_in+t_count*0.2;
printf("The wavelength of wave 1 is %5.3lf meters.\n", wl1);
printf("The wavelength of wave 2 is %5.3lf meters.\n", wl2);
printf("\nWave height calculations \n\n");
for (t_in; t_in <= t_fin; t_in += 0.2)
{
wh1 = 2*A1*sin(((2*PI)/T1)*t_in);
wh2 = 2*A2*sin((((2*PI)/T2)*t_in)+(PI*phase));
wSum = wh1 + wh2;
if (wSum >= wMax)
{
wMax = wSum;
}
else if (wSum <= wMin)
{
wMin = wSum;
}
printf("Wave 1 height: Wave 2 height: Sum: Time:\n");
printf("%6.3lf %6.3lf %6.3lf %3.1lf\n", wh1, wh2, wSum, t_in);
}
printf("Maximum wave height is %6.3lf m\n", wMax);
printf("Minimum wave height is %6.3lf m\n", wMin);
return 0;
}
Convert ft/s to mi/h
#include <stdio.h>
#include <math.h>
int main(void)
{
int ft;
double mi, conv=0.681818181818;
printf("Table of conversion for ft/s to mi/h\n\n");
for (ft=1; ft <= 100; ft+=1)
{
mi = ft * conv;
printf("Feet per second: Miles per hour: \n");
printf("%15.i %15.3f\n", ft, mi);
}
return 0;
}
TEMP
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
const int TMP = 0; //sensor pin
int TMP_OUT;
int TMP_CAL = 170; //calibration temp
const int RLED = 11;
const int GLED = 10; //LED pins
const int BLED = 9;
int d = 1; //increment led interval
int R = 0;
int G = 0; //LED colors
int B = 0;
const int cal = 2; //calibration button pin
boolean debounce(boolean last)
{
boolean current = digitalRead(cal); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(cal); //read it again
}
return current; //return the current value
}
void setup()
{
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);
pinMode(cal, INPUT);
Serial.begin(9600); //Start Serial Communication
}
void loop()
{
TMP_OUT = analogRead(TMP);
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
TMP_CAL = TMP_OUT;
}
lastButton = currentButton;
if (TMP_OUT - TMP_CAL <= -8)
{
if (R > 10)
{
R -= d;
}
else
{
R = 0;
}
if (G > 10)
{
G -= d;
}
else
{
G = 0;
}
if (B < 250)
{
B += d;
}
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
}
else if (TMP_OUT - TMP_CAL >= 8)
{
if (R < 250)
{
R += d;
}
if (G > 10)
{
G -= d;
}
else
{
G = 0;
}
if (B > 10)
{
B -= d;
}
else
{
B = 0;
}
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
}
else
{
if (R > 10)
{
R -= d;
}
else
{
R = 0;
}
if (G < 250)
{
G += d;
}
if (B > 10)
{
B -= d;
}
else
{
B = 0;
}
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
}
Serial.println(TMP_OUT);
Serial.println(TMP_CAL);
delay (3);
}
#include <stdio.h>
#include <math.h>
#define PI 3.1415
int main(void)
{
double T1, T2, A1, A2, wl1, wl2, wh1, wh2, wSum=0, wMax=0, wMin=0, t_in, t_fin, t_count, phase;
printf("Input wave 1 period in seconds and wave height in meters: \n");
scanf("%lf", &T1);
scanf("%lf", &wh1);
printf("Input wave 2 period in seconds and wave height in meters: \n");
scanf("%lf", &T2);
scanf("%lf", &wh2);
printf("Input initial time and nubmer of 0.2s increments: \n");
scanf("%lf", &t_in);
scanf("%lf", &t_count);
printf("Input phase shift in multiples of PI: \n");
scanf("%lf", &phase);
A1 = wh1/2;
A2 = wh2/2;
wl1 = (5.13 * pow(T1, 2))*0.3048;
wl2 = (5.13 * pow(T2, 2))*0.3048;
t_fin=t_in+t_count*0.2;
printf("The wavelength of wave 1 is %5.3lf meters.\n", wl1);
printf("The wavelength of wave 2 is %5.3lf meters.\n", wl2);
printf("\nWave height calculations \n\n");
for (t_in; t_in <= t_fin; t_in += 0.2)
{
wh1 = 2*A1*sin(((2*PI)/T1)*t_in);
wh2 = 2*A2*sin((((2*PI)/T2)*t_in)+(PI*phase));
wSum = wh1 + wh2;
if (wSum >= wMax)
{
wMax = wSum;
}
else if (wSum <= wMin)
{
wMin = wSum;
}
printf("Wave 1 height: Wave 2 height: Sum: Time:\n");
printf("%6.3lf %6.3lf %6.3lf %3.1lf\n", wh1, wh2, wSum, t_in);
}
printf("Maximum wave height is %6.3lf m\n", wMax);
printf("Minimum wave height is %6.3lf m\n", wMin);
return 0;
}
Convert ft/s to mi/h
#include <stdio.h>
#include <math.h>
int main(void)
{
int ft;
double mi, conv=0.681818181818;
printf("Table of conversion for ft/s to mi/h\n\n");
for (ft=1; ft <= 100; ft+=1)
{
mi = ft * conv;
printf("Feet per second: Miles per hour: \n");
printf("%15.i %15.3f\n", ft, mi);
}
return 0;
}
TEMP
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
const int TMP = 0; //sensor pin
int TMP_OUT;
int TMP_CAL = 170; //calibration temp
const int RLED = 11;
const int GLED = 10; //LED pins
const int BLED = 9;
int d = 1; //increment led interval
int R = 0;
int G = 0; //LED colors
int B = 0;
const int cal = 2; //calibration button pin
boolean debounce(boolean last)
{
boolean current = digitalRead(cal); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(cal); //read it again
}
return current; //return the current value
}
void setup()
{
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);
pinMode(cal, INPUT);
Serial.begin(9600); //Start Serial Communication
}
void loop()
{
TMP_OUT = analogRead(TMP);
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
TMP_CAL = TMP_OUT;
}
lastButton = currentButton;
if (TMP_OUT - TMP_CAL <= -8)
{
if (R > 10)
{
R -= d;
}
else
{
R = 0;
}
if (G > 10)
{
G -= d;
}
else
{
G = 0;
}
if (B < 250)
{
B += d;
}
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
}
else if (TMP_OUT - TMP_CAL >= 8)
{
if (R < 250)
{
R += d;
}
if (G > 10)
{
G -= d;
}
else
{
G = 0;
}
if (B > 10)
{
B -= d;
}
else
{
B = 0;
}
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
}
else
{
if (R > 10)
{
R -= d;
}
else
{
R = 0;
}
if (G < 250)
{
G += d;
}
if (B > 10)
{
B -= d;
}
else
{
B = 0;
}
analogWrite(RLED, R);
analogWrite(GLED, G);
analogWrite(BLED, B);
}
Serial.println(TMP_OUT);
Serial.println(TMP_CAL);
delay (3);
}
Comments
Post a Comment