Saturday, November 4, 2017

Day 7 - Random Numbers and Recursion

CLASSWORK:
PI Calculation code
/* Program to compute Pi using Monte Carlo methods */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define SEED 35791246
void main(void)
{
 int niter=0;
 double x,y,z,pi;
 int count=0; /* # of points in the 1st quadrant of unit circle */
 printf("Enter the number of iterations used to estimate pi: ");
 scanf("%d",&niter);
 /* initialize random numbers */
 srand(SEED);
 count=0;
 for (int i=0; i<niter; i++) {
 x = (double)rand()/RAND_MAX;
 y = (double)rand()/RAND_MAX;
 z = x*x+y*y;
 if (z<=1) count++;
 }
 pi=(double)count/niter*4;
 printf("# of trials= %d , estimate of pi is %g \n",niter,pi);
return 0;

}

Random Number Generator
/* */ 
/* This program generates and prints ten */ 
/* random integers between 1 and RAND_MAX. */ 
#include <stdio.h>
#include <stdlib.h>
int main(void) { 
/* Declare variables. */ 
unsigned int seed; 
int k; 
/* Get seed value from the user. */ 
printf("Enter a positive integer seed value: \n"); 
scanf("%u",&seed); 
srand(seed); /
* Generate and print ten random numbers. */ 
printf("Random Numbers: \n"); 
for (k=1; k<=10; k++) printf("%i ",rand()); 
printf("\n"); 
/* Exit program. */ 
return 0; 
} 
HOMEWORK:
Randon Dice Roll Generator
/* */
/* This program generates and prints ten random */
/* integers between user-specified limits. */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/* Declare variables and function prototype. */
unsigned int seed;
int a, b, k, n;
int rand_int(int a,int b);
int count = 0; 
/* 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) for dice roll: \n");
scanf("%i %i",&a,&b);
printf("Enter number of rolls: \n");
scanf("%i",&n);
/* Generate and print N random numbers. */
printf("Dice Rolls: \n");
for (k=1; k<=2*n; k++)
  printf("%i ",rand_int(a,b));
  printf("\t");
if (rand_int(a,b) + rand_int(a,b) == 8)
    count++;
