Node Chomsky
Posts tagged Arduino
A better video of my DIY arduino-based oscilloscope. The only sound in the room was the Venture Bros. blaring from my laptop at top volume, so I just muted it so you wouldn’t have to listen to it, but I will post another video with a piezo element connected to the signal line so you can hear what it sounds like.
This is the output from My Arduino based DIY oscilloscope with the 4D Oled screen. Code available in a prior post. The trigger works fairly well, it has a threshold and slope adjustment.
Oscilloscope for Arduino and 4D graphics controller oled screen
//////////////////////////////////////////////////////////////////////////////
// Basic Oscilloscope for the Arduino and
// 4D graphics controller based Oled screen
//
// Based on a template for the GOLDELOX-SGC Processor by
// Paul J Karlik <-Thank him for finally writing a library for the screen
// Cubtastic71@gmail.com
//
// The <Oled.h> library is available at:
// http://code.google.com/p/arduino-oled/
//
//////////////////////////////////////////////////////////////////////////////
#include <Oled.h>
#include <string.h>
#include <Wire.h>
// Int an Define the Base Vars and Params ////////////////////////////
OLED myOLED(7,8,2500,57600);
int color=0;
int color2=12;
int color3=36;
int tick = 0;
int trigger = 0;
int signal[5];
int x = 0;
int y = 0;
int px = 96;
int py = 0;
int sigtol = analogRead(A1);
// Set Up Function ///////////////////////////////////////////////////
void setup(){
Serial.begin(57600);
pinMode(10, OUTPUT);
digitalWrite(10,HIGH);
myOLED.Init();
myOLED.Clear();
color = myOLED.get16bitRGB(25,25,25);
color3 = myOLED.get16bitRGB(0,255,0);
color2 = myOLED.get16bitRGB(0,0,0);
}
// Main Program Loop ////////////////////////////////////////////////
void loop(){
//// This ‘while loop’ draws one frame of the graph
while (x <= 96) {
x++;
myOLED.DrawLine(px, py, x, y, color);
px = x;
py = y;
y=(((analogRead(A0)) / 16));
}
// This ‘analogRead()’ and ‘while loop’ allow the device to wait
// for an increasing slope from the Attack of the incoming signal
signal[4] = analogRead(A0);
while ((signal[3] - signal[4]) <= sigtol) {
sigtol = analogRead(A1); //analogRead(A1) reads a 1k pot connected to pin A1
delay(1); //to use as a 1 to 1 threshold for the trigger
signal[4] = analogRead(A0);
delay(1);
delay(int(analogRead(A2)/200));//The slope of the threshold for the trigger with a 1k pot to pin A2
signal[3] = analogRead(A0);
delay(1);
//if you have cut and pasted this from my website, you may have to retype the //following command manually, because of character translation
myOLED.DrawText(1,1,0,”hold”,color3);
}
//This sets origin points for the new frame and clears the screen by drawing a rectangle
x=0;
px = 0;
myOLED.DrawRectangle(0,0,96,64,color2);
}
oLED + Arduino Oscilloscope, trigger, zoom, filtering and sensitivity to come!
Here are the libraries I used:
http://code.google.com/p/arduino-oled/downloads/list
The oLED in this is the 96 x 64 pixel oLed display with a 4D systems graphics controller, this is all done with serial commands from the arduino which results in graphics rendering by the screens controller.
Data Logger with Parallel Interface LCD Code



