Iván Santos
/
IDW01M1_Cloud_IBM14
santos_ej3 modulo 3
main.cpp@0:5e1dd727b886, 2022-05-04 (annotated)
- Committer:
- sedu_santos
- Date:
- Wed May 04 18:22:06 2022 +0000
- Revision:
- 0:5e1dd727b886
santos_ej3 modulo 3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sedu_santos | 0:5e1dd727b886 | 1 | #include "mbed.h" |
sedu_santos | 0:5e1dd727b886 | 2 | #include "DHT22.h" |
sedu_santos | 0:5e1dd727b886 | 3 | |
sedu_santos | 0:5e1dd727b886 | 4 | Serial pc(USBTX, USBRX); |
sedu_santos | 0:5e1dd727b886 | 5 | |
sedu_santos | 0:5e1dd727b886 | 6 | // Se definen la entradas analógicas |
sedu_santos | 0:5e1dd727b886 | 7 | |
sedu_santos | 0:5e1dd727b886 | 8 | AnalogIn ldr(A0); |
sedu_santos | 0:5e1dd727b886 | 9 | |
sedu_santos | 0:5e1dd727b886 | 10 | AnalogIn dataX(A1); |
sedu_santos | 0:5e1dd727b886 | 11 | AnalogIn dataY(A2); |
sedu_santos | 0:5e1dd727b886 | 12 | AnalogIn dataZ(A3); |
sedu_santos | 0:5e1dd727b886 | 13 | |
sedu_santos | 0:5e1dd727b886 | 14 | // Se inicializa el DHT con la librería |
sedu_santos | 0:5e1dd727b886 | 15 | |
sedu_santos | 0:5e1dd727b886 | 16 | DHT22 dht22(D2); |
sedu_santos | 0:5e1dd727b886 | 17 | |
sedu_santos | 0:5e1dd727b886 | 18 | void bucle() { |
sedu_santos | 0:5e1dd727b886 | 19 | pc.baud(4800); |
sedu_santos | 0:5e1dd727b886 | 20 | double x,y,z; |
sedu_santos | 0:5e1dd727b886 | 21 | double x_g_value,y_g_value,z_g_value; |
sedu_santos | 0:5e1dd727b886 | 22 | double roll, pitch; |
sedu_santos | 0:5e1dd727b886 | 23 | int hum; |
sedu_santos | 0:5e1dd727b886 | 24 | int temp; |
sedu_santos | 0:5e1dd727b886 | 25 | while(1) { |
sedu_santos | 0:5e1dd727b886 | 26 | pc.printf("-------------------------------\r\n"); |
sedu_santos | 0:5e1dd727b886 | 27 | pc.printf("\r\n"); |
sedu_santos | 0:5e1dd727b886 | 28 | float ldrStatus = ldr.read(); |
sedu_santos | 0:5e1dd727b886 | 29 | pc.printf("LDR READING = %4.2f \r\n", ldrStatus); |
sedu_santos | 0:5e1dd727b886 | 30 | |
sedu_santos | 0:5e1dd727b886 | 31 | x = dataX.read(); |
sedu_santos | 0:5e1dd727b886 | 32 | y = dataY.read(); |
sedu_santos | 0:5e1dd727b886 | 33 | z = dataZ.read(); |
sedu_santos | 0:5e1dd727b886 | 34 | |
sedu_santos | 0:5e1dd727b886 | 35 | // Calculamos las aceleraciones en g |
sedu_santos | 0:5e1dd727b886 | 36 | x_g_value = ( ( ( (double)(x * 5)/1024) - 1.65 ) / 0.330 ); |
sedu_santos | 0:5e1dd727b886 | 37 | y_g_value = ( ( ( (double)(y * 5)/1024) - 1.65 ) / 0.330 ); |
sedu_santos | 0:5e1dd727b886 | 38 | z_g_value = ( ( ( (double)(z * 5)/1024) - 1.80 ) / 0.330 ); |
sedu_santos | 0:5e1dd727b886 | 39 | |
sedu_santos | 0:5e1dd727b886 | 40 | //A partir de los valores del acelerometro, se calculan los angulos Y, X |
sedu_santos | 0:5e1dd727b886 | 41 | //respectivamente, con la formula de la tangente. |
sedu_santos | 0:5e1dd727b886 | 42 | roll = ( ( (atan2(y_g_value,z_g_value) * 180) / 3.14 ) + 180 ); |
sedu_santos | 0:5e1dd727b886 | 43 | pitch = ( ( (atan2(z_g_value,x_g_value) * 180) / 3.14 ) + 180 ); |
sedu_santos | 0:5e1dd727b886 | 44 | |
sedu_santos | 0:5e1dd727b886 | 45 | |
sedu_santos | 0:5e1dd727b886 | 46 | pc.printf("Accelerometer X : %f\n\r", x); |
sedu_santos | 0:5e1dd727b886 | 47 | pc.printf("Accelerometer Y : %f\n\r", y); |
sedu_santos | 0:5e1dd727b886 | 48 | pc.printf("Accelerometer Z : %f\n\r", z); |
sedu_santos | 0:5e1dd727b886 | 49 | pc.printf("Roll: %f\n\r",roll); |
sedu_santos | 0:5e1dd727b886 | 50 | pc.printf("pitch: %f\n\r",pitch); |
sedu_santos | 0:5e1dd727b886 | 51 | |
sedu_santos | 0:5e1dd727b886 | 52 | // Conseguimos humedad y temperatura en grados del DHT |
sedu_santos | 0:5e1dd727b886 | 53 | dht22.sample() ; |
sedu_santos | 0:5e1dd727b886 | 54 | hum=dht22.getHumidity()/10.0; |
sedu_santos | 0:5e1dd727b886 | 55 | temp=dht22.getTemperature()/10.0; |
sedu_santos | 0:5e1dd727b886 | 56 | pc.printf("temp: %d , hum:%d \r\n",temp,hum); |
sedu_santos | 0:5e1dd727b886 | 57 | pc.printf("\r\n"); |
sedu_santos | 0:5e1dd727b886 | 58 | pc.printf("-------------------------------\r\n"); |
sedu_santos | 0:5e1dd727b886 | 59 | pc.printf("\r\n"); |
sedu_santos | 0:5e1dd727b886 | 60 | |
sedu_santos | 0:5e1dd727b886 | 61 | wait(1); |
sedu_santos | 0:5e1dd727b886 | 62 | } |
sedu_santos | 0:5e1dd727b886 | 63 | } |
sedu_santos | 0:5e1dd727b886 | 64 | |
sedu_santos | 0:5e1dd727b886 | 65 | int main() { |
sedu_santos | 0:5e1dd727b886 | 66 | bucle(); |
sedu_santos | 0:5e1dd727b886 | 67 | } |