Posts

BMP180 Code + Library to SD card

#include <Wire.h> #include <SD.h> #include "BMP180Lib.c" #define BMP180_ADDRESS 0x77 // I2C address of BMP180 // Read 1 byte from the BMP180 at 'address' // Add these to the top of your program const float p0 = 101325; // Pressure at sea level (Pa) float altitude; const int CS_PIN =10; char bmp180Read(unsigned char address) {  unsigned char data;  Wire.beginTransmission(BMP180_ADDRESS);  Wire.write(address);  Wire.endTransmission();  Wire.requestFrom(BMP180_ADDRESS, 1);  while(!Wire.available())  ;  return Wire.read(); } // Read 2 bytes from the BMP180 // First byte will be from 'address' // Second byte will be from 'address'+1 int bmp180ReadInt(unsigned char address) {  unsigned char msb, lsb;  Wire.beginTransmission(BMP180_ADDRESS);  Wire.write(address);  Wire.endTransmission();  Wire.requestFrom(BMP180_ADDRESS, 2);  while(Wire.available()<2)  ;  msb = Wire....

Strings

#include <stdio.h> #include <string.h> int main(void) { /* Declare and initialize variables. */ int count=0; int choice = 0; int l_l, l_s; char long_str[80]; char short_str[79]; char *ptr1=long_str, *ptr2=short_str; while (choice != 1 && choice != 2){ printf( "1. read from file \n OR 2. input longstring "); scanf(" %i", &choice); } printf(" \n"); if( choice == 1) { FILE *file; file = fopen("longstring.txt", "r"); if(file){ fgets(long_str, 80, file); } fclose(file); } if (choice == 2){ printf("Print the long string: \t"); scanf("%s", &long_str); } printf("Print short string: \t"); scanf("%s", &short_str); l_l = strlen(long_str); l_s = strlen(short_str); printf("Original Long String: "); for (int i =0;i<=l_l; i++){ printf("%c", long_str[i]); if(((int)long_str[i]) < 97){ long_str[i] = (cha...

Seismic Code

/* determines the times of possible seismic events. */ #include <stdio.h> #define FILENAME "seismic1.txt" #define MAX_SIZE 1000 float THRESHOLD = 0; int x = 0; int main(void) { /* Declare variables and function prototypes. */ while(THRESHOLD <=2 1){ printf("Enter Threshold: \n"); scanf(" %f ", &THRESHOLD); printf("Threshold: %f \n", THRESHOLD); } int k, npts, short_window, long_window; double sensor[MAX_SIZE], time_incr, short_power, long_power, ratio; FILE *file_ptr; double power_w(double *ptr,int n); /* Read sensor data file. */ file_ptr = fopen(FILENAME,"r"); if (file_ptr == NULL) printf("Error opening input file. \n"); else { fscanf(file_ptr,"%d %lf",&npts,&time_incr); if (npts > MAX_SIZE) printf("Data file too large for array. \n"); else { /* Read data into an array. */ for (k=0; k<=npts-1; k++) fscanf(file_ptr,"%l...

Temperature Sensor + Potentiometer

const int POT = 0; double val = 0; double voltage = 0; float temp = 0; int red = 7; int green = 6; int blue = 5; void setup() {   // put your setup code here, to run once:  Serial.begin(9600); } void loop() {   // put your main code here, to run repeatedly:  val = analogRead(A0);  voltage = (val)*5;  temp = (voltage-500)/10;  Serial.println(val);  delay(500);  if (val<140){   digitalWrite(blue, HIGH);    }  else if (140<=val<= 150){     digitalWrite(green, HIGH);    }   if (150 < val){   digitalWrite(red, HIGH);  } }

I^2C Temperature Sensor Configured with LCD screen and Buttons

//Reads Temp from I2C temperature sensor and prints it on the serial port //Include Wire I2C library #include <Wire.h> #include <LiquidCrystal.h> //Initialize the library with the numbers of the interface pins LiquidCrystal lcd(8, 9, 4, 5, 6, 7); int temp_address = 72; //1001000 written as decimal number int speaker = 8; void setup() {  //Start serial communication at 9600 baud Serial.begin(9600);  //Create a Wire object  Wire.begin();  lcd.begin(16,2); } 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); ...

Inclinometer Sketch

#include <math.h> #include <LiquidCrystal.h> LiquidCrystal lcd(8, 9, 4, 5, 6, 7); const int xpin = A2; // x-axis of the accelerometer const int ypin = A1; // y-axis const int zpin = A3;// z-axis int x_data; int y_data; int z_data; float x_rad; float y_rad; float degree; unsigned long time; void setup() {  // initialize the serial communications:  Serial.begin(57600);  lcd.begin(16,2);  lcd.print("Angle:"); } void loop() {  x_data = analogRead(xpin);  y_data = analogRead(ypin);  z_data = analogRead(zpin); // // /*time = millis(); //// //// Serial.print(time); //prints time since program started //// ////Serial.print("\t"); */ // // // print the sensor values: // // // // print a tab between values: // // Serial.print(x_data); // // Serial.print("\t"); // // // // // print a tab between values: // // Serial.print(y_data); // // Serial.print("\t"); // Serial.println(z_data); ...

Peaks And Valleys Code

#include <stdio.h> #include <math.h> #define N 25 #define FILENAME "grid1.txt" int main(void) { /* Declare variables. */ int nrows, ncols, i, j, num_peaks=0, num_valleys = 0; int highest, lowest; int row_highest, row_lowest, col_highest, col_lowest; double location =0; double elevation[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); 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]) && (...