/* 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;

}

Friday, November 3, 2017

Project 1: DJ Sound Box

PROJECT DESCRIPTION:
Our task was to create a musical art piece that integrates three different sensing modes. It must also have at least two LEDs---one to indicate whether the device is on or off and another one that changes in response to user interaction. The device should also generate sound in response to either the environment or user interaction. There should also be a button that turns the music on/off.

INDIVIDUAL PROJECT CONTRIBUTION:
Richard and I collaborated on a majority of the coding and debugging process. For example, on the one hand, I coded the function that allowed the Arduino to convert the analog signal from the photoresistor to a brightness value for the LEDs. On the other hand, Richard finalized logic statements  within my function that would play 8-bit tones while the LED was illuminated. I also used the serial monitor to calibrate the extrema of our photoresistor values to the brightness of the LEDs. Richard made it clear to me that he had trouble calibrating the light values, so I taught him the process, helped him analyze the values; and by the end of the project, he had calibrated most of the light values. We also troubleshooted a majority of the hardware together. For instance, I established a methodology for wiring the lasers and the LEDs, while Richard soldered the necessary components together. While Richard acquired most of the electrical components for this project---e.g alligator clips, lasers, and speakers---we shared the burden of ensuring that these pieces made it into the final piece. For instance, approximately 48 hours before our project was due, we discovered that Richard had left his soldering iron in the ENGR 6 classroom. At this point, we still had a majority of our components to solder together, including the lasers and speakers. However, I was able to obtain a different soldering iron the next day, allowing us to complete the necessary connections and by extension our project as a whole. Moreover, approximately 5 hours before the deadline, I discovered and undid a major hardware problem---involving one of our speakers and buttons---that resolved a major roadblock for the upload of our code to the Arduino, thereby allowing my teammate Richard to amend that portion of the hardware and assemble the breadboard-Arduino wire nest within of our cardboard package. This DJ music box is therefore demonstrative of a collaborative effort that was only made possible by our joint skill set and problem-solving knowledge.

PROJECT CODE:
// Project 4 Mason
// This program facilitates the use of our DJ sound box. It uses photo resistors as inputs, lasers, LED's, and speakers as outputs.
int BUTTON_1 = 4;// First button on pin 4 is a mute button for the music
int BUTTON_2 = 3;// Second button on pin 3 controls Imperial March music
int buttonState_1 = LOW;// This variable stores the on/off state of the mute button
int buttonState_2 = LOW;// This variable stores the on/off state of the music button
int SPEAKER_1 = 9;// First Speaker on pin 9
int SPEAKER_2 = 8;
int SPEAKER_3 = 7;
int SPEAKER_4 = 6;
int SPEAKER_5 = 5;// This speaker plays Imperial March
const int RLED = 10;// Two Red LEDs connected in parallel on pin 10 (PWM)
const int GLED = 11;
const int BLED = 0;
const int GLED_RGB = 1;// Green LED on pin 1
const int RLED_RGB = 2;// Red LED on pin 2
const int LASER_1 = 12;// Two cheap lasers connected in parallel on pin 12
const int LASER_2 = 13;
const int LIGHT = A0;// Light Sensor (aka photoresistor) on analog pin 0
const int LIGHT_2 = A1;
const int LIGHT_3 = A4;
const int LIGHT_4 = A2;
const int MIN_LIGHT = 10; //MINIMUM expected light value for first photoresistor
const int MAX_LIGHT = 240;//MAXIMUM Expected Light value for first photoresistor
const int MINLIGHT2 = 20;
const int MAXLIGHT2 = 650;
const int MINLIGHT3 = 10;
const int MAXLIGHT3 = 240;
const int MINLIGHT4 = 10;
const int MAXLIGHT4 = 240;
int val = 0; //variable to hold the analog reading for the light value of photoresistor
int val_2 = 0;
int val_3 = 0;
int val_4 = 0;
void setup()
{
Serial.begin(9600); // Set the bit rate for serial communication to 9600
pinMode(RLED, OUTPUT); //Set LED pin as output
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);
pinMode(GLED_RGB, OUTPUT);
pinMode(RLED_RGB, OUTPUT);
pinMode(LASER_1,OUTPUT); //Set laser pin as output
pinMode(LASER_2,OUTPUT);
pinMode(LIGHT, INPUT); //Set Photoresistor as input
pinMode(LIGHT_2,INPUT);
pinMode(LIGHT_3,INPUT);
pinMode(LIGHT_4,INPUT);
pinMode(BUTTON_1, INPUT); //Set first button as input
pinMode(BUTTON_2, INPUT);
pinMode(SPEAKER_1, OUTPUT); //Set speaker as output
pinMode(SPEAKER_2, OUTPUT);
pinMode(SPEAKER_3, OUTPUT);
pinMode(SPEAKER_4, OUTPUT);
pinMode(SPEAKER_5, OUTPUT);
}
void loop()
{
buttonState1 = digitalRead(BUTTON1);
buttonState2 = digitalRead(BUTTON2);
// The following three variables control: (a) the previous state of the music button,
// (b) the current state of the music button, and (c) the on/off state of its
// connected speaker
/*boolean lastButton = LOW;
boolean currentButton = LOW;
boolean SPEAKER5On = false;
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
SPEAKER_5_On = !SPEAKER_5_On;
}
lastButton = currentButton;
if (SPEAKER5On)
{ }
else
{
boolean SPEAKER_5_On = false;
}*/
if (buttonState_1 == HIGH) //if mute button has been pressed...
{
noTone(SPEAKER_5); //turn off the music speaker
}
if (buttonState_2 == HIGH)
{
// play the Music 
tone(9,146.83,400);//D 
delay(400); 
tone(9,146.83,400);//D
delay(400); 
tone(9,146.83,250);//D 
delay(250); 
tone(9,174.61,250);//F
delay(250); 
tone(9,196.00,350);//G 
delay(400); 
tone(9,146.83,350);//D
delay(400); 
tone(9,146.83,200);//D 
delay(250); 
tone(9,146.83,200);//D
delay(250); 
tone(9,130.81,400);//C 
delay(450); 
tone(9,130.81,400);//C
delay(450); 
tone(9,146.83,400);//D 
delay(450); 
tone(9,146.83,400);//D
delay(450); 
tone(9,146.83,400);//D 
delay(450); 
tone(9,174.61,400);//F
delay(300); 
tone(9,196.00,400);//G 
delay(450); 
tone(9,146.83,400);//D
delay(450); 
tone(9,146.83,400);//D 
delay(450); 
tone(9,146.83,400);//D
delay(450); 
tone(9,130.81,400);//C 
delay(300); 
tone(9,130.81,400);//C
delay(300); 
tone(9,130.81,400);//C 
delay(450); 
tone(9,174.61,400);//F
delay(450); 
tone(9,146.83,400);//D 
delay(450); 
tone(9,220.00,400);//A
delay(450); 
tone(9,174.61,400);//D 
delay(450); 
tone(9,146.83,400);//D
delay(450); 
tone(9,207.65,400);//GSHARP 
delay(450);
}
{
val = analogRead(LIGHT); //Read the light sensor
val = map(val, MINLIGHT, MAXLIGHT, 255,0 );//Map the light reading
val = constrain(val, 0, 255); //Constrain light value
analogWrite(RLED, val); //Control the LED
Serial.println(val);
delay(200);
if(val > 150) { tone(SPEAKER_1, val+200,200); //digitalWrite(RGB_LED.HIGH) delay(10); }
}
//////////////////////////////////////////////////////////////////////////////////////////
{
val2 = analogRead(LIGHT2); //Read the light sensor
val2 = map(val2, MINLIGHT2, MAXLIGHT2, 255,0 );//Map the light reading
val2 = constrain(val2, 0, 255); //Constrain light value
analogWrite(GLED, val_2); //Control the LED
Serial.println(val_2);
delay(200);
if(val_2 > 150) { tone(SPEAKER_2, 2000,100); delay(100); tone(SPEAKER_2, 1500,100); delay(10); tone(SPEAKER_2, 1000,100); delay(150);tone(SPEAKER_2, 2000,100); delay(10); tone(SPEAKER_2, 1500,200);delay(100); tone(SPEAKER_2, 2000,200); delay(10); }
}
//////////////////////////////////////////////////////////////////////////////////////////
{
val3 = analogRead(LIGHT3); //Read the light sensor
val3 = map(val3, MINLIGHT3, MAXLIGHT3, 255,0 );//Map the light reading
val3 = constrain(val3, 0, 255); //Constrain light value
analogWrite(LASER1, val3); //Control the LED
//digitalWrite(GLED, HIGH);
Serial.println(val_3);
delay(200);
if(val_3 > 140) { tone(SPEAKER_3, val_3-600,200); delay(10); }
}
//////////////////////////////////////////////////////////////////////////////////////////
{
val4 = analogRead(LIGHT4); //Read the light sensor
val4 = map(val3, MINLIGHT4, MAXLIGHT4, 255,0 );//Map the light reading
val4 = constrain(val4, 0, 255); //Constrain light value
analogWrite(LASER2, val4); //Control the LED
//digitalWrite(GLED, HIGH);
Serial.println(val_4);
delay(200);
if(val_4 > 140) { tone(SPEAKER_4, val_4+1000,200); delay(10); }
}
if(val<150)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED_RGB, LOW);
digitalWrite(BLED, HIGH);
}
}