//Here is the code:
// Thank MemphisTechno for the code
#include <SdFat.h>
#include <Wire.h>
#include “RTClib.h”
#include <LiquidCrystal.h>
//The I2C address of the pressure sensor
#define I2C_ADDRESS 0x77
// A simple data logger for theArduino analog pins
#define LOG_INTERVAL 500 // mills between entries
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3
#define number_of_sound_readings 10
// The analog pins that connect to the sensors
#define photocellPin 0 // analog 0
#define humiPin 1 // analog 1
#define soundPin 2 // analog 2
#define tempPin 3 // analog 3
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
// constants won’t change. They’re used here to
// set pin numbers:
#define buttonPin 4 // the number of the pushbutton pin
#define ledPin 13 // the number of the LED pin
#define piezpin 7 // The pin the buzzer is tied too
// Buzzer Properties
#define chrpFqz 4
#define chrpDly 500
#define chrpQnt 100
const unsigned char oversampling_setting = 3; //oversamplig for measurement
const unsigned char pressure_waittime[4] = { 5, 8, 14, 26 };
//just taken from the BMP085 datasheet
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// for sound output
int tick=0;
// Sensor Variables
float temperatureF = 0;
float photocellReading = 0;
float hmp = 0;
float humiReading = 0;
int hour=0;
int minute=0;
int second=0;
// sound array
float sound_buffer[number_of_sound_readings];
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 5, 4, 3, 2);
RTC_DS1307 RTC; // define the Real Time Clock object
DateTime now;
// The objects to talk to the SD card
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// Create the custom characters for the display
byte smiley[8] = {
B01000,
B10100,
B01000,
B00011,
B00010,
B00011,
B00010,
};
byte rob[8] = {
B00100,
B01010,
B00100,
B00000,
B00000,
B00000,
B00000,
};
byte bvbv[8] = {
B11000,
B10100,
B11000,
B10101,
B11101,
B00101,
B00010,
};
byte edge[8] = {
B11111,
B10101,
B11111,
B10101,
B11111,
B10101,
B11111,
};
void error(char *str)
{
Serial.print(“error: “);
Serial.println(str);
// Print to the LCDp
lcd.print(“error: “);
lcd.print(str);
// Error Sound Function
while(1) {
tick=0;
delay(chrpDly);
while(tick<chrpQnt) {
digitalWrite(piezpin, LOW);
delay(chrpFqz);
digitalWrite(piezpin, HIGH);
tick++;
}
}
}
void sd_card_file_init()
{
// initialize the SD card
if (!card.init()) error(“card.init”);
// initialize a FAT volume
if (!volume.init(card)) error(“volume.init”);
// open root directory
if (!root.openRoot(volume)) error(“openRoot”);
// create a new file “v”+version number+screenstyle+ “p” +”U” unit number+ xx + .CSV
char name[] = “v12pu400.CSV”;
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + ‘0’;
name[7] = i%10 + ‘0’;
if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) error (“file.create”);
//START LCDp Startup Screen &&&&&&&&&&&&&&&&&&&&&
Serial.print(“Logging to: “);
Serial.println(name);
lcd.setCursor(5, 3);
lcd.print(name);
lcd.setCursor(0, 1);
lcd.print(“EcobeX Version b.7”);
lcd.setCursor(0, 0);
lcd.print(“Booting”);
int vrl = 0;
lcd.setCursor(0, 0);
while(vrl < 20){
// do something repetitive 20 times
vrl++;
lcd.print(“*”);
delay(500);
}
lcd.clear();
//END LCDp Startup Screen &&&&&&&&&&&&&&&&&&&&&
// write header
file.writeError = 0;
file.println(“millis,stamp,datetime,light,sound,humidity,tempF,pres,temp2,vcc”);
// attempt to write out the header to the file
if (file.writeError || !file.sync()) {
error(“write header”);
}
}
void start_rtc()
{
if (!RTC.begin()) {
file.println(“RTC failed”);
Serial.print(“RTC failed”);
#if ECHO_TO_SERIAL
Serial.println(“RTC failed”);
#endif //ECHO_TO_SERIAL
}
}
void setup()
{
// serial init
Serial.begin(9600);
Wire.begin();
// Initialize Screen
// backlightOn();
// selectLineOne();
// initialize the error buzzer’s pin as an OUTPUT on pin 5;
pinMode(piezpin, OUTPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
//Push the custom LCDp characters to the display
lcd.createChar(0, smiley);
lcd.createChar(3, rob);
lcd.createChar(2, edge);
lcd.createChar(1, bvbv);
Serial.println();
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);
lcd.leftToRight();
sd_card_file_init();
delay(10);
start_rtc();
// arduino stuff
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
bmp085_get_cal_data();
}
void bmp085_read_temperature_and_pressure(int& temperature, long& pressure);
void loop(void)
{
// delay for the amount of time we want between readings
// digitalWrite(redLEDpin, HIGH);
// clear print error
file.writeError = 0;
// initial delay
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
// time stamp MS
uint32_t m = millis();
int snd_delay = 5;
for (int i = 0; i< number_of_sound_readings; i++){
sound_buffer[i] = analogRead(soundPin);
delay(snd_delay);
}
photocellReading = analogRead(photocellPin);
delay(10);
humiReading = analogRead(humiPin);
delay(10);
float tempReading = analogRead(tempPin);
delay(10);
float refReading = analogRead(BANDGAPREF);
delay(10);
// BAROMETRIC PRESSURE STUFF
int temperature_baro = 0;
long pressure = 0;
bmp085_read_temperature_and_pressure(&temperature_baro,&pressure);
// AVERAGE PORTION
temperature_baro = ((temperature_baro/10) * 9 / 5) + 32;
// Log the estimated ‘VCC’ voltage by measuring the internal 1.1v ref
float supplyvoltage = (bandgap_voltage * 1024) / refReading;
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
photocellReading = ((photocellReading * aref_voltage) / 1024.0)*1000;
// sound average
float sound_total = 0;
for (int i = 0; i<number_of_sound_readings; i++){
sound_total += sound_buffer[i];
}
sound_total = sound_total / number_of_sound_readings;
hmp = 20 * log (sound_total);
humiReading = humiReading * aref_voltage / 1024.0;
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
float voltage = tempReading * aref_voltage / 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
temperatureF = (temperatureC * 9 / 5) + 32;
humiReading = (humiReading - 0.958)/0.0307;
//
// FILE I/O
//
// fetch the time
now = RTC.now();
file.print(m); // milliseconds since start
file.print(“, “);
file.print(now.unixtime()); // seconds since 1/1/1970
file.print(“, “);
file.print(‘”’);
file.print(now.year(), DEC);
file.print(“/”);
file.print(now.month(), DEC);
file.print(“/”);
file.print(now.day(), DEC);
file.print(” “);
file.print(now.hour(), DEC);
file.print(“:”);
file.print(now.minute(), DEC);
file.print(“:”);
file.print(now.second(), DEC);
file.print(‘”’);
/// VARIABLES
file.print(“, “);
file.print(photocellReading);
file.print(“, “);
file.print(hmp);
file.print(“, “);
file.print(humiReading);
file.print(“, “);
file.print(temperatureF);
file.print(“, “);
file.print(pressure);
file.print(“, “);
file.print(temperature_baro);
file.print(“, “);
file.print(supplyvoltage);
file.println();
if (file.writeError) error(“write data”);
if (!file.sync()) error(“sync”);
// digitalWrite(redLEDpin, LOW);
//START LCDp Live Screen &&&&&&&&&&&&&&&&&&&&&
lcd.clear();
lcd.leftToRight();
lcd.setCursor(1, 3);
// Make the Border
//Top
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
// Left Side
lcd.setCursor(0, 1);
lcd.write(2);
lcd.setCursor(0, 2);
lcd.write(2);
lcd.setCursor(0, 3);
lcd.write(2);
// Right Side
lcd.setCursor(9, 1);
lcd.write(2);
lcd.setCursor(9, 2);
lcd.write(2);
lcd.setCursor(9, 3);
lcd.write(2);
//Bottom
lcd.setCursor(0, 0);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.setCursor(1, 2);
lcd.print(now.month(), DEC);
lcd.print(“/”);
lcd.print(now.day(), DEC);
lcd.print(“/”);
lcd.print((now.year()-2000), DEC);
lcd.setCursor(1, 1);
lcd.print(“00:00:00”);
lcd.setCursor(1, 1);
hour=now.hour();
minute=now.minute();
second=now.second();
if (hour <= 9) (lcd.setCursor(2, 1));
lcd.print(now.hour(), DEC);
lcd.setCursor(3, 1);
lcd.print(“:”);
if (minute <= 9) (lcd.setCursor(5, 1));
lcd.print(now.minute(), DEC);
lcd.setCursor(6, 1);
lcd.print(“:”);
if (second <= 9) (lcd.setCursor(8, 1));
lcd.print(now.second(), DEC);
lcd.setCursor(10, 0);
lcd.print(“Tmp”);
lcd.print(“=”);
lcd.print(temperatureF);
lcd.write(0);
lcd.setCursor(10, 3);
lcd.print(“L=”);
lcd.print(photocellReading);
// lcd.print(“mV”);
lcd.setCursor(12, 2);
// lcd.print(hmp);
lcd.print(pressure);
lcd.setCursor(10, 2);
// lcd.print(“SPL=”);
lcd.print(“P=”);
lcd.setCursor(18, 2);
// lcd.print(“d”);
// lcd.write(1);
lcd.print(“Pa”);
lcd.setCursor(10, 1);
lcd.print(“RHu=”);
lcd.print(humiReading);
lcd.print(“%”);
//END LCDp Live Screen &&&&&&&&&&&&&&&&&&&&&
} // X MARKS THE SPOT, END OF LOOP()
//############################################################################
//############################################################################
/////////// BARO METRIC PRESSURE FUNCTIONS
void bmp085_read_temperature_and_pressure(int* temperature, long* pressure) {
long ut= bmp085_read_ut();
long up = bmp085_read_up();
long x1, x2, x3, b3, b5, b6, p;
unsigned long b4, b7;
//calculate the temperature
x1 = ((long)ut - ac6) * ac5 » 15;
x2 = ((long) mc « 11) / (x1 + md);
b5 = x1 + x2;
*temperature = (b5 + 8) » 4;
//calculate the pressure
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 » 12)) » 11;
x2 = ac2 * b6 » 11;
x3 = x1 + x2;
//b3 = (((int32_t) ac1 * 4 + x3)<> 2;
if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) « 1;
if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2);
if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) » 1;
if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) » 2;
x1 = ac3 * b6 » 13;
x2 = (b1 * (b6 * b6 » 12)) » 16;
x3 = ((x1 + x2) + 2) » 2;
b4 = (ac4 * (uint32_t) (x3 + 32768)) » 15;
b7 = ((uint32_t) up - b3) * (50000 » oversampling_setting);
p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
x1 = (p » 8) * (p » 8);
x1 = (x1 * 3038) » 16;
x2 = (-7357 * p) » 16;
*pressure = p + ((x1 + x2 + 3791) » 4);
}
unsigned int bmp085_read_ut() {
write_register(0xf4,0x2e);
delay(5); //longer than 4.5 ms
return read_int_register(0xf6);
}
void bmp085_get_cal_data() {
Serial.println(“Reading Calibration Data”);
ac1 = read_int_register(0xAA);
Serial.print(“AC1: “);
Serial.println(ac1,DEC);
ac2 = read_int_register(0xAC);
Serial.print(“AC2: “);
Serial.println(ac2,DEC);
ac3 = read_int_register(0xAE);
Serial.print(“AC3: “);
Serial.println(ac3,DEC);
ac4 = read_int_register(0xB0);
Serial.print(“AC4: “);
Serial.println(ac4,DEC);
ac5 = read_int_register(0xB2);
Serial.print(“AC5: “);
Serial.println(ac5,DEC);
ac6 = read_int_register(0xB4);
Serial.print(“AC6: “);
Serial.println(ac6,DEC);
b1 = read_int_register(0xB6);
Serial.print(“B1: “);
Serial.println(b1,DEC);
b2 = read_int_register(0xB8);
Serial.print(“B2: “);
Serial.println(b1,DEC);
mb = read_int_register(0xBA);
Serial.print(“MB: “);
Serial.println(mb,DEC);
mc = read_int_register(0xBC);
Serial.print(“MC: “);
Serial.println(mc,DEC);
md = read_int_register(0xBE);
Serial.print(“MD: “);
Serial.println(md,DEC);
}
long bmp085_read_up() {
write_register(0xf4,0x34+(oversampling_setting«6));
delay(pressure_waittime[oversampling_setting]);
unsigned char msb, lsb, xlsb;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(0xf6); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 3); // read a byte
while(!Wire.available()) {
// waiting
}
msb = Wire.receive();
while(!Wire.available()) {
// waiting
}
lsb |= Wire.receive();
while(!Wire.available()) {
// waiting
}
xlsb |= Wire.receive();
return (((long)msb«16) | ((long)lsb«8) | ((long)xlsb)) »(8-oversampling_setting);
}
void write_register(unsigned char r, unsigned char v)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r);
Wire.send(v);
Wire.endTransmission();
}
char read_register(unsigned char r)
{
unsigned char v;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
while(!Wire.available()) {
// waiting
}
v = Wire.receive();
return v;
}
int read_int_register(unsigned char r)
{
unsigned char msb, lsb;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 2); // read a byte
while(!Wire.available()) {
// waiting
}
msb = Wire.receive();
while(!Wire.available()) {
// waiting
}
lsb = Wire.receive();
return (((int)msb«8) | ((int)lsb));
}
Current code for my data logger, mostly uncommented, and the hardware will be documented later.

