Wednesday, September 19, 2018

day 7 9/18/18

Random dice problem and factorial approximation problem
#1
Write a program to simulate an experiment rolling two six-sided “fair” dice. Allow the user to enter the number of rolls of the dice to simulate. What percentage of the time does the sum of the dots on the dice equal 8 in the simulation?
ANSWER:
about 8% which includes combinations (2,6), (3,5), (4,4), (5,3), (6,2) out of 72 combinations
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/* Declare variables and function prototype. */
unsigned int seed;
int a=1, b=6, sum, dice1, dice2, k;
int rand_int(int a,int b);
/* Get seed value and interval limits. */
printf("Enter a positive integer seed value: \n");
scanf("%u",&seed);
srand(seed);
//printf("Enter integer limits a and b (a<b): \n");
//scanf("%i %i",&a,&b);
printf("Enter the amount of times to roll the dice: \n");
scanf("%i", &k);
/* Generate and print ten random numbers. */
for (int i=1; i<=k; i++){
dice1 = rand_int(a,b);
dice2 = rand_int(a,b);
printf("Random dice number 1: \n");
printf("%i ",dice1);
printf("\n");
printf("Random dice number 2: \n");
printf("%i ",dice2);
printf("\n");
sum = dice1 + dice2;
printf("the sum of the dice is: %i \n",sum);}
/* Exit program. */
return 0;
}
/* This function generates a random integer */
/* between specified limits a and b (a<b). */
int rand_int(int a,int b)
{
return rand()%(b-a+1) + a;
}



#2
 A convenient approximation for computing the factorial n! for large values of n is given by the Stirling formula: Write an integer function for computing this approximation to a factorial. Assume that the corresponding prototype is int n_fact(int n);
/* This program compares a recursive function and */
/* a nonrecursive function for computing factorials. */
#include <stdio.h>
#include <math.h>
#define PI 3.1415
#define e 2.7182
int main(void)
{
/* Declare variables and function prototypes. */
int n;
double factorial(int k);
double factorial_r(int k);
int n_fact(int n);
/* Get user input. */
printf("Enter positive integer: \n");
scanf("%i",&n);
/* Compute and print factorials. */
printf("sterling formula for the factorial: %i! = %i",n,n_fact(n));
//printf("Nonrecursive: %i! = %f \n",n,factorial(n));
//printf("Recursive: %i! = %f \n",n,factorial_r(n));
/* Exit program. */
return 0;
}
int n_fact(int n){
n = ceil((sqrt(2*PI*n))*(pow((n/e),n)));
return n;
}

Arduino speaker and servo/PIR sensor
#1
Combine your servo and your PIR sensor. Tape a large cardboard arrow on top of the servo. When someone walks into the room, have the servo point a sign that reads "occupied". When no person is detected, have the servo point to a sign that says "empty" If you wish to be more creative and use something other than an arrow and have other signs that is great!
I developed 2 programs one where the sensor is triggered once and the servo moves from unoccupied
to occupied and back when the sensor is triggered only once; the other program is triggered once and
moves from unoccupied to occupied and needs to be triggered again to go back to unoccupied.
the first one
#include <Servo.h> const int SERVO=9; //Servo on Pin 9 const int PIR=2; Servo myServo; int val = 0; //for storing the reading from the POT void setup() { pinMode (PIR, INPUT); myServo.attach(SERVO); Serial.begin(9600); } void loop() { if (digitalRead(PIR) == HIGH) { myServo.write(80); Serial.println("occupied"); delay(500); } else if (digitalRead(PIR) == LOW) { myServo.write(0); Serial.println("unoccupied"); delay(500); } }

the second one