Lab 11 - Serial IO

CLASSWORK:
POT reading converted to Percentage
//Simple Serial Printing Test with a Potentiometer 
const int POT=0; //Pot on analog pin 0 
void setup() { 
 Serial.begin(9600); //Start serial port with baud = 9600 
} void loop() { 
 int val = analogRead(POT); //Read potentiometer 
 int per = map(val, 0, 1023, 0, 100); //Convert to percentage
 Serial.print("Analog Reading: "); 
 Serial.print(val); //Print raw analog value 
 Serial.print(" Percentage: "); 
 Serial.print(per); //Print percentage analog value   Serial.println("%"); //Print % sign and newline 
 delay(1000); //Wait 1 second, then repeat 
} 

Serial Printing a Potentiometer Data Table
//Tabular serial printing test with a potentiometer 
const int POT=0; //Pot on analog pin 0 
void setup() 
{ 
 Serial.begin(9600); //Start Serial Port with Baud = 9600 
} 
void loop() { 
 Serial.println("\nAnalog Pin\tRaw Value\tPercentage");
 Serial.println("------------------------------------------"); 
 for (int i = 0; i < 10; i++) { 
 int val = analogRead(POT); //Read potentiometer 
 int per = map(val, 0, 1023, 0, 100); //Convert to percentage   Serial.print("A0\t\t"); 
 Serial.print(val); 
 Serial.print("\t\t"); 
 Serial.print(per); //Print percentage analog value   Serial.println("%"); //Print % sign and newline 
 delay(1000); //Wait 1 second, then repeat 
 } 
}

