santos_ej4_modulo3

Dependencies:   mbed DHT22

Revision:
0:df2df6fad3f6
diff -r 000000000000 -r df2df6fad3f6 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 04 18:47:02 2022 +0000
@@ -0,0 +1,76 @@
+#include "mbed.h"
+#include "DHT22.h"
+
+Serial pc(USBTX, USBRX);
+
+// Se definen la entradas analógicas
+
+AnalogIn ldr(A0);
+
+AnalogIn dataX(A1);                                
+AnalogIn dataY(A2);
+AnalogIn dataZ(A3);
+
+// Se inicializa el DHT con la librería
+
+DHT22 dht22(D2);
+
+int magicNumber;
+
+void bucle() {
+    pc.baud(4800);
+    double x,y,z;
+    double x_g_value,y_g_value,z_g_value;
+    double roll, pitch;
+    int hum;
+    int temp;
+    while(1) {
+        float ldrStatus = ldr.read();
+        
+        x = dataX.read();
+        y = dataY.read();
+        z = dataZ.read();
+        
+        // Calculamos las aceleraciones en g
+        x_g_value = ( ( ( (double)(x * 5)/1024) - 1.65 ) / 0.330 );
+        y_g_value = ( ( ( (double)(y * 5)/1024) - 1.65 ) / 0.330 );
+        z_g_value = ( ( ( (double)(z * 5)/1024) - 1.80 ) / 0.330 );
+        
+        //A partir de los valores del acelerometro, se calculan los angulos Y, X
+        //respectivamente, con la formula de la tangente.
+        roll = ( ( (atan2(y_g_value,z_g_value) * 180) / 3.14 ) + 180 );
+        pitch = ( ( (atan2(z_g_value,x_g_value) * 180) / 3.14 ) + 180 );
+        
+        // Conseguimos humedad y temperatura en grados del DHT
+        dht22.sample() ;
+        hum=dht22.getHumidity()/10.0;
+        temp=dht22.getTemperature()/10.0;
+
+        pc.printf("Esperando el numero magico...\n\r");
+        scanf("%d", &magicNumber);
+        if(magicNumber == 1){
+            pc.printf("-------------------------------\r\n");
+            pc.printf("\r\n");
+            
+            // Lectura LDR
+            pc.printf("LDR READING = %4.2f \r\n", ldrStatus);
+
+            // Lectura IMU
+            pc.printf("Accelerometer X : %f\n\r", x);
+            pc.printf("Accelerometer Y : %f\n\r", y);
+            pc.printf("Accelerometer Z : %f\n\r", z);
+            pc.printf("Roll: %f\n\r",roll);
+            pc.printf("pitch: %f\n\r",pitch);
+            
+            // Lectura DHT
+            pc.printf("temp: %d  , hum:%d    \r\n",temp,hum);
+            pc.printf("\r\n");
+            pc.printf("-------------------------------\r\n");
+            pc.printf("\r\n");
+        }
+    }
+}
+
+int main() {
+    bucle();
+}
\ No newline at end of file