Monday, September 10, 2018

day 4 9/6/18

Aircraft time, velocity, and acceleration problems
here is the base code:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double itime, velocity, acceleration;
/* Get time value from the keyboard. */
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. */
printf("Velocity = %8.3f m/s \n",velocity);
printf("Acceleration = %8.3f m/sˆ2 \n",acceleration);
/* Exit program. */
return 0;
}


#1
Enter different values of time until you find one that gives a velocity
between 210 and 211 m/s.
ANSWER:
56-58 seconds
#2
Enter different values of time until you find one that gives an acceleration
between 0.5 m/s^2 and 0.6 m/s^2
ANSWER:
24-31 seconds
#3 
Modify the program so that the input values are entered in minutes instead
of seconds. Remember that the equations will still assume that the time values
are in seconds.
Here is the modified code:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double itime, velocity, acceleration;
/* Get time value from the keyboard. */
printf("Enter new time value in minutes: \n");
scanf("%lf",&itime);
/* Compute velocity and acceleration. */
itime = itime*60;
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. */
printf("Velocity = %8.3f m/s \n",velocity);
printf("Acceleration = %8.3f m/sˆ2 \n",acceleration);
/* Exit program. */
return 0;
}

#4
Modify the program so that the output values are printed in feet per second, and
feet per minute(Recall that 1 meter = 39.37 inches)
Here is the modified code (I also added mph and acceleration equivalents):
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double itime, velocity, velocity2, mph, acceleration, acceleration2, mphs;
/* Get time value from the keyboard. */
printf("Enter new time value in minutes: \n");
scanf("%lf",&itime);
/* Compute velocity and acceleration. */
itime = itime*60;
velocity = 0.00001*pow(itime,3) - 0.00488*pow(itime,2)+ 0.75795*itime + 181.3566;
acceleration = 3 - 0.000062*velocity*velocity;
acceleration = acceleration*3.281;
acceleration2 = acceleration*60;
mphs = acceleration2*.0114;
velocity = velocity*3.281;
velocity2 = velocity*60;
mph = velocity2*.0114;
/* Print velocity and acceleration. */
printf("Velocity = %8.3f f/s \n",velocity);
printf("Velocity = %8.3f f/m \n",velocity2);
printf("Velocity = %8.3f mph \n",mph);
printf("Acceleration = %8.3f f/sˆ2 \n",acceleration);
printf("Acceleration = %8.3f f/m/s \n",acceleration2);
printf("Acceleration = %8.3f mph/s \n",mphs);
/* Exit program. */
return 0;
}

Logarithm program problems
#1
Write a program that reads a positive number and then computes and prints the
logarithm of the value to base 2. For example, the logarithm of 8 to base 2 is
3 because 23 = 8.
#include <stdio.h>
#include <math.h>
int main(void)
{
double X, ANSWER;
printf("enter X in expression: log of X base 2\n");
scanf("%lf",&X);
ANSWER = log(X)/log(2);
printf("the answer to log of %5.2f to the base 2 is %5.2f\n", X,ANSWER);
return 0;
}

#2
Write a program that reads a positive number and then computes and prints the
logarithm of the value to base 8. For example, the logarithm of 64 to base 8 is
2 because 82 = 64.
#include <stdio.h>
#include <math.h>
int main(void)
{
double X, ANSWER;
printf("enter X in expression: log of X base 8\n");
scanf("%lf",&X);
ANSWER = log(X)/log(8);
printf("the answer to log of %5.2f to the base 8 is %5.2f\n", X,ANSWER);
return 0;
}

BONUS PROGRAM!!
I also developed a program that takes a second input from the user, so that the
user can now enter the base of the log expression and what they are taking the
log of!
#include <stdio.h>
#include <math.h>
int main(void)
{
double X, B, ANSWER;
printf("enter the base to your log function");
scanf("%lf",&B);
printf("enter X in expression: log of X base %5.2f\n",B);
scanf("%lf",&X);
ANSWER = log(X)/log(B);
printf("the answer to log of %5.2f to the base %5.2f is %5.2f\n", X,B,ANSWER);
return 0;
}