#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2;
Servo myServo;
int val = 1; 
void setup()
{
 pinMode (PIR, INPUT);
 myServo.attach(SERVO);
 Serial.begin(9600);
}
void loop()
{
 while (val == 1)
 {
 if (digitalRead(PIR) == HIGH)
 {
 myServo.write(80);
 Serial.println("triggered");
 delay(2000);
 val++;
 Serial.println(val);
 while (val == 2)
 {
 Serial.println("occupied");
 if (digitalRead(PIR) == HIGH)
 {
 myServo.write(0);
 delay(500);
 val--;
 Serial.println(val);
 }
 }
 }
 val--;
 }
 if (digitalRead(PIR) == LOW)
 {
 myServo.write(0);
 Serial.println("unoccupied");
 delay(500);
 val++;
 }
 Serial.println(val);
}

#2
See if you can modify this program to play a simple song.
I made when you wish upon a star
const int kSpkr = 9; #define NOTE_C4 262 #define NOTE_D4 294 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_F45 370 #define NOTE_G4 392 #define NOTE_A4 440 #define NOTE_Bb4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_D5 587 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_G5 784 #define NOTE_C6 1047 void setup() { pinMode(kSpkr, OUTPUT); } void loop() { tone(kSpkr, NOTE_C4, 500); delay(500); tone(kSpkr, NOTE_C5, 500); delay(500); tone(kSpkr, NOTE_Bb4, 500); delay(500); tone(kSpkr, NOTE_A4, 500); delay(500); tone(kSpkr, NOTE_F45, 500); delay(500); tone(kSpkr, NOTE_G4, 500); delay(500); tone(kSpkr, NOTE_D5, 1000); delay(500); noTone(kSpkr); delay(50); tone(kSpkr, NOTE_E4, 500); delay(500); tone(kSpkr, NOTE_E5, 500); delay(500); tone(kSpkr, NOTE_D5, 500); delay(500); tone(kSpkr, NOTE_C5, 500); delay(500); tone(kSpkr, NOTE_B4, 500); delay(500); tone(kSpkr, NOTE_C5, 500); delay(500); tone(kSpkr, NOTE_F5, 1000); delay(500); noTone(kSpkr); delay(50); tone(kSpkr, NOTE_G5, 500); delay(500); tone(kSpkr, NOTE_F5, 500); delay(550); tone(kSpkr, NOTE_E5, 500); delay(550); tone(kSpkr, NOTE_D5, 500); delay(550); tone(kSpkr, NOTE_C5, 500); delay(550); tone(kSpkr, NOTE_Bb4, 550); delay(600); tone(kSpkr, NOTE_A4, 600); delay(650); tone(kSpkr, NOTE_G4, 650); delay(700); tone(kSpkr, NOTE_D5, 700); delay(750); tone(kSpkr, NOTE_G5, 700); delay(750); tone(kSpkr, NOTE_C6, 1500); delay(2000); }

Monday, September 17, 2018

day 6 9/13/18

