Seeed Studio Grove Sensors Example

Dependencies:   mbed DHT DigitDisplay

Fork of STM32_Read_Sensors_Example by AT&T Developer Summit Hackathon 2016

main.cpp

Committer:
dangriffin
Date:
2014-12-18
Revision:
7:d52c3793a04a
Parent:
6:d25c6da3f0b6

File content as of revision 7:d52c3793a04a:

#include "mbed.h"
#include "ADXL345_I2C.h"
#include "DHT.h"
#include "DigitDisplay.h"


//#define LED                 // Grove LED
//#define ACCEL               // SEN04051P Grove - 3-Axis Digital Accelerometer(±16g)
//#define LIGHT_SENSOR        // Grove Light Sensor using GL5528 photoresistor 
//#define WATER_SENSOR        // Grove Water Sensor
//#define GAS_SENSOR          // SEN90512P Grove - Gas Sensor(MQ2)
//#define TEMP_SENSOR         // Grove Temperature Sensor using TTC03 Thermistor 
//#define TEMP_HUMID_SENSOR   // Grove Temp/Humidity Sensor (DHT11)
#define DIGIT_DISPLAY       // Grove 4-digit display

 
AnalogIn temp_sensor(A0);     
AnalogIn analog_light_sensor_read(A1); 
DigitalIn water_sensor(A2);
AnalogIn gas_sensor(A3);
DigitalOut my_led(D7);
ADXL345_I2C accelerometer(D14, D15);
DHT temp_humid_sensor(A0, DHT11);  // See DHT.h for other supported sensor types
DigitDisplay DigitalDisplay(D2, D3); // For connection to port D2 on Grove shield

int main() {
    
    float vol;
    float Rsensor;
    int gas_sensor_value;
    int sound_sensor_value;
    int adc_scale = 4096; 
    int readings[3] = {0, 0, 0};
    int sensorValue;
    int B = 3975;
    float resistance; 
    float temperature;
    float temperature_f;  
    float humidity;
    float dewpoint;
    char amb_temp[6];
    int a;
    
#ifdef ACCEL     
    printf("Starting ADXL345 test...\n\r");
    wait(.001);
    printf("Device ID is: 0x%02x\n\r", accelerometer.getDeviceID());
    wait(.001);
    
    // These are here to test whether any of the initialization fails. It will print the failure
    if (accelerometer.setPowerControl(0x00))
    {
         printf("didn't intitialize power control\n"); 
         return 0;  
    }
    
    //Full resolution, +/-16g, 4mg/LSB.
    wait(.001);
     
    if(accelerometer.setDataFormatControl(0x0B))
    {
        printf("didn't set data format\n");
        return 0;  
    }
    wait(.001);
     
     //3.2kHz data rate.
    if(accelerometer.setDataRate(ADXL345_3200HZ))
    {
        printf("didn't set data rate\n");
        return 0;    
    }
    wait(.001);
     
    //Measurement mode.     
    if(accelerometer.setPowerControl(MeasurementMode)) 
    {
        printf("didn't set the power control to measurement\n"); 
        return 0;   
    } 
#endif
    
    while(1) 
    {
#ifdef LED        
        my_led = 1; // LED is ON
        wait(0.2); // 200 ms
        my_led = 0; // LED is OFF
        wait(1.0); // 1 sec
#endif

#ifdef WATER_SENSOR        
        printf("water_sensor: %d \n\r", water_sensor.read());   
        wait(0.5);                  
#endif

#ifdef GAS_SENSOR        
        gas_sensor_value = gas_sensor.read_u16();
        printf("gas_sensor_value: 0x%X \n\r", gas_sensor_value);          

        vol = (float)gas_sensor_value/adc_scale*5.0;
        printf("gas vol: %f \n\r", vol);  
        wait(1);          
#endif

#ifdef ACCEL
        accelerometer.getOutput(readings);     
        wait(0.1); // 100 ms        
            
        /* x-axis, y-axis and z-axis */        
        printf("%d, %d, %d\n\r", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
#endif

#ifdef LIGHT_SENSOR
        sensorValue = analog_light_sensor_read.read_u16();               
        Rsensor=(float)(adc_scale-sensorValue)*10/sensorValue;
        printf("Light Sensor Analog Reading is 0x%X = %d   ", sensorValue, sensorValue);        
        printf("The sensor resistance is %f  \n\n\r", Rsensor);  
        wait(1); // 1s  
#endif
      
#ifdef TEMP_SENSOR
        a = temp_sensor.read_u16();
               
        resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;              
        temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;  //convert to temperature via datasheet ;        
        temperature_f = (1.8 * temperature) + 32.0;
        sprintf(amb_temp, "%0.2f", temperature_f);  
        
        printf("Temp Sensor Analog Reading is 0x%X = %d   ", a, a);         
        printf("Current Temperature: %f C  %f F \n\r", temperature, temperature_f); 
        wait(1); // 1s          
#endif      

#ifdef TEMP_HUMID_SENSOR
        temp_humid_sensor.readData();
        
        temperature = temp_humid_sensor.ReadTemperature(CELCIUS);
        temperature_f = temp_humid_sensor.ReadTemperature(FARENHEIT);
        humidity = temp_humid_sensor.ReadHumidity();
        dewpoint = temp_humid_sensor.CalcdewPointFast(temperature, humidity);
        
        printf("\r\n");
        printf("Temperature: %f C / %f F\r\n", temperature, temperature_f);
        printf("Humidity: %f%%\r\n", humidity);
        printf("Dewpoint: %f C / %f F\r\n", dewpoint, (dewpoint * 1.8f) + 32);
        wait(1);
#endif

#ifdef DIGIT_DISPLAY
        static int16_t message[] = {0xF, 0xE, 0xE, 0xD, 0xC, 0x0, 0xD, 0xE};
        static int ArraySize = sizeof(message)/sizeof(message[0]);
        static int MessageStart = 0;
        DigitalDisplay.on();

        for (int position = 0; position <= 3; position++)
        {
            DigitalDisplay.write(position, message[(position + MessageStart) % ArraySize]);
        }
        MessageStart = (MessageStart + 1) % ArraySize;

        wait(.3);
#endif

    }
}