Sunday, November 5, 2017

Lab 4 - RGB LED

CLASSWORK:
RGB LED Control with Debounced Button
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
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.
*/
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
 if (mode == 1)
 {
 digitalWrite(RLED, HIGH);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, LOW);
 }
 //GREEN
 else if (mode == 2)
 {
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, HIGH);
 digitalWrite(BLED, LOW);
 }
 //BLUE
 else if (mode == 3)
 {
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, HIGH);
 }
 //PURPLE (RED+BLUE)
 if (mode == 4)
 {
 digitalWrite(RLED, HIGH);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, HIGH);
 }
 //WHITE (RED+GREEN+BLUE)
 if (mode == 5)
 {
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, HIGH);
  digitalWrite(BLED, HIGH);
 }
 //ORANGE
 if (mode == 6)
 {
  analogWrite(RLED, 127);
  analogWrite(GLED, 127);
  analogWrite(BLED, 0);
 }
 //TEAL
 else if (mode == 7)
 {
  analogWrite(RLED, 0);
  analogWrite(GLED, 127);
  analogWrite(BLED, 127);
 }
 //OFF (mode = 0)
 else
 {
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, LOW);
 }
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 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

}

HOMEWORK:
RGB LED Control with Switch Statements
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=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;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int mode = 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.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON1); //Read the button state
 if (last != current) //if it's different...
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON1); //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)
{
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, HIGH);
  digitalWrite(GLED, HIGH);
  digitalWrite(BLED, HIGH);
  break;
case 6: 
  analogWrite(RLED, 127);
  analogWrite(GLED, 127);
  analogWrite(BLED, 0);
  break;
case 7: 
  analogWrite(RLED, 0);
  analogWrite(GLED, 127);
  analogWrite(BLED, 127);
  break;
default:
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, LOW);
break;
}
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 mode++; //increment the LED value
 }
 lastButton = currentButton; //reset button value
 //if you've cycled through the different options,
 //reset the counter to 0
 if (mode == 8) mode = 0;
 setMode(mode); //change the LED state
}


Lab 3 - Digital Inputs and Indexed Loops

CLASSWORK:
Blinking LED with For Loop (aka PWM)
const int LED=9; //define LED for Pin 9
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
}
void loop()
{
 for (int i=100; i<=1000; i=i+100)
 {
 digitalWrite(LED, HIGH);
 delay(i);
 digitalWrite(LED, LOW);
 delay(i);
 }

}

LED with Changing Brightness
const int LED=9; //define LED for Pin 9
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
}
void loop()
{
 for (int i=0; i<256; i++)
 {
 analogWrite(LED, i);
 delay(10);
}
 for (int i=255; i>=0; i--)
 {
 analogWrite(LED, i);
 delay(10);
 }

}

Basic Button Without Debounce
const int LED=9; //The LED is connected to pin 9
const int BUTTON=2; //The Button is connected to pin 2
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
 pinMode (BUTTON, INPUT); //Set button as input (not required)
}
void loop()
{
 if (digitalRead(BUTTON) == LOW)
 {
 digitalWrite(LED, LOW);
 }
 else
 {
 digitalWrite(LED, HIGH);
 }

}

HOMEWORK:
Basic Button With Debounce
const int LED=9; //The LED is connected to pin 9
const int BUTTON=2; //The Button is connected to pin 2
boolean lastButton = LOW; //Variable containing the previous button state
boolean currentButton = LOW; //Variable containing the current button state
boolean ledOn = false; //The present state of the LED (on/off)
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an 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.
*/
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
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 ledOn = !ledOn; //toggle the LED value
 }
 lastButton = currentButton; //reset button value

 digitalWrite(LED, ledOn); //change the LED state

}



Day 3 - Numerical Technique: Linear Interpolation, Math Functions. Brinicles and Open Jet Engines

