Arduino homework
1. Interface your servo with the arduino according to the directions in the previous lab. Allow the user
to enter an angle and the servo turns to that angle. If the angle entered is invalid a red light should light
on the breadboard.
#include <Servo.h>
const int SERVO = 9;
const int RLED = 10;
const int GLED = 11;
int servoval = 0;
int val = 0;
Servo Servo;
void setup()
{
Serial.begin(9600);
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
Servo.attach(SERVO);
Serial.println("Enter servo degree value between 0 and 180");
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
}
void loop()
{
//Keep working as long as data is in the buffer
while (Serial.available() > 0)
{
servoval = Serial.parseInt();
if (Serial.read() == '\n') //Done transmitting
{
//set servo
if (servoval <= 180 && servoval >= 0){
Serial.println(servoval);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
Servo.write(servoval);
}
else {
Serial.println("Not a value between 0 and 180, try again");
val++;
while (val == 1){
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
delay(150);
digitalWrite(RLED, LOW);
delay(150);
servoval = Serial.parseInt();
if (Serial.read() == '\n'){//Done transmitting
if (servoval <= 180 && servoval >= 0){
Serial.println(servoval);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
Servo.write(servoval);
val--;
}
}
}
}
}
}
}
Balloon programming problems
a. Write a program that will print a table of the altitude and the velocity for this weather
balloon using units of meters and meters per second. Let the user enter the start time, the
increment in time between lines of the table, and the ending time, where all the time values must be
less than 48 hours. Use the program to generate a table showing the weather balloon information every
10 minutes over a 2-hour period, starting 4 hours after the balloon was launched.
b. Modify the program above so that it also prints the peak altitude and its corresponding time.
c. Modify the program so that it will check to be sure that the final time is greater than the initial time. If
it is not, ask the user to reenter the complete set of report information.
d. The preceding equations are accurate only for the time from 0 to 48 hours, so modify the program
generated above so that it prints a message to the user that specifies an upper bound of 48 hours. Also,
check the user input to make sure that it stays within the proper bounds. If there are errors, ask the user
to reenter the complete set of report information.
e. Modify the program above so that it stores the time, altitude, and velocity information in a data file
named .
#include <stdio.h>
#include <math.h>
#define FILENAME "ballon1.txt"
double starttime, endtime, timeincr, t, altitude1, velocity, maxwave = 0, maxwavetime = 0;
char minorhour;
int main(void){
FILE *balloon;
balloon = fopen(FILENAME,"w");
_Bool keep_looping = 1;
_Bool keep_looping2 = 1;
_Bool keep_looping3 = 1;
printf("This program will print a table of the altitudes and velocities of a weather balloon at a given time interval in hours (within 48)\n");
while (keep_looping == 1){
while (keep_looping2 == 1){
printf("Enter a start time\n");
scanf("%lf",&starttime);
if(starttime < 0){
printf("The start time cannot be negative,\nplease reenter all credentials\n\n");
}
if(starttime >= 48){
printf("The start time cannot be equal to or over 48,\nplease reenter all credentials\n\n");
}
if(starttime >= 0 && starttime < 48){
keep_looping2 = 0;
}
}
printf("\nEnter an end time\n");
scanf("%lf",&endtime);
if(starttime > endtime){
printf("The start time cannot be after the end time,\nplease reenter all credentials\n\n");
keep_looping2 = 1;
}
if(starttime == endtime){
printf("The start time cannot equal the end time,\nplease reenter all credentials\n\n");
keep_looping2 = 1;
}
if(endtime < 0){
printf("the end time cannot be negative,\nplease reenter all credentials\n\n");
}
if(endtime >48){
printf("the end time cannot be over 48 hours,\nplease reenter all credentials\n\n");
}
if(starttime < endtime && endtime > 0 && endtime <= 48){
keep_looping = 0;
}
}
printf("\nEnter a time increment\n");
scanf("%lf",&timeincr);
while (keep_looping3 == 1){
printf("Please specify if it is in minutes or hour (m or h)\n");
scanf(" %c",&minorhour);
if(minorhour != 109 || minorhour != 77 || minorhour != 104 || minorhour != 72){
printf("time interval must be in either minutes or hours (m or h),\nplease reenter all credentials\n\n");
}
if(minorhour == 109 || minorhour == 77 || minorhour == 104 || minorhour == 72){
keep_looping3 = 0;
}
}
printf("Altitudes (in meters):\n");
if(minorhour == 109 || minorhour == 77){
timeincr = timeincr / 60;
}
int timedivider = (endtime-starttime) / timeincr;
int time[100], velocity[100], altitude[100];
fprintf(balloon, "Altitudes:\n");
for (t = starttime; t <= endtime; t = t + timeincr){
int i = 0;
altitude[i] = (-.12 * pow(t,4)) + (12 * pow(t,3)) - (380 * pow(t,2)) + (4100 * t) + 220;
altitude1 = (-.12 * pow(t,4)) + (12 * pow(t,3)) - (380 * pow(t,2)) + (4100 * t) + 220;
printf("%d\n", altitude[i]);
fprintf(balloon, "%d\n", altitude[i]);
if (altitude1 >maxwave){
maxwave = altitude1;
maxwavetime = t;
}
i++;
}
fprintf(balloon, "Velocities:\n");
printf("Velocities (in meters per second):\n");
for (t = starttime; t <= endtime; t = t + timeincr){
int j = 0;
velocity[j] = (-.48 * pow(t,3)) + (36 * pow(t,2)) - (760 * t) + 4100;
printf("%d\n", velocity[j]);
fprintf(balloon, "%d\n", velocity[j]);
j++;
}
fprintf(balloon, "Times:\n");
printf("times:\n");
for (t = starttime; t <= endtime; t = t + timeincr){
int h = 0;
time[h] = t;
printf("%d\n", time[h]);
fprintf(balloon, "%d\n", time[h]);
h++;
}
/*fprintf(balloon, "Altitude Velocity Time\n");
for (int k = 0; k <= timedivider; k++){
fprintf(balloon, "%d %d %d\n", altitude[k], velocity[k], time[k]);
}*/
printf("the peak altitude was %f meters at time %f hours", maxwave, maxwavetime);
fclose(balloon);
return 0;
}
No comments:
Post a Comment