Thursday, October 11, 2018

day 13 lab

//Reads Temp from I2C temperature sensor and prints it on the serialport
//Include Wire I2C library
#include <Wire.h>
#include "pitches.h"
const int speaker=9;
int temp_address = 72; //1001000 written as decimal number
int notes[] = { NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1};
int value[] = { 22, 23, 24, 25, 26} ;
int d;
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
}
void loop()
{
 //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();
 c = map(c, 22, 32, 22, 32);
 c = constrain(c, 22, 32);
 //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.println(c);
 //Serial.print("C ");
 //Serial.print(f);
 //Serial.println("F");
 delay(500);
 switch ( c )
 {
 case 26:
  {
    tone(speaker, NOTE_DS1);
    break;
  }
  case 25:
  {
    tone(speaker, NOTE_D1);
    break;
  }
  case 24:
 {
  tone(speaker, NOTE_CS1);
  break;
  }
  case 23:
  {
     tone(speaker, NOTE_C1);
     break;
  }
  case 22:
  {
    tone(speaker, NOTE_B0);
    break;
  }
 }
return;
}

No comments:

Post a Comment