Echo Serial Inputs to the Serial Output Space
//Echo every character char data; //Holds incoming character 
void setup() { 
 Serial.begin(9600); //Serial Port at 9600 baud 
} 
void loop() { 
 //Only print when data is received 
 if (Serial.available() > 0)  { 
 data = Serial.read(); //Read byte of data 
 Serial.print(data); //Print byte of data 
 } 
} 

HOMEWORK:
Serial Input RGB values to light up RGB LED
//Sending Multiple Variables at Once
//Define LED pins
const int RED =11;
const int GREEN =10;
const int BLUE =9;
//Variables for RGB levels
int rval = 0;
int gval = 0;
int bval = 0;
void setup()
{
 Serial.begin(9600); //Serial Port at 9600 baud
 //Set pins as outputs
 pinMode(RED, OUTPUT);
 pinMode(GREEN, OUTPUT);
 pinMode(BLUE, OUTPUT);
}
void loop()
{
 //Keep working as long as data is in the buffer
 while (Serial.available() > 0)
{
 rval = Serial.parseInt(); //First valid integer
 gval = Serial.parseInt(); //Second valid integer
 bval = Serial.parseInt(); //Third valid integer
 if (Serial.read() == '\n') //Done transmitting
 {
 //set LED
 analogWrite(RED, rval);
 analogWrite(GREEN, gval);
 analogWrite(BLUE, bval);
 }
 }

}

Day 11 - File IO

CLASSWORK:
Wave Data Code
/* This program determines the maximum height of a wave that is the sum of two specified waves. */
#include <stdio.h>
#include <math.h>
#define PI 3.141593
#define FILENAME "waves1.txt"
int main(void)
{
/* Declare variables. */
int k;
double A1, A2, freq1, freq2, height1, height2, length1, length2;
double T1, T2, w1, w2, sum, new_period, new_height, time_incr, t;
double maxwave=0;
FILE *waves;
/* Open output file. */
waves = fopen(FILENAME,"w");
/* Get user input from the keyboard. */
printf("Enter integer wave period (s) and wave height (ft) \n");
printf("for wave 1: \n");
scanf("%lf %lf",&T1,&height1);
printf("Enter integer wave period (s) and wave height (ft) \n");
printf("for wave 2: \n");
scanf("%lf %lf",&T2,&height2);
/* Determine and print wavelengths. */
length1 = 5.13*T1*T1;
length2 = 5.13*T2*T2;
printf("Wavelengths (in ft) are: %.2f %.2f \n",length1,length2);
new_period = T1*T2;/* Determine period of combined waves. */
/* Compute 200 points of the combined waves over the */
/* period specified, and find the maximum height. */
time_incr = new_period/200;
A1 = height1/2;
A2 = height2/2;
freq1 = 1/T1;
freq2 = 1/T2;
for (k=0; k<=199; k++)
{
t = k*time_incr;
w1 = A1*sin(2*PI*freq1*t);
w2 = A2*sin(2*PI*freq2*t);
sum = w1 + w2;
fprintf(waves,"%.4f %.4f %.4f %.4f \n",t,w1,w2,sum);
if (sum >maxwave)
maxwave = sum;
}
new_height = maxwave*2;
/* Print new wave maximum. */
printf("Maximum combined wave height is %.2f ft \n",new_height);
fclose(waves);/* Close file and exit program. */
return 0;

}

HOMEWORK:
Balloon Data Code:
/* This program calculates the amplitude and velocity of a weather balloon and stores
the data values in a file called balloon1.txt */
#include <stdio.h>
#include <math.h>
#define FILENAME "balloon1.txt"

