Homework Number 4
OPEN JET MODIFICATIONS
/* This code will allow you to calculate the acceleration and velocity of a jet
with a parameter of time input. It will allow you to choose your output and input unit
preference */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
char input_method, output_method, more = 'y';
double itime, velocity, acceleration, minutes, velocity_feet_sec, velocity_feet_min, acceleration_feet_sec, acceleration_feet_min;
/* Get time value from the keyboard. */
while (more == 'y'){
printf("Choose input method: (s)econds/(m)inutes");
scanf(" %c", &input_method);
printf("Choose output method: (m)eters/sec, (f)eet/sec, feet/m(i)nute");
scanf(" %c", &output_method);
if (input_method == 'm'){
printf("Enter new time value in minutes: \n");
scanf(" %lf", &minutes);
itime = minutes*60; }
else{
printf("Enter new time value in seconds: \n");
scanf(" %lf",&itime);}
/* Compute velocity and acceleration. */
velocity = 0.00001*pow(itime,3) - 0.00488*pow(itime,2) + 0.75795*itime + 181.3566;
acceleration = 3 - 0.000062*velocity*velocity;
/* Print velocity and acceleration. */
velocity_feet_sec = velocity*3.2808399;
acceleration_feet_sec = acceleration*3.2808399;
velocity_feet_min = (velocity_feet_sec)*60;
acceleration_feet_min = (acceleration_feet_sec)*3600;
if (output_method == 'm'){
printf("Velocity = %8.3f m/s \n",velocity);
printf("Acceleration = %8.3f m/sˆ2 \n",acceleration); }
if (output_method == 'f'){
printf("Velocity = %8.3f f/s \n", velocity_feet_sec);
printf("Acceleration = %8.3f f/s^2 \n", acceleration_feet_sec);}
if (output_method == 'i'){
printf("Velocity = %8.3f f/min \n", velocity_feet_min);
printf("Acceleration = %8.3f f/min^2 \n", acceleration_feet_min);}
/* Exit program. */
printf("Enter a new time and go again? (y/n)");
scanf(" %c", &more);
}
return 0;
}
LOGARITHMIC CODE
/* This code performs logarithmic functions given the base and the number */
#include <stdio.h>
#include <math.h>
int main(void) {
char more = 'y';
float base, number, answer;
while (more == 'y'){
printf("Input the base number:");
scanf(" %f", &base);
printf("Input a positive number:");
scanf(" %f", &number);
answer = log(number)/log(base);
printf("Answer : %2.1f . ( %2.1f ^ %2.1f = %2.1f) \n", answer, base, answer, number);
printf("Again? (y/n)");
scanf(" %c", &more);
}
}
ARDUINO SKETCH FOR LED
/*This sketch allows you to switch through several colors in an RGB led.
One button switches, while 3 buttons control Red, Blue and Green colors. A 5th button
will cause the current color to blink on and off */
const int BLED=6; //Blue LED on Pin 9
const int GLED=5; //Green LED on Pin 10
const int RLED=3; //Red LED on Pin 11
const int BUTTON=13;
const int redbutton = 12;
const int blueutton = 11;
const int greenbutton = 10;
const int blink_button = 8;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
* /*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
//RED
switch (mode){
case 1:
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
//GREEN
case 2:
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
//BLUE
break;
case 3:
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
break;
//PURPLE (RED+BLUE)
case 4:
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
break;
case 5:
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
analogWrite(BLED, 200);
break;
/* teal */
case 6:
analogWrite(RLED, 255);
analogWrite(GLED, 200);
digitalWrite(BLED, LOW);
break;
/*orange*/
case 7:
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
break;
//OFF (mode = 0)
default:
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
}
}
void loop(){
while (digitalRead (12) or digitalRead(11) or digitalRead(10))
{
if (digitalRead(12) == HIGH){
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
return;}
if (digitalRead(11) == HIGH){
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
return;}
if (digitalRead(10) == HIGH){
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
return;}
}
currentButton = debounce(lastButton); //read debounced state
while (digitalRead(blink_button)){
setMode(ledMode);
delay(100);
setMode(30);
delay(100);
}
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 8) ledMode = 0;
setMode(ledMode); //change the LED state
}
const int BLED=6; //Blue LED on Pin 9
const int GLED=5; //Green LED on Pin 10
const int RLED=3; //Red LED on Pin 11
const int BUTTON=13;
const int redbutton = 12;
const int blueutton = 11;
const int greenbutton = 10;
const int blink_button = 8;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
* /*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
//RED
switch (mode){
case 1:
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
//GREEN
case 2:
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
//BLUE
break;
case 3:
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
break;
//PURPLE (RED+BLUE)
case 4:
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
break;
case 5:
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
analogWrite(BLED, 200);
break;
/* teal */
case 6:
analogWrite(RLED, 255);
analogWrite(GLED, 200);
digitalWrite(BLED, LOW);
break;
/*orange*/
case 7:
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
break;
//OFF (mode = 0)
default:
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
}
}
void loop(){
while (digitalRead (12) or digitalRead(11) or digitalRead(10))
{
if (digitalRead(12) == HIGH){
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
return;}
if (digitalRead(11) == HIGH){
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
return;}
if (digitalRead(10) == HIGH){
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
return;}
}
currentButton = debounce(lastButton); //read debounced state
while (digitalRead(blink_button)){
setMode(ledMode);
delay(100);
setMode(30);
delay(100);
}
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 8) ledMode = 0;
setMode(ledMode); //change the LED state
}
Comments
Post a Comment