// here is the code
// Thank MemphisTechno for the code
#include <SdFat.h>
#include <Wire.h>
#include “RTClib.h”
//The I2C address of the pressure sensor
#define I2C_ADDRESS 0x77
// A simple data logger for theArduino analog pins
#define LOG_INTERVAL 500 // mills between entries
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
#define redLEDpin 2
#define number_of_sound_readings 10
// The analog pins that connect to the sensors
#define photocellPin 0 // analog 0
#define humiPin 1 // analog 1
#define soundPin 2 // analog 2
#define tempPin 3 // analog 3
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
// constants won’t change. They’re used here to
// set pin numbers:
#define buttonPin 4 // the number of the pushbutton pin
#define piezpin 5 // The pin the buzzer is tied too
// Buzzer Properties
#define chrpFqz 4
#define chrpDly 500
#define chrpQnt 100
const unsigned char oversampling_setting = 3; //oversamplig for measurement
const unsigned char pressure_waittime[4] = { 5, 8, 14, 26 };
//just taken from the BMP085 datasheet
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// Controls
int tick=0;
// sound array
float sound_buffer[number_of_sound_readings];
RTC_DS1307 RTC; // define the Real Time Clock object
DateTime now;
// The objects to talk to the SD card
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
void error(char *str)
{
Serial.print(“error: “);
Serial.println(str);
while(1) {
tick=0;
delay(chrpDly);
while(tick<chrpQnt) {
digitalWrite(piezpin, LOW);
delay(chrpFqz);
digitalWrite(piezpin, HIGH);
tick++;
}
}
}
void sd_card_file_init()
{
// initialize the SD card
if (!card.init()) error(“card.init”);
// initialize a FAT volume
if (!volume.init(card)) error(“volume.init”);
// open root directory
if (!root.openRoot(volume)) error(“openRoot”);
// create a new file “v”+version number+screenstyle+ “m” +”U” unit number+ xx + .CSV
char name[] = “v12mU300.CSV”;
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + ‘0’;
name[7] = i%10 + ‘0’;
if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) error (“file.create”);
// write header
file.writeError = 0;
file.println(“millis,stamp,datetime,light,sound,humidity,tempF,pres,temp2,vcc”);
// attempt to write out the header to the file
if (file.writeError || !file.sync()) {
error(“write header”);
}
}
void start_rtc()
{
if (!RTC.begin()) {
file.println(“RTC failed”);
Serial.print(“RTC failed”);
#if ECHO_TO_SERIAL
Serial.println(“RTC failed”);
#endif //ECHO_TO_SERIAL
}
}
void setup()
{
// serial init
Serial.begin(9600);
Wire.begin();
// Initialize Screen
backlightOn();
selectLineOne();
// initialize the error buzzer’s pin as an OUTPUT on pin 5;
pinMode(piezpin, OUTPUT);
sd_card_file_init();
delay(10);
start_rtc();
// arduino stuff
pinMode(redLEDpin, OUTPUT);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
bmp085_get_cal_data();
}
void bmp085_read_temperature_and_pressure(int& temperature, long& pressure);
void loop(void)
{
// delay for the amount of time we want between readings
digitalWrite(redLEDpin, LOW);
// clear print error
file.writeError = 0;
// initial delay
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
// time stamp MS
uint32_t m = millis();
int snd_delay = 5;
for (int i = 0; i< number_of_sound_readings; i++){
sound_buffer[i] = analogRead(soundPin);
delay(snd_delay);
}
float photocellReading = analogRead(photocellPin);
delay(10);
float humiReading = analogRead(humiPin);
delay(10);
float tempReading = analogRead(tempPin);
delay(10);
float refReading = analogRead(BANDGAPREF);
delay(10);
// BAROMETRIC PRESSURE STUFF
int temperature_baro = 0;
long pressure = 0;
bmp085_read_temperature_and_pressure(&temperature_baro,&pressure);
// AVERAGE PORTION
temperature_baro = ((temperature_baro/10) * 9 / 5) + 32;
// Log the estimated ‘VCC’ voltage by measuring the internal 1.1v ref
float supplyvoltage = (bandgap_voltage * 1024) / refReading;
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
photocellReading = ((photocellReading * aref_voltage) / 1024.0)*1000;
// sound average
float sound_total = 0;
for (int i = 0; i<number_of_sound_readings; i++){
sound_total += sound_buffer[i];
}
sound_total = sound_total / number_of_sound_readings;
float hmp = 20 * log (sound_total);
humiReading = humiReading * aref_voltage / 1024.0;
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
float voltage = tempReading * aref_voltage / 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9 / 5) + 32;
humiReading = (humiReading - 0.958)/0.0307;
//
// FILE I/O
//
// fetch the time
now = RTC.now();
file.print(m); // milliseconds since start
file.print(“, “);
file.print(now.unixtime()); // seconds since 1/1/1970
file.print(“, “);
file.print(‘”’);
file.print(now.year(), DEC);
file.print(“/”);
file.print(now.month(), DEC);
file.print(“/”);
file.print(now.day(), DEC);
file.print(” “);
file.print(now.hour(), DEC);
file.print(“:”);
file.print(now.minute(), DEC);
file.print(“:”);
file.print(now.second(), DEC);
file.print(‘”’);
/// VARIABLES
file.print(“, “);
file.print(photocellReading);
file.print(“, “);
file.print(hmp);
file.print(“, “);
file.print(humiReading);
file.print(“, “);
file.print(temperatureF);
file.print(“, “);
file.print(pressure);
file.print(“, “);
file.print(temperature_baro);
file.print(“, “);
file.print(supplyvoltage);
file.println();
if (file.writeError) error(“write data”);
if (!file.sync()) error(“sync”);
// LCD OUT STUFF
clearLCD();
selectLineOne();
Serial.print(” ”);
Serial.print(now.hour(), DEC);
Serial.print(“:”);
Serial.print(now.minute(), DEC);
Serial.print(“:”);
Serial.print(now.second(), DEC);
selectLineTwo();
Serial.print(“BP=”);
Serial.print(pressure);
Serial.print(“Pa”);
digitalWrite(redLEDpin, LOW);
} // X MARKS THE SPOT, END OF LOOP()
/////////// BARO METRIC PRESSURE FUNCTIONS
void bmp085_read_temperature_and_pressure(int* temperature, long* pressure) {
long ut= bmp085_read_ut();
long up = bmp085_read_up();
long x1, x2, x3, b3, b5, b6, p;
unsigned long b4, b7;
//calculate the temperature
x1 = ((long)ut - ac6) * ac5 » 15;
x2 = ((long) mc « 11) / (x1 + md);
b5 = x1 + x2;
*temperature = (b5 + 8) » 4;
//calculate the pressure
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 » 12)) » 11;
x2 = ac2 * b6 » 11;
x3 = x1 + x2;
//b3 = (((int32_t) ac1 * 4 + x3)<> 2;
if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) « 1;
if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2);
if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) » 1;
if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) » 2;
x1 = ac3 * b6 » 13;
x2 = (b1 * (b6 * b6 » 12)) » 16;
x3 = ((x1 + x2) + 2) » 2;
b4 = (ac4 * (uint32_t) (x3 + 32768)) » 15;
b7 = ((uint32_t) up - b3) * (50000 » oversampling_setting);
p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
x1 = (p » 8) * (p » 8);
x1 = (x1 * 3038) » 16;
x2 = (-7357 * p) » 16;
*pressure = p + ((x1 + x2 + 3791) » 4);
}
unsigned int bmp085_read_ut() {
write_register(0xf4,0x2e);
delay(5); //longer than 4.5 ms
return read_int_register(0xf6);
}
void bmp085_get_cal_data() {
Serial.println(“Reading Calibration Data”);
ac1 = read_int_register(0xAA);
Serial.print(“AC1: “);
Serial.println(ac1,DEC);
ac2 = read_int_register(0xAC);
Serial.print(“AC2: “);
Serial.println(ac2,DEC);
ac3 = read_int_register(0xAE);
Serial.print(“AC3: “);
Serial.println(ac3,DEC);
ac4 = read_int_register(0xB0);
Serial.print(“AC4: “);
Serial.println(ac4,DEC);
ac5 = read_int_register(0xB2);
Serial.print(“AC5: “);
Serial.println(ac5,DEC);
ac6 = read_int_register(0xB4);
Serial.print(“AC6: “);
Serial.println(ac6,DEC);
b1 = read_int_register(0xB6);
Serial.print(“B1: “);
Serial.println(b1,DEC);
b2 = read_int_register(0xB8);
Serial.print(“B2: “);
Serial.println(b1,DEC);
mb = read_int_register(0xBA);
Serial.print(“MB: “);
Serial.println(mb,DEC);
mc = read_int_register(0xBC);
Serial.print(“MC: “);
Serial.println(mc,DEC);
md = read_int_register(0xBE);
Serial.print(“MD: “);
Serial.println(md,DEC);
}
long bmp085_read_up() {
write_register(0xf4,0x34+(oversampling_setting«6));
delay(pressure_waittime[oversampling_setting]);
unsigned char msb, lsb, xlsb;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(0xf6); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 3); // read a byte
while(!Wire.available()) {
// waiting
}
msb = Wire.receive();
while(!Wire.available()) {
// waiting
}
lsb |= Wire.receive();
while(!Wire.available()) {
// waiting
}
xlsb |= Wire.receive();
return (((long)msb«16) | ((long)lsb«8) | ((long)xlsb)) »(8-oversampling_setting);
}
void write_register(unsigned char r, unsigned char v)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r);
Wire.send(v);
Wire.endTransmission();
}
char read_register(unsigned char r)
{
unsigned char v;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
while(!Wire.available()) {
// waiting
}
v = Wire.receive();
return v;
}
int read_int_register(unsigned char r)
{
unsigned char msb, lsb;
Wire.beginTransmission(I2C_ADDRESS);
Wire.send(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 2); // read a byte
while(!Wire.available()) {
// waiting
}
msb = Wire.receive();
while(!Wire.available()) {
// waiting
}
lsb = Wire.receive();
return (((int)msb«8) | ((int)lsb));
}
// screen stuff
void selectLineOne(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(128, BYTE); //position
}
void selectLineTwo(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(192, BYTE); //position
}
void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
if (position<16){ Serial.print(0xFE, BYTE); //command flag
Serial.print((position+128), BYTE); //position
}else if (position<32){Serial.print(0xFE, BYTE); //command flag
Serial.print((position+48+128), BYTE); //position
} else { goTo(0); }
}
void clearLCD(){
Serial.print(0xFE, BYTE); //command flag
Serial.print(0x01, BYTE); //clear command.
}
void backlightOn(){ //turns on the backlight
Serial.print(0x7C, BYTE); //command flag for backlight stuff
Serial.print(157, BYTE); //light level.
}
void backlightOff(){ //turns off the backlight
Serial.print(0x7C, BYTE); //command flag for backlight stuff
Serial.print(128, BYTE); //light level for off.
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
Serial.print(0xFE, BYTE);
}