int main(void){
//Declare and initialize variables
double start_time_hrs, time_incr_min, end_time_hrs, upper_time_bound, t;
double altitude_m, velocity_m_hr, velocity_m_s;
double peak_altitude=0, max=0, t_peak=0;
double num_data_pts=0;

FILE *balloon;

//Open up balloon data file
balloon = fopen(FILENAME, "w");

printf("Welcome to my Program!\n");
printf("\n");

//Ask user to input information from keyboard
printf("Input an integer upper time bound(in hrs): \n");
scanf("%lf", &upper_time_bound);

//If upper time bound is a positive nonzero int...
if (upper_time_bound > 0) {
{
//Ask user to input start and end times...
printf("Input an integer start time(hrs) & an integer end time(hrs):\n");
scanf("%lf %lf", &start_time_hrs, &end_time_hrs);
}
}
//If the time bound is negative or zero...
else {
{
printf("Error: Only positive nonzero integers allowed. \n");
printf("Please reenter report information correctly. \n");
return 0;
}
}

//If the end time is greater than the start time...
//and both start time and end time are positive...
if ((end_time_hrs > start_time_hrs) && (start_time_hrs >= 0) && (end_time_hrs > 0)) {
{
//Ask user to input time increment
printf("Input an integer time increment(in min): \n");
scanf("%lf", &time_incr_min);

//If the time increment is positive...
if (time_incr_min > 0)
{
//Calculate number of data points
num_data_pts = ((end_time_hrs - start_time_hrs)/(time_incr_min/60)) + 1;

//Print the number of data points
printf("\nNumber of data points: %f \n", num_data_pts);
printf("You're good!\n");
//The program now runs the for loop below...
}
//If the time increment is negative or zero...
else
{
printf("Error: Only positive nonzero integers allowed. \n");
printf("Please reenter report information correctly. \n");
return 0;
}
}
}

//Else, check if start time or end time are negative values...
else if ((start_time_hrs < 0) || (end_time_hrs <= 0)) {
{
printf("Error: Please enter positive values from 0 to %f. \n", upper_time_bound);
return 0;
}
}

//If they're both still positive, then print this out...
else {
{
printf("Error: Start times and end times reversed. \n");
printf("Please reenter report information correctly. \n");
return 0;
}
}

//Create table for altitude and velocity values
for (double k=0; k<=num_data_pts-1; ++k)
{
t = start_time_hrs + k*(time_incr_min/60);

//Calculate altitude and velocity in m and m/hr
altitude_m = (-.12*t*t*t*t) + (12*t*t*t) - (380*t*t) + (4100*t) + 220;
velocity_m_hr = (-.48*t*t*t) + (36*t*t) - (760*t) + 4100;

//Convert velocity to SI units
velocity_m_s = velocity_m_hr/3600;

//Print out altitude and velocity in m and m/s to balloon file
fprintf(balloon, "%.4f %.4f %.4f \n", t, altitude_m, velocity_m_s);

//Let the peak altitude be equal to the current altitude
peak_altitude = altitude_m;
//If the current altitude is larger than the previous max value...
if (peak_altitude > max)
{
//Let the new max value be equal to
//the current altitude at the current time interval
max = peak_altitude;
t_peak = t;
}
}
printf("\nFinished calculating values \n");
printf("Maximum altitude reached: %.4f m at %.4f hrs", peak_altitude, t_peak);
//Close file and exit program.
fclose(balloon);
return 0;

}

Lab 13 - LCD Display

CLASSWORK:
LCD Print to Screen
#include <LiquidCrystal.h> 
int time = 0;
int k;
LiquidCrystal lcd (8,9,4,5,6,7);  //Initialize an LCD object, takes pin #'s as arguments
void setup() {
  lcd.begin(16, 2);                 //Tell the arduino we have a 16n2 (COL, ROW) display
  lcd.setCursor(0,1);               //Tell the cursor where to be, (0,0)=ORIGIN top left
  lcd.print("Prof Mason");        //Tell the LCD what to print out at the given location
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("DoomsDayIn T-");
  for (k=time; k<=10; k++) {
    lcd.setCursor(13,0);
    lcd.print(k);
    delay(1000);
  }


}

