Day_13
Comment: I had a lot of trouble understanding how to code works for the homework, I couldn't finish all of the modifications without getting an error. So I only finished the first three modifications, which took me a long time to develop. For this lab I initially had trouble getting the speaker to change the pitch according the the change in temperature. Turns out I had an extra if statement in my program and that resolved the problem. I spent about 4 hours debugging before I found the extra if statement. Also, I used a potomieter because the speaker would make the lcd screen go crazy since without the pot there was no resistance.
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j,count=0,count_2=0;
double elevation[N][N];
double valleys[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
double maxValue=elevation[N][N];
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; 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);
count++;
}
if(elevation[N][N]>maxValue)
{
maxValue = elevation[N][N];
}
printf("numbers of peaks: %i\n",count);
printf("Maximum value of peaks: %i\n",maxValue);
fclose(grid); /* Close file. */
}
fscanf(grid,"\n%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; 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("Valley at row: %d column: %d \n",i,j);
count_2++;
}
printf("numbers of Valley: %i",count_2);
fclose(grid); /* Close file. */
return 0; /* Exit program. */
}
// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
//#include "pitches.h"
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);
int temp_address = 73;
int potPin1 = A1;
int speaker=A3;
int val_1=0;
const int MIN_temp_f=30;
const int MAX_temp_f=1023;
void setup()
{
Serial.begin(9600); //Start serial communication at 9600 baud
lcd.begin(16, 2);
lcd.clear();
// set up the LCD's number of columns and rows:
pinMode(potPin1, INPUT);
Wire.begin();//Create a Wire object
}
void loop()
{
//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);
//
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print(c);
lcd.setCursor(3,0);
lcd.print("C"); // Prints value on Potpin1 to LCD
lcd.setCursor(0,1);
lcd.print(f);
lcd.setCursor(3,1);
lcd.print("f");
val_1=(f); // Read the value of the sensor but in faraheient
val_1=map(val_1,77,85,MIN_temp_f,MAX_temp_f);
tone(speaker,val_1,500);
//val_1=constrain(val_1,80,500);
Serial.println(val_1);
delay(500);
}
Picture of lab:
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j,count=0,count_2=0;
double elevation[N][N];
double valleys[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
double maxValue=elevation[N][N];
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; 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);
count++;
}
if(elevation[N][N]>maxValue)
{
maxValue = elevation[N][N];
}
printf("numbers of peaks: %i\n",count);
printf("Maximum value of peaks: %i\n",maxValue);
fclose(grid); /* Close file. */
}
fscanf(grid,"\n%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; 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("Valley at row: %d column: %d \n",i,j);
count_2++;
}
printf("numbers of Valley: %i",count_2);
fclose(grid); /* Close file. */
return 0; /* Exit program. */
}
// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
//#include "pitches.h"
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);
int temp_address = 73;
int potPin1 = A1;
int speaker=A3;
int val_1=0;
const int MIN_temp_f=30;
const int MAX_temp_f=1023;
void setup()
{
Serial.begin(9600); //Start serial communication at 9600 baud
lcd.begin(16, 2);
lcd.clear();
// set up the LCD's number of columns and rows:
pinMode(potPin1, INPUT);
Wire.begin();//Create a Wire object
}
void loop()
{
//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);
//
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print(c);
lcd.setCursor(3,0);
lcd.print("C"); // Prints value on Potpin1 to LCD
lcd.setCursor(0,1);
lcd.print(f);
lcd.setCursor(3,1);
lcd.print("f");
val_1=(f); // Read the value of the sensor but in faraheient
val_1=map(val_1,77,85,MIN_temp_f,MAX_temp_f);
tone(speaker,val_1,500);
//val_1=constrain(val_1,80,500);
Serial.println(val_1);
delay(500);
}
Picture of lab:
Comments
Post a Comment