eka sinambela / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Fork of g3_waterplay by Mario Simaremare

Files at this revision

API Documentation at this revision

Comitter:
mariosimaremare
Date:
Thu Jun 02 17:53:02 2016 +0000
Child:
1:f448c12d2c5b
Commit message:
Salinity and temperature sensors are implemented in classes.

Changed in this revision

Flasher.cpp Show annotated file Show diff for this revision Revisions of this file
Flasher.h Show annotated file Show diff for this revision Revisions of this file
SalinitySensor.cpp Show annotated file Show diff for this revision Revisions of this file
SalinitySensor.h Show annotated file Show diff for this revision Revisions of this file
TemperatureSensor.cpp Show annotated file Show diff for this revision Revisions of this file
TemperatureSensor.h Show annotated file Show diff for this revision Revisions of this file
Waterplay.cpp Show annotated file Show diff for this revision Revisions of this file
Waterplay.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flasher.cpp	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,13 @@
+#include "Flasher.h"
+#include "mbed.h"
+ 
+Flasher::Flasher(PinName pin) : _pin(pin) {
+    _pin = 0;
+}
+ 
+void Flasher::flash(int n) {
+    for(int i=0; i<n*2; i++) {
+        _pin = !_pin;
+        wait(0.2);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flasher.h	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,15 @@
+#ifndef MBED_FLASHER_H
+#define MBED_FLASHER_H
+
+#include "mbed.h"
+ 
+class Flasher {
+public:
+    Flasher(PinName pin);
+    void flash(int n);
+  
+private:
+    DigitalOut _pin;
+};
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SalinitySensor.cpp	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,41 @@
+/*
+* G3: WATERPLAY
+*/
+
+#include "SalinitySensor.h"
+#include "mbed.h"
+
+SalinitySensor::SalinitySensor(
+    PinName pin,
+    double const_voltage,
+    double const_converter,
+    double const_multiplier
+):
+    _analog_in(pin),
+    _const_voltage(const_voltage),
+    _const_converter(const_converter),
+    _const_multiplier(const_multiplier)
+{
+    read();
+}
+
+double SalinitySensor::read()
+{
+    _voltage = _analog_in.read();
+
+    return(_voltage);
+}
+
+double SalinitySensor::getVoltage()
+{
+    double retVal = _voltage * _const_voltage * _const_converter;
+
+    return(retVal);
+}
+
+double SalinitySensor::getSalinity()
+{
+    double retVal = getVoltage() * _const_multiplier;
+
+    return(retVal);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SalinitySensor.h	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,31 @@
+/*
+* G3: WATERPLAY
+*/
+
+#ifndef SALINITY_SENSOR_H
+#define SALINITY_SENSOR_H
+
+#include "mbed.h"
+
+class SalinitySensor
+{
+public:
+    SalinitySensor(
+        PinName pin,
+        double const_voltage,
+        double const_converter,
+        double const_multiplier
+    );
+    double read();
+    double getVoltage();
+    double getSalinity();
+
+private:
+    AnalogIn _analog_in;
+    double _const_voltage;
+    double _const_converter;
+    double _const_multiplier;
+    double _voltage;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TemperatureSensor.cpp	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,56 @@
+/*
+* G3: WATERPLAY
+*/
+
+#include "TemperatureSensor.h"
+#include "mbed.h"
+
+TemperatureSensor::TemperatureSensor(
+    PinName pin,
+    double const_voltage,
+    double const_converter,
+    double variance,
+    double vin,
+    double resistance
+):
+    _analog_in(pin),
+    _const_voltage(const_voltage),
+    _const_converter(const_converter),
+    _variance(variance),
+    _vin(vin),
+    _resistance(resistance),
+    _k0(0.00102119),
+    _k1(0.000222468),
+    _k2(0.000000133342),
+    _kelvin_to_celcius(-273.15)
+{
+    read();
+}
+
+double TemperatureSensor::read()
+{
+    _voltage = _analog_in.read();
+
+    return(_voltage);
+}
+
+double TemperatureSensor::getVoltage()
+{
+    float retVal = _voltage * _const_voltage;
+
+    return(retVal);
+}
+
+double TemperatureSensor::getTemperature()
+{
+    double vout = getVoltage();
+    double RT = (vout * _resistance) / (_vin - vout);
+    double logRT = log(RT);
+    double K0 = _k0;
+    double K1 = _k1 * logRT;
+    double K2 = _k2 * pow(logRT, 3.0);
+    double kelvin = 1.0 / (K0 + K1 + K2);
+    double celcius = (kelvin + _kelvin_to_celcius) + _variance;
+
+    return(celcius);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TemperatureSensor.h	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,39 @@
+/*
+* G3: WATERPLAY
+*/
+
+#ifndef TEMPERATURE_SENSOR_H
+#define TEMPERATURE_SENSOR_H
+
+#include "mbed.h"
+
+class TemperatureSensor
+{
+public:
+    TemperatureSensor(
+        PinName pin,
+        double const_voltage,
+        double const_converter,
+        double variance,
+        double vin,
+        double resistance
+    );
+    double read();
+    double getVoltage();
+    double getTemperature();
+
+private:
+    AnalogIn _analog_in;
+    double _const_voltage;
+    double _const_converter;
+    double _variance;
+    double _vin;
+    double _resistance;
+    double _voltage;
+    double _k0;
+    double _k1;
+    double _k2;
+    double _kelvin_to_celcius;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Waterplay.cpp	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,16 @@
+#include "Waterplay.h"
+#include "mbed.h"
+
+Waterplay::Waterplay(mbed::Serial &serial):_serial(serial)
+{
+    _serial.printf("init\n\r");
+}
+
+
+
+
+
+void Waterplay::calculate(int n)
+{
+    _serial.printf("calculate\n\r");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Waterplay.h	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,16 @@
+#ifndef WATERPLAY_H
+#define WATERPLAY_H
+
+#include "mbed.h"
+
+class Waterplay
+{
+public:
+    Waterplay(mbed::Serial &serial);
+    void calculate(int n);
+
+private:
+    Serial &_serial;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,60 @@
+/*
+* G3: WATERPLAY
+*/
+
+#include "mbed.h"
+#include "Flasher.h"
+#include "Waterplay.h"
+#include "SalinitySensor.h"
+#include "TemperatureSensor.h"
+
+char program_name[128] = "G3: WATERPLAY";
+int keep_running = 1;
+Serial serial(USBTX, USBRX);
+Flasher led1(LED1);
+Waterplay waterplay(serial);
+
+double salinity_voltage = 3.3;
+double salinity_converter = 1.6667; // 5.0/3.0
+double salinity_multiplier = 16.3;
+
+double temperature_voltage = 4.85;
+double temperature_converter = 1.6667; // 5.0/3.0
+double temperature_variance = 5.0;
+double temperature_vin = 4.85;
+double temperature_resistance = 15000.0;
+
+SalinitySensor salinitySensor(
+    p16,
+    salinity_voltage,
+    salinity_converter,
+    salinity_multiplier
+);
+
+TemperatureSensor temperatureSensor(
+    p15,
+    temperature_voltage,
+    temperature_converter,
+    temperature_variance,
+    temperature_vin,
+    temperature_resistance
+);
+
+int main()
+{
+    serial.printf("\n\r%s\n\r", program_name);
+
+    while(keep_running) {
+        led1.flash(5);
+
+        serial.printf(
+            "salinity: %5.4F | %5.4F |%5.4F :: temperature: %5.4F | %5.4F |%5.4F\n\r",
+            salinitySensor.read(),
+            salinitySensor.getVoltage(),
+            salinitySensor.getSalinity(),
+            temperatureSensor.read(),
+            temperatureSensor.getVoltage(),
+            temperatureSensor.getTemperature()
+        );
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jun 02 17:53:02 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file