CLASSWORK/ HOMEWORK:
Freezing Temperature of Seawater
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/*  This program uses linear interpolation to                  */
/*  compute the freezing temperature of seawater.              */
#include <stdio.h> 
#include <math.h>
int main(void)
{
/*  Declare variables.  */
double salinity_a, freezpt_a, new_salinity_b, freezpt_b, salinity_c, freezpt_c;
/*  Get user input from the keyboard.  */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&salinity_a,&freezpt_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&salinity_c,&freezpt_c);
printf("Enter new salinity: \n");
scanf("%lf",&new_salinity_b);
//  Use linear interpolation to compute   new freezing temperature.            
freezpt_b = freezpt_a + (new_salinity_b - salinity_a)/(salinity_c - salinity_a)*(freezpt_c - freezpt_a);
/*  Print new freezing temperature.  */
printf("New freezing temperature in degrees F: %4.1f \n",freezpt_b);
return 0;  /*  Exit program.  */

}

Open Jet Engine Velocity and Acceleration
#include <math.h>
#include <stdio.h>

int main(void)
{
  double Velocity,acceleration, t_s;
  printf("Input time elapsed in seconds:\n");
  scanf("%lf",&t_s);
  Velocity = .00001*(t_s*t_s*t_s)-.00488*(t_s*t_s)+.75795*(t_s)+181.3566;
  acceleration = 3-.000062*(Velocity*Velocity);
  printf("Velocity:%lf m/s\n", Velocity);
  printf("Acceleration: %lf m/s/s\n",acceleration);
 return 0;

}

Logarithm of Base 'y'
#include <stdio.h>
#include <math.h>

int main (void)
{
 double x, y, log_y;
 printf("Logarithm of x to Base y: \n");
 printf("\nInput positive numbers for x & y: \n");
 scanf("%lf" "%lf",&x, &y);
 log_y = log(x)/log(y);
 if (x <= 0 || y <= 0)
 {
  printf("Sorry, please input a positive number. \n");
 } 
 else
 {
  printf("The logarithm of x to base y is %5.1f \n", log_y);
 }
 return 0;

}

Day 4 - Top Down Programming, Logical and Conditional statements

CLASSWORK:
If the denominator is close to zero
if (fabs(denominator) < 0.0001) 
printf("Denominator close to zero"); 
else { 
x = numerator/denominator; 
printf("x = %f \n",x); 
}

