Juan Moreno
/
FinalTest
FinalTest Proyect
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "BH1750.h" 00003 //#include "PIR.h" 00004 #include<string> 00005 00006 // ================================================================== 00007 // Definición Puertos 00008 // ================================================================== 00009 00010 BH1750 lightSensor(D14, D15); // Sensor (SDA,SCL) 00011 Serial pc(SERIAL_TX, SERIAL_RX); // Inicializacion Serial PC 00012 //PIR pir_obj(A0); // Objeto PIR 00013 DigitalIn pir_obj(A0); // PIR 00014 //DigitalIn automatico(A3); // Entrada para modo Auto 00015 DigitalOut led_PIR(LED1); // LED Prueba PIR 00016 DigitalOut led_auto(LED2); // LED Prueba LED Detección Automatico(ON) y Manual(OFF) 00017 00018 // ================================================================== 00019 // Instanciando SPIO y SerialPC 00020 // ================================================================== 00021 00022 SPI spi(PB_5, PB_4, PB_3); // mosi, miso, sclk 00023 DigitalOut arduinoMega(PA_4); //Slave Selecter: Arduino Mega 00024 PwmOut led(PB_11); 00025 // ================================================================== 00026 // Variables Bluetooth 00027 // ================================================================== 00028 00029 Serial blue(D1,D0); //SERIAL_TX, SERIAL_RX 00030 float power(float base,int potencia); 00031 int func_pwm(); 00032 string estado; 00033 // ================================================================== 00034 // Funcion de potenciacion porque JD no pudo usar la por defecto.... 00035 // ================================================================== 00036 00037 float power(float base,int potencia) 00038 { 00039 float resultado=0; 00040 if(potencia>1) { 00041 for(int i=1; i<potencia; i++) { 00042 resultado=resultado+base*base; 00043 } 00044 } else if(potencia==1) { 00045 resultado=base; 00046 } else { 00047 resultado=1; 00048 } 00049 return resultado; 00050 } 00051 00052 00053 00054 int main() 00055 { 00056 // ================================================================== 00057 // Inicializacion Bluetooth 00058 // ================================================================== 00059 00060 blue.baud(9600); 00061 pc.baud(9600); 00062 int i=0,cont=0; 00063 float data_sent=0; 00064 float unid=0; 00065 float dec=0; 00066 float cent=0; 00067 float umil=0; 00068 char valor_blue=NULL; 00069 float new_data=0; 00070 00071 00072 00073 // ================================================================== 00074 // Niveles de Iluminación 00075 // ================================================================== 00076 float period=0.001; 00077 float duty=1.0f; 00078 float deseado=500; 00079 00080 // ================================================================== 00081 // Variables Envío de Datos 00082 // ================================================================== 00083 00084 float dataFromSensor; 00085 int contador=0; // 00086 bool contador_juancho=true; 00087 bool sel_tactil=true; 00088 bool sel_auto=true; 00089 float dataFromSlave=0; 00090 int valor_en=0; 00091 int contDe256=0; 00092 int Print=0; 00093 float Slave=0; 00094 float float_newdata; 00095 00096 // ================================================================== 00097 // SPIO Config 00098 // ================================================================== 00099 00100 spi.format(16,3); // Setup: bit data, SPImode3 00101 spi.frequency(1000000); // 00102 00103 // ================================================================== 00104 // BH1750 Config 00105 // ================================================================== 00106 lightSensor.init(); 00107 00108 while(1) { 00109 00110 // ================================================================== 00111 // Activación del PIR 00112 // ================================================================== 00113 if(pir_obj.read()==1) { 00114 //LEDs de Prueba 00115 led_PIR=pir_obj; 00116 led_auto=sel_auto; 00117 //Lectura de Datos 00118 dataFromSensor=lightSensor.readIntesity(); 00119 valor_en=dataFromSensor; 00120 contDe256=dataFromSensor/256; 00121 00122 // Envío de Datos a Arduino 00123 00124 if(sel_tactil==true) { 00125 00126 arduinoMega = 0; // Select device 00127 00128 if(contador_juancho) { 00129 Slave = spi.write(valor_en)/256; 00130 contador_juancho=false; 00131 } else { 00132 Slave = spi.write(contDe256)/256; 00133 contador_juancho=true; 00134 } 00135 00136 arduinoMega = 1; // Deselect device 00137 00138 00139 wait_ms(180); 00140 00141 dataFromSlave=Slave; 00142 wait_ms(10); 00143 if(dataFromSlave==19) { 00144 contador=1; 00145 } 00146 if(dataFromSlave==20) { 00147 contador=2; 00148 } 00149 00150 if(contador==1) { 00151 Print=dataFromSlave; 00152 float_newdata=Print; 00153 duty=float_newdata/100; 00154 sel_auto=false; 00155 00156 } 00157 if(contador==2) { 00158 00159 Print=dataFromSlave*10; 00160 float_newdata=Print; 00161 deseado=float_newdata; 00162 sel_auto=true; 00163 } 00164 00165 wait_ms(30); 00166 pc.printf("=============MODO PANTALLA TACTIL ==============\r\n"); 00167 pc.printf("Arduino returns %d\r\n", Print); 00168 pc.printf("Light intensity: %d Lux\r\n", valor_en); 00169 pc.printf("Duty: %.2f %\r\n", duty); 00170 wait_ms(500); 00171 00172 } 00173 00174 00175 00176 if (blue.readable()) { 00177 sel_tactil=false; 00178 if(sel_auto) { 00179 00180 valor_blue=blue.getc(); 00181 if(valor_blue=='M') { 00182 sel_auto=false; 00183 } 00184 //Interrupción de recepción de datos del bluetooth 00185 if (valor_blue=='A') { //Envío de datos desde el bluetooth 00186 unid=0; 00187 dec=0; 00188 cent=0; 00189 data_sent=0; 00190 umil=0; 00191 for ( i=0; i<5; i++) { 00192 data_sent=blue.getc(); 00193 if(data_sent=='b') { //Indica finalización anticipada del envío, mensajes de menos de 3 char 00194 break; 00195 } else { 00196 data_sent=data_sent-'0'; 00197 if (i==1) { 00198 umil=data_sent; 00199 } 00200 if (i==2) { 00201 cent=data_sent; 00202 } 00203 if (i==3) { 00204 dec=data_sent; 00205 } 00206 if(i==4) { 00207 unid=data_sent; 00208 } 00209 } //cierre else para armar la iluminación enviada desde bluetooth 00210 cont=i; 00211 }// CIERRE FOR 00212 /// Lectura del dato desde bluetooth 00213 umil=umil*power(10,cont-1); 00214 cent=cent*power(10,cont-2); 00215 dec=dec*power(10,cont-3); 00216 unid=unid*power(10,cont-4); 00217 new_data=unid+dec+cent+umil; 00218 deseado=new_data; //+++++++++++++++++++++FALTA ESTO+** 00219 }//if Valor A 00220 blue.printf("%.0f |",duty*100); 00221 blue.printf("%.0f |",dataFromSensor); 00222 pc.printf("=============MODO AUTO BLUETOOTH ==============\r\n"); 00223 pc.printf("Light intensity Sensor: %.4f Lux\r\n", dataFromSensor); 00224 pc.printf("Contador: %d |\r\n",cont); 00225 pc.printf("Deseado: %.0f |\r\n",deseado); 00226 // pc.printf("Multiplicador = %d \r\n", contDe256); 00227 // pc.printf("Valor enviado: %d Lux\r\n", valor_en); 00228 pc.printf("PWM Duty Cycle: %.2f %\r\n", duty*100); 00229 pc.printf("letra: \r\n"); 00230 pc.putc( valor_blue); 00231 pc.printf("\r\n"); 00232 wait_ms(500); 00233 00234 } else { //Contro Bluetooth (Manual) sel_auto==false 00235 //LEDs de Prueba 00236 led_PIR=pir_obj; 00237 led_auto=0; 00238 //Lectura de Datos 00239 dataFromSensor= lightSensor.readIntesity(); 00240 valor_blue=blue.getc(); 00241 if(valor_blue=='U') { //Cambio a automático 00242 sel_auto=true; 00243 } 00244 //Interrupción de recepción de datos del bluetooth 00245 if (valor_blue=='B') { //Envío de datos desde el bluetooth 00246 unid=0; 00247 dec=0; 00248 cent=0; 00249 data_sent=0; 00250 umil=0; 00251 for ( i=0; i<5; i++) { 00252 data_sent=blue.getc(); 00253 if(data_sent=='b') { //Indica finalización anticipada del envío, mensajes de menos de 3 char 00254 break; 00255 } else { 00256 data_sent=data_sent-'0'; 00257 if (i==1) { 00258 umil=data_sent; 00259 } 00260 if (i==2) { 00261 cent=data_sent; 00262 } 00263 if (i==3) { 00264 dec=data_sent; 00265 } 00266 if(i==4) { 00267 unid=data_sent; 00268 } 00269 } //cierre else para armar la iluminación enviada desde bluetooth 00270 cont=i; 00271 }// CIERRE FOR 00272 /// Lectura del dato desde bluetooth 00273 umil=umil*power(10,cont-1); 00274 cent=cent*power(10,cont-2); 00275 dec=dec*power(10,cont-3); 00276 unid=unid*power(10,cont-4); 00277 new_data=unid+dec+cent+umil; 00278 duty=new_data/100; //+++++++++++++++++++++FALTA ESTO+** 00279 00280 } //if (valorBlue==A) 00281 blue.printf("%.0f |",dataFromSensor); 00282 pc.printf("=============MODO MANUAL BLUETOOTH ============== \r\n"); 00283 pc.printf("Duty: %.0f |",duty*100); 00284 pc.printf("Contador: %d \r\n",cont); 00285 pc.printf("Iluminacion: %.4f \r\n",dataFromSensor); 00286 pc.printf("Letra: \r\n"); 00287 pc.putc(valor_blue); 00288 pc.printf("\r\n"); 00289 wait_ms(500); 00290 }//Else contro manual bluetooth 00291 } 00292 if(blue.readable()==false) { // If blue.readable() Indica si hay bluetooth conectado 00293 sel_tactil=true; 00294 } 00295 00296 // ================================================================== 00297 // PWM 00298 // ================================================================== 00299 00300 if(sel_auto) { 00301 00302 00303 if (dataFromSensor<deseado-10) { 00304 //duty=(1-(dataFromSensor/deseado)); 00305 duty=duty+0.01; 00306 } 00307 if(dataFromSensor>deseado+10) { 00308 duty=duty-0.01; 00309 } else { 00310 //duty=0; 00311 duty=duty; 00312 } 00313 } 00314 00315 led.period(period); 00316 led.write(duty); 00317 00318 00319 } else { //if PIR 00320 duty=0; 00321 led_PIR=0; 00322 led_auto=0; 00323 led.write(duty); 00324 }//else PIR 00325 int pir_int=pir_obj; 00326 pc.printf("PIR: %d %\r\n", pir_int); 00327 }//while 00328 }//main
Generated on Mon Jul 18 2022 00:53:07 by 1.7.2