Lab 12/ HW 12

Lab 12(I2C sensor):

//Reads Temp from I2C temperature sensor and prints it on the serial port
//Include Wire I2C library
#include "pitches.h"
#include <Wire.h>
int temp_address = 73; //1001000 written as decimal number
int speaker = 9;
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();
  //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);

  //Temp range: 70-82 degree F
  switch (f)
  {
    case 70:
    tone(speaker, NOTE_A3, 500);
    delay(500);
    break;
   
    case 71:
    tone(speaker, NOTE_C3, 500);
    delay(500);
    break;
   
    case 72:
    tone(speaker, NOTE_E3, 500);
    delay(500);
    break;
   
    case 73:
    tone(speaker, NOTE_G3, 500);
    delay(500);
    break;
   
    case 74:
    tone(speaker, NOTE_B4, 500);
    delay(500);
    break;
   
    case 75:
    tone(speaker, NOTE_D4, 500);
    delay(500);
    break;
   
    case 76:
    tone(speaker, NOTE_F4, 500);
    delay(500);
    break;
   
    case 77:
    tone(speaker, NOTE_A5, 500);
    delay(500);
    break;
   
    case 78:
    tone(speaker, NOTE_C5, 500);
    delay(500);
    break;
   
    case 79:
    tone(speaker, NOTE_E5, 500);
    delay(500);
    break;
   
    case 80:
    tone(speaker, NOTE_G5, 500);
    delay(500);
    break;
   
    case 81:
    tone(speaker, NOTE_B6, 500);
    delay(500);
    break;
   
    case 82:
    tone(speaker, NOTE_D6, 500);
    delay(500);
    break;
  }

}

HW 12(Peaks and Valleys):

PeaksNValleys Code:
#include <stdio.h>
#include <math.h>
#define ROWS 6
#define COLS 7
#define FILENAME "grid.txt"
float elevation[ROWS][COLS];
int max, min, countpeak, countvalley; 
float bottom = ROWS-1, left = 0;
FILE* read_elevation;
int main(void)
{
read_elevation = fopen(FILENAME, "r");
if (read_elevation == NULL)
{
printf("Error opening input file.\n");
}
else
{
for (int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
fscanf(read_elevation,"%f", &elevation[i][j]);
}
}
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0. \n");
for (int i=1; i<ROWS-1; i++)
{
for (int j=1; j<COLS-1; j++)
{
if ((elevation[i-1][j]<elevation[i][j]) &&
(elevation[i+1][j]<elevation[i][j]) &&
(elevation[i][j-1]<elevation[i][j]) &&
(elevation[i][j+1]<elevation[i][j]))
{
printf("Peak at row: %d column: %d\n",i,j);
float vertical = i, horizontal = j;
float vertdist = fabs(bottom - i), horizdist = fabs(left - j);
float distance = 100*sqrt((vertdist*vertdist)+(horizdist*horizdist));
printf("Distance from lower left corner: %.3f ft\n\n", distance);
/* Count Peaks */
countpeak++;
}
}
}
/* Print num of peaks*/
printf("Number of Peaks: %d\n", countpeak);
/* Print valley locations */
for (int i=1; i<ROWS-1; i++)
{
for (int j=1; j<COLS-1; j++)
{
if ((elevation[i-1][j]>elevation[i][j]) &&
(elevation[i+1][j]>elevation[i][j]) &&
(elevation[i][j-1]>elevation[i][j]) &&
(elevation[i][j+1]>elevation[i][j]))
{
printf("\nValley at row: %d column: %d",i,j);
/* Count Valleys */
countvalley++;
}
}
}
/* Print num of peaks*/
printf("\nNumber of Valleys: %d\n", countvalley);
max = 0;
min = elevation[0][0];
for (int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
if (max < elevation[i][j])
{
max = elevation[i][j];
}
if (min > elevation[i][j])
{
min = elevation[i][j];
}
}
}
printf("\n\nMax Value: %i", max);
printf("\nMin Value: %i\n", min);
/* Determine peak using 8 points */
for (int i=1; i<ROWS-1; i++)
{
for (int j=1; j<COLS-1; j++)
{
if ((elevation[i-1][j]<elevation[i][j])&&
(elevation[i+1][j]<elevation[i][j])&&
(elevation[i][j-1]<elevation[i][j])&&
(elevation[i][j+1]<elevation[i][j])&&
(elevation[i+1][j+1]<elevation[i][j])
&&(elevation[i-1][j+1]<elevation[i][j])&&
(elevation[i-1][j-1]<elevation[i][j])
&&(elevation[i+1][j-1]<elevation[i][j]))
{
printf("\nPeak(using 8 data points) at row: %d column: %d",i,j);
}
}
}
}
fclose(read_elevation); /* Close file. */
return 0; /* Exit program. */

}

Temperature Reader and Speaker Code:
//Reads Temp from I2C temperature sensor and prints it on the serial port
//Include Wire I2C library
#include <Wire.h>
const int speaker = 9;
int temp_address = 72; //1001000 written as decimal number
int frequency_val = 0;
int frequency=0;
int c = 0;
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
 pinMode(speaker, OUTPUT);
}
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
 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.print("F ");
 delay(500);
 }
 c=map(c,20,35,400,2000);
 c = constrain(c, 400, 2000);
 Serial.print("Frequency: ");
 Serial.println(c);
 tone(speaker, c, 500);
 delay(1000);
}

Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21