Example of an if/else statement
if (code == 10) 
  printf("Too hot - turn equipment off \n"); 
    else 
    { 
      if (code == 11) 
        printf("Caution - recheck in 5 minutes \n"); 
          else { 
            if (code == 13) 
              printf("Turn on circulating fan \n"); 
                else 
                  printf("Normal mode of operation \n"); 
            } 


An equivalent statement is the following switch statement:

switch (code) 

case 10: 
printf("Too hot - turn equipment off \n"); 
break; 
case 11: 
printf("Caution - recheck in 5 minutes \n"); 
break; 
case 13: printf("Turn on circulating fan \n"); 
break; 
default: 
printf("Normal temperature range \n"); 
break; 


HOMEWORK:
Give the value of a after the following set of statements is executed.
int a = 750; 
... 
if (a>0) //if a is greater than zero
if (a >= 1000) //check if a is greater than or equal to 1000
a = 0; //if the previous statements are true, set a equal to zero
else if (a < 500) //if not, check if a is less than 500
a *= 2; //if the previous statement is true, let a equal 2a
else 
a *= 10; //if a is greater than or equal to 500, let a equal 10a 
else 
a += 3; //if a is less than or equal to zero, let a equal 3+a

Since a = 750, a = a*10 => a = 7500



Day 5 - Loops

CLASSWORK:
Degrees to Radians Using While Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a while loop structure. */ 
#include <stdio.h>
#define PI 3.141593 

int main(void) {
 /* Declare and initialize variables. */ 
int degrees=0; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
while (degrees <= 360) { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
degrees += 10; 

/* Exit program. */ 
return 0; 
}

Degrees to Radians Using Do/While Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a do-while loop structure. */ 
#include <stdio.h> 
#define PI 3.141593

int main(void) { 
/* Declare and initialize variables. */ 
int degrees=0; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
do { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
degrees += 10; 
} while (degrees <= 360); 
/* Exit program. */ 
return 0; 
} 

Degrees to Radians Using For Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a for loop structure. */ 
#include <stdio.h> 
#define PI 3.141593 

int main(void) { 
/* Declare variables. */ 
int degrees; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
for (degrees=0; degrees<=360; degrees+=10) { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 

/* Exit program. */ 
return 0; 


HOMEWORK:
Wave Generator Code
#include <stdio.h>
#include <math.h>
#define PI 3.141592

int main (void)
{
  double wl_1, wl_2, wl_3, wh_1, wh_2, per_1, per_2, per_c, time;
  double sum, wavemax,steps, w_1, w_2, w_3;
  printf("Input period and wave height:\n" );
  printf("Wave 1: \n");
  scanf("%f" "%f", &per_1, &wh_1);
  printf("Wave 2: \n");
  scanf("%f" "%f", &per_2, &wh_2);
  
  wl_1 = 5.13*per_1*per_1;
  wl_2 = 5.13*per_2*per_2;
  
  per_c = per_1*per_2;
  wl_3 = 5.13*per_c*per_c;
  
  w_1 = .5*wh_1*sin((2*PI/per_1)*time);
  w_2 = .5*wh_2*sin((2*PI/per_2)*time);
  w_3 = .5*(wh_1 + wh_2)*sin((2*PI/per_c)*time);
  sum = w_1 + w_2;
  
  steps = per_c/200;

/*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*/
printf("Individual Wavelengths:");
for (time=0, sum=0; time<=2; time += steps)
{
  printf("WL 1: %5.4f""WL 2: %5.4f""WL 3: %5.4f", wl_1, wl_2, wl_3);
  if (sum > wavemax)
  {
    wavemax = sum;
  }
continue;
}
printf("Max Wave Height: %f", wavemax);

  return 0;
}

Lab 6 - Voltage Dividers, CdS, and PIR Sensors

CLASSWORK:
Simple PIR Sensor
const int LED=9;//The LED is connected to pin 9
const int PIR=2;//The PIR is connected to pin 2
void setup()
{
 pinMode (LED, OUTPUT);
 pinMode (PIR, INPUT);
 Serial.begin(9600);
}
void loop()
{
 if (digitalRead(PIR) == LOW)
 {
 Serial.println("OFF");
 delay(500);
 digitalWrite(LED, LOW);
 }
 if (digitalRead(PIR) == HIGH)
 {
 Serial.println("ON");
 delay(500);
 }

}

Automatic Night Light with RGB LED and PIR Sensor
//Automatic Nightlight 

const int RLED=9; //Red LED on pin 9 (PWM) 
const int LIGHT=0; //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 


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 
}

HOMEWORK:
Photoresistor Controls LED & Speaker
int SPEAKER_1=9;
int SPEAKER_2=8;
int SPEAKER_3=7;
int SPEAKER_4=6;
const int RLED=10;//Red LED on pin 10 (PWM)
const int GLED=11;
const int LASER_1=12;
const int LASER_2=13;
const int LIGHT=A0;//Light Sensor on analog pin 0
const int LIGHT_2=A1;
const int MIN_LIGHT=150; //Minimum expected light value
const int MAX_LIGHT=600;//Maximum Expected Light value
const int MIN_LIGHT_2=150;
const int MAX_LIGHT_2=600;
const int MIN_LIGHT_3=150;
const int MAX_LIGHT_3=600;
const int MIN_LIGHT_4=150;
const int MAX_LIGHT_4=600;
int val = 0; //variable to hold the analog reading
int val_2 = 0;
int val_3 = 0;
int val_4 = 0;
void setup()
{
 pinMode(RLED, OUTPUT); //Set LED pin as output
 pinMode(GLED, OUTPUT);
 pinMode(LIGHT, INPUT);//Set Photoresistor as input
 pinMode(LIGHT_2,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
 analogWrite(RLED, val); //Control the LED
 Serial.println(val);
 delay(500);
    if(val > 150)
    {
    tone(SPEAKER_1, 1500,500);
    //digitalWrite(RGB_LED. HIGH)
    delay(500);

    }
//////////////////////////////////////////////////////////////////////////////////////////
{
 val_2 = analogRead(LIGHT_2); //Read the light sensor
 val_2 = map(val_2, MIN_LIGHT_2, MAX_LIGHT_2, 255,0 );//Map the light reading
 val_2 = constrain(val_2, 0, 255); //Constrain light value
 analogWrite(GLED, val_2); //Control the LED
 //digitalWrite(GLED, HIGH);
 Serial.println(val_2);
 delay(500);
    if(val_2 > 140)
     {
      tone(SPEAKER_2, 1500,500);
    delay(500);
     }
  }
//////////////////////////////////////////////////////////////////////////////////////////
 {
 val_3 = analogRead(LASER_1); //Read the light sensor
 val_3 = map(val_3, MIN_LIGHT_3, MAX_LIGHT_3, 255,0 );//Map the light reading
 val_3 = constrain(val_3, 0, 255); //Constrain light value
 analogWrite(LASER_1, val_3); //Control the LED
 //digitalWrite(GLED, HIGH);
 Serial.println(val_3);
 delay(500);
    if(val_3 > 140)
     {
      tone(SPEAKER_3, 1500,500);
    delay(500);
     }
  }
//////////////////////////////////////////////////////////////////////////////////////////
   {
 val_4 = analogRead(LASER_2); //Read the light sensor
 val_4 = map(val_3, MIN_LIGHT_4, MAX_LIGHT_4, 255,0 );//Map the light reading
 val_4 = constrain(val_4, 0, 255); //Constrain light value
 analogWrite(LASER_2, val_4); //Control the LED
 //digitalWrite(GLED, HIGH);
 Serial.println(val_4);
 delay(500);
    if(val_4 > 140)
     {
      tone(SPEAKER_4, 1500,500);
    delay(500);
     }
  }

}

Saturday, November 4, 2017

Day 6 - Functions and Modularity

CLASSWORK:
Beehive function
int beehive(double a,double b,double c, double d)
{
int count = 0;
if (a > b)
count ++;
if (b<c)
count ++;
if (d>a)
count ++;
if (a<c)
count ++;
else
count ++;
Return count;
}

HOMEWORK:
Great Circle Distance b/w Two Points in the Northern Hemisphere
/*––––––––––––––––––––â€*/
/*  This program determines the distance between two points    */
/*  that are specified with latitude and longitude values      */
/*  that are in the Northern Hemisphere.                       */
#include <stdio.h>
#include <math.h>
#define PI 3.141593
int main(void)
{
/*  Declare variables and function prototype.  */
double lat1, long1, lat2, long2;
double gc_distance(double lat1,double long1, 
double lat2,double long2);
/*  Get locations of two points.  */
printf("Enter latitude north and longitude west ");
printf("for location 1: \n");
scanf("%lf %lf",&lat1,&long1);
printf("Enter latitude north and longitude west ");
printf("for location 2: \n");
scanf("%lf %lf",&lat2,&long2);
/*  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, dot, dist1, dist2,x1, y1, z1, x2, y2, z2;
/*  Convert latitude,longitude to rectangular coordinates.  */
rho = 3960;
phi = (90 - lat1)*(PI/180.0);
theta = (360 - long1)*(PI/180.0);
x1 = rho*sin(phi)*cos(theta);
y1 = rho*sin(phi)*sin(theta);
z1 = rho*cos(phi);
phi = (90 - lat2)*(PI/180.0);
theta = (360 - long2)*(PI/180.0);
x2 = rho*sin(phi)*cos(theta);
y2 = rho*sin(phi)*sin(theta);
z2 = rho*cos(phi);
/*  Compute angle between vectors.  */
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));
/*  Compute and return great circle distance.  */
return gamma*rho;
}


Lab 9 - Sweeping Distance Sensor

CLASSWORK/ HOMEWORK:
Sweeping Servo 2.0
//Sweeping Distance Sensor
#include <Servo.h>
  const int SERVO =9;
  const int IR    =0;
  const int LED1  =3;
  const int LED2  =5;
  const int LED3  =6;
  const int LED4  =11;
  int LEDonTime   =50;
  int ServoSpeed  =100;
Servo myServo;
int dist1 = 0;
int dist2 = 0;
int dist3 = 0;
int dist4 = 0;
void setup() 
{
    myServo.attach(SERVO);
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    pinMode(LED4, OUTPUT);
}
void loop() 
{
  //Sweep the Servo into 4 regions and change the LEDs
  dist1 = readDistance(78);
  analogWrite(LED1, dist1);
  delay(LEDonTime);

  dist2 = readDistance(82);
  analogWrite(LED2, dist2);
  delay(LEDonTime);

  dist3 = readDistance(86);
  analogWrite(LED3, dist3);
  delay(LEDonTime);

  dist4 = readDistance(90);
  analogWrite(LED4, dist4);
  delay(LEDonTime);
}
int readDistance(int pos)
{
  myServo.write(pos);
  delay(ServoSpeed);
  int dist = analogRead(IR);
  dist = map(dist, 50, 500, 0, 255);
  dist = constrain(dist, 0, 255);
  return dist;

}


Lab 8 - Part 2: Sound and Arrays

CLASSWORK:
Song on a Speaker with pitches.h and Arrays
//Plays a song on a speaker
#include "pitches.h" //Header file with pitch definitions
const int SPEAKER=10; //Speaker Pin
//Note Array
int notes[] = { NOTE_A4, NOTE_E3, NOTE_A4, 0, NOTE_A4, NOTE_E3,
NOTE_A4, 0,NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_A4, NOTE_B4,
NOTE_C4, NOTE_D4,NOTE_E4, NOTE_E3, NOTE_A4, 0 };
//The Duration of each note (in ms)
int times[] = { 250, 250, 250, 250, 250, 250, 250, 250,
125, 125, 125, 125, 125, 125, 125, 125,
250, 250, 250, 250 };
void setup()
{
//Play each note for the right duration
for (int i = 0; i < 20; i++)
{
 tone(SPEAKER, notes[i], times[i]);
 delay(times[i]);
}
}
void loop()
{
//Press the Reset button to play again.

}

Pitches.h Header file
/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699

#define NOTE_DS8 4978



Lab 8 - Part 1: Servos and Voltage Regulators

CLASSWORK:
Single Servo POT Control
//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int POT=2; //POT on Analog Pin 0
Servo myServo; 
void setup()
{
 myServo.attach(SERVO);
 Serial.begin(9600);
}
void loop()
{
 int val = digitalRead(POT); //Read Pot
 val = map(val, 0, 1, 0, 179); //scale it to servo range
 Serial.println(val);
 delay(500);
 if (val==179)
 {
  myServo.write(val); //sets the servo
 delay(15); //waits for the servo
 }

}

HOMEWORK:
Single Servo Control with PIR Sensor
//Servo PIR Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2; //POT on Digital Pin 2
Servo myServo; 
void setup()
{
 myServo.attach(SERVO);
 Serial.begin(9600);
}
void loop()
{
 int val = digitalRead(PIR); //Read Pot
 val = map(val, 0, 1, 0, 179); //scale it to servo range
 Serial.println(val);
 delay(500);
 if (val>=178)
 {
  myServo.write(133); //sets the servo
  delay(15); //waits for the servo
 }
 else
 {
  myServo.write(37);
  delay(15);
 }
}