ARDUINO RGB Night Light MODS
#1 and 2
Modify your program to add the colors of Teal, Orange and White.
Implement a switch statement to control the color mode.
The code below includes both!
const int BLED=11; const int GLED=10; const int RLED=9; const int Button=2; boolean lastButton = LOW; boolean currentButton = LOW; int ledMode = 0; void setup() { pinMode(BLED, OUTPUT); pinMode(GLED, OUTPUT); pinMode(RLED, OUTPUT); pinMode(Button, INPUT); } boolean debounce(boolean last){ boolean current = digitalRead(Button); if (last != current){ delay(5); current = digitalRead(Button); } return current; } void setMode(int mode){ switch (mode){ case 1: digitalWrite(RLED, HIGH); digitalWrite(GLED, LOW); digitalWrite(BLED, LOW); break; case 2: digitalWrite(RLED, LOW); digitalWrite(GLED, HIGH); digitalWrite(BLED, LOW); break; case 3: digitalWrite(RLED, LOW); digitalWrite(GLED, LOW); digitalWrite(BLED, HIGH); break; case 4: digitalWrite(RLED, HIGH); digitalWrite(GLED, LOW); digitalWrite(BLED, HIGH); break; case 5: digitalWrite(RLED, LOW); digitalWrite(GLED, HIGH); digitalWrite(BLED, HIGH); break; case 6: analogWrite(RLED, 255); analogWrite(GLED, 127.6); analogWrite(BLED, 0); break; case 7: digitalWrite(RLED, HIGH); digitalWrite(GLED, HIGH); digitalWrite(BLED, HIGH); break; default: digitalWrite(RLED, LOW); digitalWrite(GLED, LOW); digitalWrite(BLED, LOW); break; } } void loop(){ currentButton = debounce(lastButton); if (lastButton == LOW && currentButton == HIGH){ ledMode++; } lastButton = currentButton; if(ledMode == 8){ ledMode = 0;} setMode(ledMode); }

#3
Add additional buttons to independently control each of the three colors.
const int BLED=11;
const int GLED=10;
const int RLED=9;
const int Button=2;
const int Button2=3;
const int Button3=4;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean lastButton3 = LOW;
boolean currentButton3 = LOW;
int ledMode = 0;
int ledMode2 = 0;
int ledMode3 = 0;
void setup() {
  pinMode(BLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  pinMode(Button, INPUT);
}
boolean debounce(boolean last){
  boolean current = digitalRead(Button);
  if (last != current){
    delay(5);
    current = digitalRead(Button);
  }
  return current;
}
boolean debounce2(boolean last2){
  boolean current2 = digitalRead(Button2);
  if(last2 != current2){
    delay(5);
    current2 = digitalRead(Button2);
  }
  return current2;
}
boolean debounce3(boolean last3){
  boolean current3 = digitalRead(Button3);
  if(last3 != current3){
    delay(5);
    current3 = digitalRead(Button3);
  }
  return current3;
}
void setMode (int mode){
  switch (mode){
    case 0:
    digitalWrite(RLED, LOW);
    break;
    case 1:
    digitalWrite(RLED, HIGH);
    break;
  }
}
void setMode2 (int mode){
  switch (mode){
    case 0:
    digitalWrite(GLED, LOW);
    break;
    case 1:
    digitalWrite(GLED, HIGH);
    break;
  }
}
void setMode3 (int mode){
  switch (mode){
    case 0:
    digitalWrite(BLED, LOW);
    break;
    case 1:
    digitalWrite(BLED, HIGH);
    break;
  }
}
void loop(){
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH){
    ledMode++ ;
  }
  lastButton = currentButton;
  if(ledMode == 2){
  ledMode = 0;}
  setMode(ledMode);
   currentButton2 = debounce2(lastButton2);
  if (lastButton2 == LOW && currentButton2 == HIGH){
    ledMode2++ ;
  }
  lastButton2 = currentButton2;
  if(ledMode2 == 2){
  ledMode2 = 0;}
  setMode2(ledMode2);
   currentButton3 = debounce3(lastButton3);
  if (lastButton3 == LOW && currentButton3 == HIGH){
    ledMode3++ ;
  }
  lastButton3 = currentButton3;
  if(ledMode3 == 2){
  ledMode3 = 0;}
  setMode3(ledMode3);
}

