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