LCD with Progress Bar
//LCD with Progress Bar
//Include the library code:
#include <LiquidCrystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//Create the progress bar characters
byte p20[8] = {
 B10000,
 B10000,
 B10000,
 B10000,
 B10000,
 B10000,
 B10000,
 B10000,
};
byte p40[8] = {
 B11000,
 B11000,
 B11000,
 B11000,
 B11000,
 B11000,
 B11000,
 B11000,
};
byte p60[8] = {
 B11100,
 B11100,
 B11100,
 B11100,
 B11100,
 B11100,
 B11100,
 B11100,
};
byte p80[8] = {
 B11110,
 B11110,
 B11110,
 B11110,
 B11110,
 B11110,
 B11110,
 B11110,
};
byte p100[8] = {
 B11111,
 B11111,
 B11111,
 B11111,
 B11111,
 B11111,
 B11111,
 B11111,
};
void setup()
{
 //Set up the LCDs number of columns and rows:
 lcd.begin(16, 2);
 // Print a message to the LCD.
 lcd.print("ENGR6 Display");
 //Make progress characters
 lcd.createChar(0, p20);
 lcd.createChar(1, p40);
 lcd.createChar(2, p60);
 lcd.createChar(3, p80);
 lcd.createChar(4, p100);
}
void loop()
{
 //Move cursor to second line
 lcd.setCursor(0,1);
 //Clear the line each time it reaches the end
 //with 16 " " (spaces)
 lcd.print("                ");
 //Iterate through each character on the second line
 for (int i = 0; i<16; i++)
 {
 //Iterate through each progress value for each character
 for (int j=0; j<5; j++)
 {
 lcd.setCursor(i, 1); //Move the cursor to this location
 lcd.write(j); //Update progress bar
 delay(100); //Wait
 }
 }

}

HOMEWORK:
LCD Temperature Radar (FAIL)
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int waitTime = 200;
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 lcd.begin(16, 2);
 lcd.print("ENRG6 Display");
}
void loop() {
 // read the input on analog pin 0:
 int sensorValue = analogRead(A0);
 int tempValue = analogRead(A1);
 // print out the value you read:
 Serial.print(sensorValue);
 Serial.print("   ");
 Serial.println(tempValue);
 delay(1); // delay in between reads
 lcd.setCursor(0,1);
 lcd.print("                ");
 lcd.setCursor(0,1);
 /*double Celsius=0.01, Rankine;
  /*  Computes the temperature in Rankine scale. */
  /*Rankine = (Celsius + 273.15)*1.8;*/

 switch (sensorValue) {
    case 0: //RIGHT
      lcd.print("CELSIUS");
      delay(waitTime);
      break;
    case 720: //SELECT
      lcd.print("DEGREES");
      delay(waitTime);
      break;
    default:
      lcd.print("DEFAULT!");
      delay(waitTime);
 }

} 

Lab 12 - I2C Bus

CLASSWORK:
TC74 I2C Temperature Sensor Code
//Reads Temp from I2C temperature sensor and prints it on the serial
//port
//Include Wire I2C library
#include <Wire.h>
int temp_address = 73; //1001000 written as decimal number
int speakerPin = 3;
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
}
void loop()
{
 tone(speakerPin, 1000, 500);
 delay(500);
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 int c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.println("F");
 delay(500);
}


HOMEWORK:
TC74 Sensor with Proportional Sound Output and LCD Display (Work in Progress)
//Reads Temp from I2C temperature sensor and prints it on the serial port
//Include Wire I2C library
#include <Wire.h>
#include <LiquidCrystal.h>
int temp_address = 73; //1001001 written as decimal number
int speakerPin = 3;
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
Serial.print("Startting..");
 //Create a Wire object
 Wire.begin();
 lcd.begin(16,2);
 //lcd.print("TEMP DISPLAY:");
 //lcd.setCursor(0,1);
 //lcd.print("Please print!");
}
void loop()
{
 tone(speakerPin, 1000, 500);
 delay(500);
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0)
 Serial.println("loading..");
 //Get the temp and read it into a variable
 int c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.println("F");

 //Send the temperature in degrees C and F to the LCD display
 lcd.setCursor(2,1);
 lcd.print(c);
 lcd.setCursor(3,1);
 lcd.print("C");
 lcd.setCursor(7,1);
 lcd.print(f);
 lcd.setCursor(8,1);
 lcd.print("F");

 delay(500);

}