Great Circle Distance Problems
#1,2,3,4,and5
#include <stdio.h>
#include <math.h>
#define PI 3.141593
double angle(double, double, double, double, double, double);
int main(void)
{
/* Declare variables and function prototype. */
double lat1, long1, lat2, long2;
char direction, direction2, direction3, direction4;
double gc_distance(double lat1,double long1,
double lat2,double long2);
/* Get locations of two points. */
printf("Enter latitude and longitude west ");
printf("for location 1: \n");
scanf("%lf %lf",&lat1,&long1);
printf("is the latitude N or S\n");
scanf(" %c", &direction);
printf("is the longitude W or E\n");
scanf(" %c", &direction3);
printf("Enter latitude and longitude west ");
printf("for location 2: \n");
scanf("%lf %lf",&lat2,&long2);
printf("is the latitude N or S\n");
scanf(" %c", &direction2);
printf("is the longitude W or E\n");
scanf(" %c", &direction4);
if(direction==78){
lat1 = lat1*1;
}
else if(direction==83){
lat1 = lat1*-1;
}
if(direction2==78){
lat2 = lat2*1;
}
else if(direction2==83){
lat2 = lat2*-1;
}
if(direction3==69){
long1 = 360 - long1;
}
else if(direction3==87){
long1 = long1*1;
}
if(direction4==69){
long2 = 360 - long2;
}
else if(direction4==87){
long2 = long2*1;
}
/* Print great circle distance. */
printf("Great Circle Distance: %.0f miles \n",
gc_distance(lat1,long1,lat2,long2));
/* Exit program. */
return 0;
}
/* This function computes the distance between two */
/* points using great circle distances. */
double gc_distance(double lat1,double long1,
double lat2,double long2)
{
/* Declare variables. */
double rho, phi, theta, gamma, x, y, z, X, Y, Z;
/* Convert latitude,longitude to rectangular coordinates. */
rho = 3960;
phi = (90 - lat1)*(PI/180.0);
theta = (360 - long1)*(PI/180.0);
x = rho*sin(phi)*cos(theta);
y = rho*sin(phi)*sin(theta);
z = rho*cos(phi);
phi = (90 - lat2)*(PI/180.0);
theta = (360 - long2)*(PI/180.0);
X = rho*sin(phi)*cos(theta);
Y = rho*sin(phi)*sin(theta);
Z = rho*cos(phi);
/* Compute angle between vectors. */
gamma = angle(x,y,z,X,Y,Z);
/* Compute and return great circle distance. */
return gamma*rho;
}
double angle(double x1, double y1, double z1, double x2, double y2, double z2){
double dot, dist1, dist2, gamma;
dot = x1*x2 + y1*y2 + z1*z2;
dist1 = sqrt(x1*x1 + y1*y1 + z1*z1);
dist2 = sqrt(x2*x2 + y2*y2 + z2*z2);
gamma = acos(dot/(dist1*dist2));
return gamma;
}

Arduino Light and Motion Sensor
CLASSWORK
This program senses the amount of light coming into the sensor and as the sensor is
covered into darkness it illuminates the rgb more and more.
const int RLED=9; //Red LED on pin 9 (PWM) const int LIGHT=A0; //Lght Sensor on analog pin 0 const int MIN_LIGHT=200; //Minimum expected light value const int MAX_LIGHT=900; //Maximum Expected Light value int val = 0; //variable to hold the analog reading void setup() { pinMode(RLED, OUTPUT); //Set LED pin as output Serial.begin(9600); } void loop() { val = analogRead(LIGHT); //Read the light sensor val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0);//Map the light reading val = constrain(val, 0, 255); //Constrain light value analogWrite(RLED, val); //Control the LED Serial.println(val); }

HOMEWORK
Connect your PIR sensor and your CdS cell(light sensor). The PIR and CdS sensors will control the LED behavior as follows: When the PIR is triggered the LED is on. When the CdS is uncovered and the PIR sensor is untriggered, the LED is off. When the CdS cell is covered and the PIR sensor is off, the LED blinks.
const int LED=9;
const int PIR=2;
const int LIGHT=A0; //Lght Sensor on analog pin 0
const int MIN_LIGHT=200; //Minimum expected light value
const int MAX_LIGHT=900; //Maximum Expected Light value
int val = 0; //variable to hold the analog 
void setup()
{
 pinMode (LED, OUTPUT);
 pinMode (PIR, INPUT);
 Serial.begin(9600);
}
void loop()
{
 val = analogRead(LIGHT); //Read the light sensor
 val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0);//Map the light reading
 val = constrain(val, 0, 255); //Constrain light value
 Serial.println(val);
 if (val == 255){
   digitalWrite(LED, HIGH);
   delay(100);
   digitalWrite(LED, LOW);
   delay(100);
 }
 else{
   digitalWrite(LED, LOW);
 }
 if (digitalRead(PIR) == LOW)
 {
 digitalWrite(LED, LOW);
 }
 else
 {
 digitalWrite(LED, HIGH);
 }

Tuesday, September 11, 2018

day 5 9/11/18

