Lab 4/ HW 4

Lab 4 (RGB Nightlight):

const int RLED = 9; //Blue LED on Pin 9
const int GLED = 10; //Green LED on Pin 10
const int BLED = 11; //Red LED on Pin 11
const int BUTTON1 = 2; //The Button is connected to pin 2
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;
const int BUTTON5 = 6;
boolean R_ledOn = false;
boolean B_ledOn = false;
boolean G_ledOn = false;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
 pinMode (RLED, OUTPUT); //Set Red LED as Output
 pinMode (GLED, OUTPUT); //Set Green LED as Output
 pinMode (BLED, OUTPUT); //Set Blue LED as Output
 pinMode (BUTTON1, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON1)||digitalRead(BUTTON2)||digitalRead(BUTTON3)
 ||digitalRead(BUTTON4)||digitalRead(BUTTON5);           //Read the button state
 if (last != current)                           //if it's different...
 {
 delay(5);                                        //wait 5ms
 current = digitalRead(BUTTON1)||digitalRead(BUTTON2)||digitalRead(BUTTON3)
 ||digitalRead(BUTTON4)||digitalRead(BUTTON5);                  //read it again
 }
 return current;                                  //return the current value
}
void loop()
{
 currentButton = debounce(lastButton);           //read debounced state
 if (lastButton == LOW && currentButton == HIGH && digitalRead(BUTTON1)) //if it was pressed...
 {
  ledMode++;
  switch(ledMode)
  {
  case 1: //PURPLE
  analogWrite(RLED, 127);
  analogWrite(GLED, 0);
  analogWrite(BLED, 127);
  break;
  
  case 2: //TEAL
  analogWrite(RLED, 0);
  analogWrite(GLED, 128);
  analogWrite(BLED, 128);
  break;
  
  case 3: //ORANGE
  analogWrite(RLED, 255);
  analogWrite(GLED, 25);
  analogWrite(BLED, 0);
  break;
  
  case 4: //WHITE
  analogWrite(RLED, 255);
  analogWrite(GLED, 255);
  analogWrite(BLED, 255);
  break;
  
  case 5: //OFF
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, LOW);
  ledMode = 0;
  break;
  }
 }
 if (lastButton == LOW && currentButton == HIGH && digitalRead(BUTTON2)) 
  {     
    R_ledOn = !R_ledOn;                    //Toggles RLED Value           
  }
 if (lastButton == LOW && currentButton == HIGH && digitalRead(BUTTON3)) 
  {     
    G_ledOn = !G_ledOn;                     //Toggles GLED Value            
  }
 if (lastButton == LOW && currentButton == HIGH && digitalRead(BUTTON4)) 
  {     
    B_ledOn = !B_ledOn;                    //Toggles BLED Value            
  }
  if (lastButton == LOW && currentButton == HIGH && digitalRead(BUTTON5))
  {
    digitalWrite(RLED,HIGH);
    delay(50);
    digitalWrite(RLED,LOW);
    delay(50);
    
  }
  
  lastButton = currentButton;
  if (digitalRead(BUTTON2))
  digitalWrite(RLED, R_ledOn);
  if (digitalRead(BUTTON3))
  digitalWrite(GLED, G_ledOn);
  if (digitalRead(BUTTON4))
  digitalWrite(BLED, B_ledOn);
  

}


Velocity & Acceleration:
/* This program estimates new velocity and */
/* acceleration values for a specified time. */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double time_i, velocity, acceleration;
/* Get time value from the keyboard. */
printf("Enter new time value in minutes: \n");
scanf("%lf",&time_i);
/* Compute velocity and acceleration. */
velocity = 0.00001*pow((time_i*60),3) - 0.00488*pow((time_i*60),2) + 0.75795*(time_i*60) + 181.3566;
acceleration = 3 - 0.000062*velocity*velocity;
/* Print velocity and acceleration. */
printf("Velocity = %8.3f ft/s \n",velocity*.054680664);
printf("Velocity = %9.3f ft/min \n",velocity*3.280839895);
printf("Acceleration = %11.9f ft/sˆ2 \n",acceleration*.000911344);
printf("Acceleration = %7.5f ft/minˆ2 \n",acceleration*3.280839895);
/* Exit program. */
return 0;
}



Log w/ base 2

/* Computes Logarithms to Base b */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double value, ans;
/* Number input for base of logarithms. */
printf("Enter a positive number : \n");
scanf("%lf",&value);
/* Compute log with base 2 */
ans = log2(fabs(value));
/* Print Answer */
printf("Answer: %5.2f \n",ans);
/* Exit program. */
return 0;
}



Log w/ base 8

/* Computes Logarithms to Base b */
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double base, value, ans;
/* Number input for base of logarithms. */
printf("Enter a positive number : \n");
scanf("%lf",&value);
printf("Enter a positive base: \n");
scanf("%lf",&base);

/* Compute log with base 2 */
ans = log(fabs(value))/log(fabs(base));

/* Print Answer */
printf("Answer: %5.2f \n",ans);
/* Exit program. */
return 0;
}

Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21