#4
Add a blink mode to have your color flash on and off.
For this one I developed 2 different codes, one where the 3 buttons turn on their own blinking light 
and one where 1 button is used to flip through each color from #1 except now, the colors blink.
3 Button blink light sketch:
const int BLED=11;
const int GLED=10;
const int RLED=9;
const int Button=2;
const int Button2=3;
const int Button3=4;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean lastButton3 = LOW;
boolean currentButton3 = LOW;
int ledMode = 0;
int ledMode2 = 0;
int ledMode3 = 0;
void setup() {
  pinMode(BLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  pinMode(Button, INPUT);
}
boolean debounce(boolean last){
  boolean current = digitalRead(Button);
  if (last != current){
    delay(5);
    current = digitalRead(Button);
  }
  return current;
}
boolean debounce2(boolean last2){
  boolean current2 = digitalRead(Button2);
  if(last2 != current2){
    delay(5);
    current2 = digitalRead(Button2);
  }
  return current2;
}
boolean debounce3(boolean last3){
  boolean current3 = digitalRead(Button3);
  if(last3 != current3){
    delay(5);
    current3 = digitalRead(Button3);
  }
  return current3;
}
void setMode (int mode){
  switch (mode){
    case 0:
    digitalWrite(RLED, LOW);
    break;
    case 1:
    digitalWrite(RLED, HIGH);
    delay(100);
    digitalWrite(RLED, LOW);
    delay(100);
    break;
  }
}
void setMode2 (int mode){
  switch (mode){
    case 0:
    digitalWrite(GLED, LOW);
    break;
    case 1:
    digitalWrite(GLED, HIGH);
    delay(100);
    digitalWrite(GLED, LOW);
    delay(100);
    break;
  }
}
void setMode3 (int mode){
  switch (mode){
    case 0:
    digitalWrite(BLED, LOW);
    break;
    case 1:
    digitalWrite(BLED, HIGH);
    delay(100);
    digitalWrite(BLED, LOW);
    delay(100);
    break;
  }
}
void loop(){
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH){
    ledMode++ ;
  }
  lastButton = currentButton;
  if(ledMode == 2){
  ledMode = 0;}
  setMode(ledMode);
   currentButton2 = debounce2(lastButton2);
  if (lastButton2 == LOW && currentButton2 == HIGH){
    ledMode2++ ;
  }
  lastButton2 = currentButton2;
  if(ledMode2 == 2){
  ledMode2 = 0;}
  setMode2(ledMode2);
   currentButton3 = debounce3(lastButton3);
  if (lastButton3 == LOW && currentButton3 == HIGH){
    ledMode3++ ;
  }
  lastButton3 = currentButton3;
  if(ledMode3 == 2){
  ledMode3 = 0;}
  setMode3(ledMode3);
}

1 Button blink light sketch:
const int BLED=11;
const int GLED=10;
const int RLED=9;
const int Button=2;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledMode = 0;
void setup() {
  pinMode(BLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  pinMode(Button, INPUT);
}
boolean debounce(boolean last){
  boolean current = digitalRead(Button);
  if (last != current){
    delay(5);
    current = digitalRead(Button);
  }
  return current;
}
void setMode(int mode){
  switch (mode){
  case 1:
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
    delay(100);
    digitalWrite(RLED, LOW);
    delay(100);
    break;
  case 2:
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, LOW);
    delay(100);
    digitalWrite(GLED, LOW);
    delay(100);
    break;
  case 3:
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, HIGH);
    delay(100);
    digitalWrite(BLED, LOW);
    delay(100);
    break;
  case 4:
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, HIGH);
    delay(100);
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, LOW);
    delay(100);
    break;
  case 5:
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, HIGH);
    delay(100);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
    delay(100);
    break;
  case 6:
    analogWrite(RLED, 255);
    analogWrite(GLED, 127.6);
    analogWrite(BLED, 0); 
    delay(100);
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    delay(100);   
    break;
  case 7:
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, HIGH);
    delay(100);
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
    delay(100);
    break;
  default:
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
    break;
}
}
void loop(){
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH){
    ledMode++;
  }
  lastButton = currentButton;
  if(ledMode == 8){
  ledMode = 0;}
  setMode(ledMode);
}

No comments:

Post a Comment