Wave interaction problems
#1 
Implement the pseudocode for your sea level program into c.
read the period and wave height for wave 1
read the period and wave height for wave 2
compute and print the wavelength for each wave 
set new period to the product of the wave periods 
set time increment to new period/200 
set wavemax to 0 
set time to 0 
set steps to 0 
while steps <= 199 
     set sum to wave 1 + wave 2 
     if sum > wavemax 
         set wavemax to sum 
     add 1 to steps 
print wavemax 
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time = 0, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet", wavemax);
return 0;
}
#2
Modify the program so that it finds the maximum crest and the minimum trough for the
combined waves
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, wavemin = 0, time = 0, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
while(steps <= 199){
wave1 = -height1 * sin((1/period1)*PI*(timei * steps));
wave2 = -height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum < wavemin){
wavemin = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet\n", wavemax);
printf("minimum combined wave height is %5.2f feet", wavemin);
return 0;
}
#3
Modify the program so that it also allows the user to enter a time, then the program computes and prints the combined wave height at that time.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, time, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
printf("Enter a time (in seconds) to calculate a combined wave height\n");
scanf("%lf",&time);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
wave1 = height1 * sin((1/period1)*PI*(time));
wave2 = height2 * sin((1/period2)*PI*(time));
sum = (wave1 + wave2);
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("the combined wave height is %5.2f feet at time: %5.2f seconds", sum, time);
return 0;
}
#4
Modify the program so that it allows the user to specify the number of points to compute for determining the maximum combined wave height over the specified period of time. Should this make any difference in the answer? Explain.
ANSWER:
If you increase the number of points beyond 200 there is no difference however, if you decrease
the number of points below 200 the number decreases.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
printf("specify the number of points to calculate IE'200'\n");
scanf("%lf",&time);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/time;
while(steps <= time-1){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet", wavemax);
return 0;
}
#5
Modify the program so that it allows the user to specify a phase shift between the two waves. This phase shift should be used as the phase shift for the second wave; the phase shift for the first wave should still be zero. Experiment with different phase shifts to see if they change the maximum possible wave height.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time = 0, timei, phaseshift;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
printf("Enter the phaseshift between the two waves\n");
scanf("%lf",&phaseshift);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin(((1/period2)*PI*(timei * steps))+phaseshift);
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet", wavemax);
return 0;
}
#6
Modify the program so that it uses units of meters and seconds, instead of feet and seconds. Remember to modify the program that computes the wavelengths appropriately.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time = 0, timei;
printf("Enter the period (in seconds) and wave height (in meters) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in meters) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
height1 = height1 * 3.28;
height2 = height2 * 3.28;
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
length1 = length1/3.28;
length2 = length2/3.28;
wavemax = wavemax/3.28;
printf("the wavelength for the first wave is %5.2f meters\n", length1);
printf("the wavelength for the second wave is %5.2f meters\n", length2);
printf("maximum combined wave height is %5.2f meters", wavemax);
return 0;
}

Arduino temperature sensor and RGB bulb
#1
the base program here just illustrates how the temperature sensor works
const int POT=A0; int val =0; void setup() { Serial.begin(9600); } void loop() { val = analogRead(POT); Serial.println(val); delay(500); }
#2
Write the temperature alert sketch that the causes the LED to light blue when the temperature is cold (ice will be provided), red when the temperature is hot (from sqeezing with your fingers) and green when the temperature is just right.
const int POT=A0;
const int BLED=11;
const int GLED=10;
const int RLED=9;
double celsius=22;
void setup() {
  pinMode(BLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  Serial.begin(9600);}
void loop() {
  int Val = analogRead(POT);
  double Vout = (Val/1023.)*5000.;
  double Celsius = (Vout-500)/10.;
  if(Celsius >=celsius+1.5){
    digitalWrite(RLED, HIGH);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, LOW);
  }
  else if(Celsius <=celsius-2.5){
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, HIGH);
    digitalWrite(GLED, LOW);
  }
  else{
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, HIGH);
  }
  Serial.println(Celsius);
  delay(500);
  }
 

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);
}