correccion spi

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Thu Nov 22 10:38:28 2018 +0000
Revision:
7:5f1c8385cf8e
Parent:
6:4c9e8118c303
Child:
8:9c2dff610de9
final final este si es...;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiuk 0:2dabb9782f24 1 // Read from I2C slave at address 0x62
jiuk 0:2dabb9782f24 2
jiuk 0:2dabb9782f24 3 #include "mbed.h"
jiuk 5:f8ca9f670abe 4 #include "Ticker.h"
jiuk 0:2dabb9782f24 5 #include "MPU6050.h"
jiuk 0:2dabb9782f24 6
jiuk 0:2dabb9782f24 7 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:2dabb9782f24 8 MPU6050 Wire(PB_9 , PB_8 );
jiuk 0:2dabb9782f24 9
jiuk 0:2dabb9782f24 10 SPI deviceM(PB_15, PB_14, PB_13);
jiuk 0:2dabb9782f24 11 DigitalOut ssel (PB_12);
jiuk 0:2dabb9782f24 12 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
jiuk 0:2dabb9782f24 13 //DigitalOut ssel (PB_9); //Chip Select para el controlador.
jiuk 0:2dabb9782f24 14 //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:2dabb9782f24 15 Serial com_tar(PC_10,PC_11); //master f446R
jiuk 0:2dabb9782f24 16 //Serial com_tar(PA_15,PB_7); //slave f411R //habilitar la comunicacion serial a traves del puerto usb.
jiuk 2:484928b9a5b5 17 Ticker timer;
jiuk 4:3426b2472f99 18 DigitalIn pulsador(PH_1);
jiuk 4:3426b2472f99 19 AnalogIn in1(PC_2);
jiuk 4:3426b2472f99 20 AnalogIn in2(PC_3);
jiuk 0:2dabb9782f24 21
jiuk 0:2dabb9782f24 22 #define VEL 200 //Velocidad de actualizacion de dato en el controlador.
jiuk 0:2dabb9782f24 23 #define MIN 1
jiuk 0:2dabb9782f24 24 #define MAX 8
jiuk 0:2dabb9782f24 25 #define MINC 128
jiuk 0:2dabb9782f24 26 #define MAXC 1
jiuk 0:2dabb9782f24 27 #define DEL 0.1
jiuk 3:81dc55b0a8d7 28 #define GIRO 2
jiuk 3:81dc55b0a8d7 29 #define G_DIAG 2
jiuk 0:2dabb9782f24 30
jiuk 2:484928b9a5b5 31 int columna=1,manzanas=0;
jiuk 0:2dabb9782f24 32 int fila=1, fil=1, colum=1;
jiuk 4:3426b2472f99 33 bool _boton=0;
jiuk 2:484928b9a5b5 34 void blink();
jiuk 4:3426b2472f99 35 void boton();
jiuk 7:5f1c8385cf8e 36 void sendSPI(uint8_t,uint8_t);
jiuk 7:5f1c8385cf8e 37
jiuk 7:5f1c8385cf8e 38
jiuk 7:5f1c8385cf8e 39 //--------------------------------------------------
jiuk 7:5f1c8385cf8e 40 //-------------------CARA_FELIZ------------------------
jiuk 7:5f1c8385cf8e 41 //--------------------------------------------------
jiuk 7:5f1c8385cf8e 42 void cara_feliz()
jiuk 7:5f1c8385cf8e 43 {
jiuk 7:5f1c8385cf8e 44 sendSPI(0x02,0b01100110);
jiuk 7:5f1c8385cf8e 45 wait(0.2);
jiuk 7:5f1c8385cf8e 46 sendSPI(0x03,0b01100110);
jiuk 7:5f1c8385cf8e 47 wait(0.2);
jiuk 7:5f1c8385cf8e 48 sendSPI(0x06,0b01000010);
jiuk 7:5f1c8385cf8e 49 wait(0.2);
jiuk 7:5f1c8385cf8e 50 sendSPI(0x07,0b00111100);
jiuk 7:5f1c8385cf8e 51 wait(0.2);
jiuk 7:5f1c8385cf8e 52 for(int i=0;i<4;i++)
jiuk 7:5f1c8385cf8e 53 {
jiuk 7:5f1c8385cf8e 54 sendSPI(0x06,0x00);
jiuk 7:5f1c8385cf8e 55 sendSPI(0x07,0b00111100);
jiuk 7:5f1c8385cf8e 56 wait(0.2);
jiuk 7:5f1c8385cf8e 57 sendSPI(0x07,0b00111100);
jiuk 7:5f1c8385cf8e 58 wait(0.2);
jiuk 7:5f1c8385cf8e 59 sendSPI(0x06,0b01000010);
jiuk 7:5f1c8385cf8e 60 wait(0.2);
jiuk 7:5f1c8385cf8e 61 }
jiuk 7:5f1c8385cf8e 62 }
jiuk 7:5f1c8385cf8e 63
jiuk 7:5f1c8385cf8e 64 //--------------------------------------------------
jiuk 7:5f1c8385cf8e 65 //-------------------CARA_TRISTE------------------------
jiuk 7:5f1c8385cf8e 66 //--------------------------------------------------
jiuk 7:5f1c8385cf8e 67 void cara_triste()
jiuk 7:5f1c8385cf8e 68 {
jiuk 7:5f1c8385cf8e 69 sendSPI(0x02,0b01100110);
jiuk 7:5f1c8385cf8e 70 wait(0.2);
jiuk 7:5f1c8385cf8e 71 sendSPI(0x03,0b01100110);
jiuk 7:5f1c8385cf8e 72 wait(0.2);
jiuk 7:5f1c8385cf8e 73 sendSPI(0x06,0b00111100);
jiuk 7:5f1c8385cf8e 74 wait(0.2);
jiuk 7:5f1c8385cf8e 75 sendSPI(0x07,0b01000010);
jiuk 7:5f1c8385cf8e 76 wait(0.2);
jiuk 7:5f1c8385cf8e 77 for(int i=0;i<4;i++)
jiuk 7:5f1c8385cf8e 78 {
jiuk 7:5f1c8385cf8e 79 sendSPI(0x07,0x00);
jiuk 7:5f1c8385cf8e 80 sendSPI(0x06,0b00111100);
jiuk 7:5f1c8385cf8e 81 wait(0.2);
jiuk 7:5f1c8385cf8e 82 sendSPI(0x06,0b00111100);
jiuk 7:5f1c8385cf8e 83 wait(0.2);
jiuk 7:5f1c8385cf8e 84 sendSPI(0x07,0b01000010);
jiuk 7:5f1c8385cf8e 85 wait(0.2);
jiuk 7:5f1c8385cf8e 86 }
jiuk 7:5f1c8385cf8e 87 }
jiuk 0:2dabb9782f24 88
jiuk 2:484928b9a5b5 89 //--------------------------------------------------
jiuk 2:484928b9a5b5 90 //-------------------TICKER------------------------
jiuk 2:484928b9a5b5 91 //--------------------------------------------------
jiuk 5:f8ca9f670abe 92 void attime()
jiuk 5:f8ca9f670abe 93 {
jiuk 2:484928b9a5b5 94 blink();
jiuk 5:f8ca9f670abe 95 boton();
jiuk 7:5f1c8385cf8e 96 if (com_tar.readable())
jiuk 6:4c9e8118c303 97 {
jiuk 7:5f1c8385cf8e 98 cara_triste();
jiuk 6:4c9e8118c303 99 command.printf("Button pressed %b\n", _boton);
jiuk 6:4c9e8118c303 100 }
jiuk 5:f8ca9f670abe 101 };
jiuk 4:3426b2472f99 102
jiuk 4:3426b2472f99 103 //--------------------------------------------------
jiuk 4:3426b2472f99 104 //-------------------BOTON------------------------
jiuk 4:3426b2472f99 105 //--------------------------------------------------
jiuk 4:3426b2472f99 106 void boton()
jiuk 4:3426b2472f99 107 {
jiuk 5:f8ca9f670abe 108 if (pulsador == 0)
jiuk 4:3426b2472f99 109 {
jiuk 5:f8ca9f670abe 110 command.printf("Button pressed %b\n", _boton);
jiuk 5:f8ca9f670abe 111 _boton=!_boton;
jiuk 4:3426b2472f99 112 }
jiuk 4:3426b2472f99 113 }
jiuk 5:f8ca9f670abe 114
jiuk 2:484928b9a5b5 115 //--------------------------------------------------
jiuk 2:484928b9a5b5 116 //-------------------SPI------------------------
jiuk 2:484928b9a5b5 117 //--------------------------------------------------
jiuk 0:2dabb9782f24 118 void sendSPI(uint8_t d1, uint8_t d2)
jiuk 0:2dabb9782f24 119 {
jiuk 0:2dabb9782f24 120 deviceM.unlock();
jiuk 0:2dabb9782f24 121 ssel=0;
jiuk 0:2dabb9782f24 122 deviceM.write(d1);
jiuk 0:2dabb9782f24 123 deviceM.write(d2);
jiuk 0:2dabb9782f24 124 ssel=1;
jiuk 0:2dabb9782f24 125 deviceM.lock();
jiuk 2:484928b9a5b5 126 };
jiuk 2:484928b9a5b5 127
jiuk 0:2dabb9782f24 128
jiuk 2:484928b9a5b5 129 //--------------------------------------------------
jiuk 2:484928b9a5b5 130 //-------------------TEST------------------------
jiuk 2:484928b9a5b5 131 //--------------------------------------------------
jiuk 0:2dabb9782f24 132 void test() //test
jiuk 0:2dabb9782f24 133 {
jiuk 0:2dabb9782f24 134 sendSPI(0x09,0); //no decodificacion
jiuk 4:3426b2472f99 135 sendSPI(0x0A,0x9); //intensidad
jiuk 0:2dabb9782f24 136 sendSPI(0x0B,0x07); //usa 7 leds
jiuk 0:2dabb9782f24 137 sendSPI(0x0C,1); //no apaga
jiuk 0:2dabb9782f24 138 sendSPI(0x0F,0); //operacion normal
jiuk 0:2dabb9782f24 139 }
jiuk 0:2dabb9782f24 140
jiuk 2:484928b9a5b5 141
jiuk 2:484928b9a5b5 142 //--------------------------------------------------
jiuk 2:484928b9a5b5 143 //-------------------BORRAR------------------------
jiuk 2:484928b9a5b5 144 //--------------------------------------------------
jiuk 0:2dabb9782f24 145 void borrar() //borrar toda la matriz;
jiuk 0:2dabb9782f24 146 {
jiuk 0:2dabb9782f24 147 int i;
jiuk 0:2dabb9782f24 148 for(i=0;i<=8;i++)
jiuk 0:2dabb9782f24 149 {
jiuk 0:2dabb9782f24 150 sendSPI(i,0);
jiuk 0:2dabb9782f24 151 }
jiuk 0:2dabb9782f24 152 }
jiuk 0:2dabb9782f24 153
jiuk 2:484928b9a5b5 154
jiuk 2:484928b9a5b5 155 //--------------------------------------------------
jiuk 2:484928b9a5b5 156 //-------------------GENERAR_PUNTO------------------------
jiuk 2:484928b9a5b5 157 //--------------------------------------------------
jiuk 0:2dabb9782f24 158 void generar_punto()
jiuk 0:2dabb9782f24 159 {
jiuk 2:484928b9a5b5 160 int col[8]={1,2,4,8,16,32,64,128};
jiuk 0:2dabb9782f24 161 //int fil=0;
jiuk 0:2dabb9782f24 162 borrar();
jiuk 2:484928b9a5b5 163 int _fil=fil;
jiuk 2:484928b9a5b5 164 fil= rand() % 7+1;
jiuk 0:2dabb9782f24 165 int y= rand() % 7+1;
jiuk 0:2dabb9782f24 166 command.printf("\n\n Fila: %d",fil);
jiuk 0:2dabb9782f24 167 command.printf("\n Columna:%d",col[y]);
jiuk 0:2dabb9782f24 168 //int time=0;
jiuk 0:2dabb9782f24 169 //while(time<4)
jiuk 0:2dabb9782f24 170 // {
jiuk 2:484928b9a5b5 171 if(_fil==fil)
jiuk 2:484928b9a5b5 172 colum=colum+col[y];
jiuk 2:484928b9a5b5 173 colum=col[y];
jiuk 0:2dabb9782f24 174 if(fil==fila){
jiuk 0:2dabb9782f24 175 int guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 176 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 177 }
jiuk 0:2dabb9782f24 178 else
jiuk 0:2dabb9782f24 179 sendSPI(fil,col[y]);
jiuk 2:484928b9a5b5 180
jiuk 0:2dabb9782f24 181 // wait_ms(VEL);
jiuk 0:2dabb9782f24 182 // sendSPI(fil,0);
jiuk 0:2dabb9782f24 183 // wait_ms(VEL);
jiuk 0:2dabb9782f24 184 // time++;
jiuk 0:2dabb9782f24 185 // }
jiuk 0:2dabb9782f24 186
jiuk 0:2dabb9782f24 187 }
jiuk 0:2dabb9782f24 188
jiuk 2:484928b9a5b5 189 //--------------------------------------------------
jiuk 2:484928b9a5b5 190 //-------------------BLINK------------------------
jiuk 2:484928b9a5b5 191 //--------------------------------------------------
jiuk 2:484928b9a5b5 192 void blink(){
jiuk 2:484928b9a5b5 193 sendSPI(fil,0);
jiuk 2:484928b9a5b5 194 if(fil==fila)
jiuk 2:484928b9a5b5 195 sendSPI(fila,columna);
jiuk 5:f8ca9f670abe 196 wait(0.05);
jiuk 2:484928b9a5b5 197 }
jiuk 2:484928b9a5b5 198
jiuk 2:484928b9a5b5 199 //--------------------------------------------------
jiuk 2:484928b9a5b5 200 //-------------------REVISAR_MOV------------------------
jiuk 2:484928b9a5b5 201 //--------------------------------------------------
jiuk 0:2dabb9782f24 202 void revisar_mov(uint8_t correr){
jiuk 0:2dabb9782f24 203 switch (correr){
jiuk 3:81dc55b0a8d7 204
jiuk 3:81dc55b0a8d7 205 case 2:
jiuk 3:81dc55b0a8d7 206 columna=columna<<1;
jiuk 3:81dc55b0a8d7 207 fila--;
jiuk 3:81dc55b0a8d7 208 if (columna>MINC)
jiuk 3:81dc55b0a8d7 209 columna=MAXC;
jiuk 3:81dc55b0a8d7 210 if (fila<MIN){
jiuk 3:81dc55b0a8d7 211 fila=MAX;
jiuk 3:81dc55b0a8d7 212 sendSPI(MIN,0);
jiuk 3:81dc55b0a8d7 213 }
jiuk 3:81dc55b0a8d7 214 sendSPI(fila+1,0);
jiuk 3:81dc55b0a8d7 215 break;
jiuk 3:81dc55b0a8d7 216
jiuk 3:81dc55b0a8d7 217 case 4:
jiuk 3:81dc55b0a8d7 218 columna=columna<<1;
jiuk 3:81dc55b0a8d7 219 fila++;
jiuk 3:81dc55b0a8d7 220 if (fila>MAX){
jiuk 3:81dc55b0a8d7 221 fila=MIN;
jiuk 3:81dc55b0a8d7 222 sendSPI(MAX,0);
jiuk 3:81dc55b0a8d7 223 }
jiuk 3:81dc55b0a8d7 224 if (columna>MINC)
jiuk 3:81dc55b0a8d7 225 columna=MAXC;
jiuk 3:81dc55b0a8d7 226 sendSPI(fila-1,0);
jiuk 3:81dc55b0a8d7 227 break;
jiuk 3:81dc55b0a8d7 228
jiuk 3:81dc55b0a8d7 229 case 1:
jiuk 3:81dc55b0a8d7 230 columna=columna>>1;
jiuk 3:81dc55b0a8d7 231 fila--;
jiuk 3:81dc55b0a8d7 232 if (columna<MAXC)
jiuk 3:81dc55b0a8d7 233 columna=MINC;
jiuk 3:81dc55b0a8d7 234 if (fila<MIN){
jiuk 3:81dc55b0a8d7 235 fila=MAX;
jiuk 3:81dc55b0a8d7 236 sendSPI(MIN,0);
jiuk 3:81dc55b0a8d7 237 }
jiuk 3:81dc55b0a8d7 238 sendSPI(fila+1,0);
jiuk 3:81dc55b0a8d7 239 //command.printf("\n case1 Col:%d",columna);
jiuk 3:81dc55b0a8d7 240 //command.printf("\n FIL:%d",fila);
jiuk 3:81dc55b0a8d7 241 break;
jiuk 3:81dc55b0a8d7 242
jiuk 3:81dc55b0a8d7 243 case 3:
jiuk 3:81dc55b0a8d7 244 columna=columna>>1;
jiuk 3:81dc55b0a8d7 245 fila++;
jiuk 3:81dc55b0a8d7 246 if (columna<MAXC)
jiuk 3:81dc55b0a8d7 247 columna=MINC;
jiuk 3:81dc55b0a8d7 248 if (fila>MAX){
jiuk 3:81dc55b0a8d7 249 fila=MIN;
jiuk 3:81dc55b0a8d7 250 sendSPI(MAX,0);
jiuk 3:81dc55b0a8d7 251 }
jiuk 3:81dc55b0a8d7 252 sendSPI(fila-1,0);
jiuk 3:81dc55b0a8d7 253 //command.printf("\n case3 Col:%d",columna);
jiuk 3:81dc55b0a8d7 254 //command.printf("\n FIL:%d",fila);
jiuk 3:81dc55b0a8d7 255 break;
jiuk 3:81dc55b0a8d7 256
jiuk 0:2dabb9782f24 257 case 28:
jiuk 0:2dabb9782f24 258 columna=columna<<1;
jiuk 0:2dabb9782f24 259 if (columna>MINC)
jiuk 0:2dabb9782f24 260 columna=MAXC;
jiuk 0:2dabb9782f24 261 break;
jiuk 0:2dabb9782f24 262
jiuk 0:2dabb9782f24 263 case 29:
jiuk 0:2dabb9782f24 264 columna=columna>>1;
jiuk 0:2dabb9782f24 265 if (columna<MAXC)
jiuk 0:2dabb9782f24 266 columna=MINC;
jiuk 0:2dabb9782f24 267 break;
jiuk 0:2dabb9782f24 268
jiuk 0:2dabb9782f24 269 case 30:
jiuk 0:2dabb9782f24 270 fila--;
jiuk 3:81dc55b0a8d7 271 if (fila<MIN){
jiuk 3:81dc55b0a8d7 272 fila=MAX;
jiuk 3:81dc55b0a8d7 273 sendSPI(MIN,0);
jiuk 3:81dc55b0a8d7 274 }
jiuk 0:2dabb9782f24 275 sendSPI(fila+1,0);
jiuk 0:2dabb9782f24 276 break;
jiuk 0:2dabb9782f24 277
jiuk 0:2dabb9782f24 278 case 31:
jiuk 0:2dabb9782f24 279 fila++;
jiuk 3:81dc55b0a8d7 280 if (fila>MAX){
jiuk 3:81dc55b0a8d7 281 fila=MIN;
jiuk 3:81dc55b0a8d7 282 sendSPI(MAX,0);
jiuk 3:81dc55b0a8d7 283 }
jiuk 0:2dabb9782f24 284 sendSPI(fila-1,0);
jiuk 0:2dabb9782f24 285 break;
jiuk 1:efcead3fb5eb 286
jiuk 1:efcead3fb5eb 287 case 0:
jiuk 1:efcead3fb5eb 288 break;
jiuk 0:2dabb9782f24 289 }
jiuk 0:2dabb9782f24 290 }
jiuk 2:484928b9a5b5 291
jiuk 3:81dc55b0a8d7 292 //--------------------------------------------------
jiuk 3:81dc55b0a8d7 293 //-------------------VELOCIDAD-----------------------
jiuk 3:81dc55b0a8d7 294 //--------------------------------------------------
jiuk 3:81dc55b0a8d7 295 int velocidad(float _acc[3])
jiuk 3:81dc55b0a8d7 296 {
jiuk 5:f8ca9f670abe 297 int vel,x=1,y=2; //
jiuk 3:81dc55b0a8d7 298 int acc[3];
jiuk 4:3426b2472f99 299 acc[0]=(int)_acc[0];
jiuk 4:3426b2472f99 300 acc[1]=(int)_acc[1];
jiuk 3:81dc55b0a8d7 301 if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO))
jiuk 3:81dc55b0a8d7 302 {
jiuk 4:3426b2472f99 303 if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x)))
jiuk 4:3426b2472f99 304 {
jiuk 4:3426b2472f99 305 vel=300;
jiuk 4:3426b2472f99 306 if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y)))
jiuk 4:3426b2472f99 307 vel=100;
jiuk 4:3426b2472f99 308 }
jiuk 4:3426b2472f99 309 else
jiuk 4:3426b2472f99 310 vel=500;
jiuk 3:81dc55b0a8d7 311 }
jiuk 3:81dc55b0a8d7 312 return vel;
jiuk 3:81dc55b0a8d7 313 }
jiuk 2:484928b9a5b5 314
jiuk 2:484928b9a5b5 315 //--------------------------------------------------
jiuk 2:484928b9a5b5 316 //-------------------BUSCADOR------------------------
jiuk 2:484928b9a5b5 317 //--------------------------------------------------
jiuk 0:2dabb9782f24 318 void buscador()
jiuk 0:2dabb9782f24 319 {
jiuk 0:2dabb9782f24 320 uint8_t correr;
jiuk 4:3426b2472f99 321 int y=0,x=0,acc[3],v;
jiuk 0:2dabb9782f24 322 //float gyro[3]; Wire.getGyro(gyro);
jiuk 4:3426b2472f99 323 float _acc[3];
jiuk 4:3426b2472f99 324 //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
jiuk 4:3426b2472f99 325 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
jiuk 5:f8ca9f670abe 326 if(_boton==1)
jiuk 4:3426b2472f99 327 {
jiuk 4:3426b2472f99 328 x=((int)(in1.read()*-650))+13;
jiuk 4:3426b2472f99 329 y=((int)(in2.read()*-650))+13;
jiuk 4:3426b2472f99 330 acc[0]=x;
jiuk 4:3426b2472f99 331 acc[1]=y;
jiuk 6:4c9e8118c303 332 //command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y);
jiuk 4:3426b2472f99 333 }
jiuk 4:3426b2472f99 334 else
jiuk 4:3426b2472f99 335 {
jiuk 4:3426b2472f99 336 Wire.getAccelero(_acc);
jiuk 4:3426b2472f99 337 acc[0]=(int)_acc[0];
jiuk 4:3426b2472f99 338 acc[1]=(int)_acc[1];
jiuk 6:4c9e8118c303 339 //command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]);
jiuk 4:3426b2472f99 340 }
jiuk 4:3426b2472f99 341
jiuk 6:4c9e8118c303 342 //command.printf("Velocidad %d \n", v);
jiuk 3:81dc55b0a8d7 343 if(acc[0]>GIRO) //eje x derecha
jiuk 3:81dc55b0a8d7 344 {
jiuk 3:81dc55b0a8d7 345 if(acc[1]>G_DIAG) //eje y arriba
jiuk 3:81dc55b0a8d7 346 correr=1;
jiuk 3:81dc55b0a8d7 347 else if(acc[1]<-G_DIAG)//eje y abajo
jiuk 3:81dc55b0a8d7 348 correr=2;
jiuk 3:81dc55b0a8d7 349 else
jiuk 3:81dc55b0a8d7 350 correr=30;
jiuk 3:81dc55b0a8d7 351 }
jiuk 3:81dc55b0a8d7 352 else if(acc[0]<-GIRO) //eje x izquierda
jiuk 3:81dc55b0a8d7 353 {
jiuk 3:81dc55b0a8d7 354 if(acc[1]>G_DIAG) //eje y arriba
jiuk 3:81dc55b0a8d7 355 correr=3;
jiuk 3:81dc55b0a8d7 356 else if(acc[1]<-G_DIAG)//eje y abajo
jiuk 3:81dc55b0a8d7 357 correr=4;
jiuk 3:81dc55b0a8d7 358 else
jiuk 3:81dc55b0a8d7 359 correr=31;
jiuk 3:81dc55b0a8d7 360 }
jiuk 3:81dc55b0a8d7 361 else if(acc[1]>GIRO) //eje y arriba
jiuk 3:81dc55b0a8d7 362 correr=29;
jiuk 3:81dc55b0a8d7 363 else if(acc[1]<-GIRO) //eje y abajo
jiuk 3:81dc55b0a8d7 364 correr=28;
jiuk 3:81dc55b0a8d7 365 else
jiuk 3:81dc55b0a8d7 366 correr=0; //
jiuk 4:3426b2472f99 367 revisar_mov(correr);
jiuk 4:3426b2472f99 368 v=velocidad(_acc);
jiuk 4:3426b2472f99 369 wait_ms(v);
jiuk 0:2dabb9782f24 370 //wait (DEL);
jiuk 2:484928b9a5b5 371 //command.printf("\n correr:\n %d",correr);
jiuk 0:2dabb9782f24 372 }
jiuk 0:2dabb9782f24 373
jiuk 2:484928b9a5b5 374
jiuk 2:484928b9a5b5 375 //--------------------------------------------------
jiuk 2:484928b9a5b5 376 //-------------------COMER------------------------
jiuk 2:484928b9a5b5 377 //--------------------------------------------------
jiuk 0:2dabb9782f24 378 int comer ()
jiuk 0:2dabb9782f24 379 {
jiuk 0:2dabb9782f24 380 int x=0;
jiuk 2:484928b9a5b5 381 if((fil==fila)&&((colum & columna) != 0))
jiuk 2:484928b9a5b5 382 {
jiuk 2:484928b9a5b5 383 manzanas++;
jiuk 2:484928b9a5b5 384 sendSPI(fila,columna);
jiuk 6:4c9e8118c303 385 int guarda_linea=columna+colum;
jiuk 6:4c9e8118c303 386 sendSPI(fila,guarda_linea);
jiuk 6:4c9e8118c303 387 generar_punto();
jiuk 6:4c9e8118c303 388 sendSPI(fil,colum);
jiuk 6:4c9e8118c303 389 if(manzanas==4)
jiuk 2:484928b9a5b5 390 {
jiuk 6:4c9e8118c303 391 com_tar.putc(1);
jiuk 6:4c9e8118c303 392 command.printf("Enviado");
jiuk 7:5f1c8385cf8e 393 cara_feliz();
jiuk 2:484928b9a5b5 394 }
jiuk 0:2dabb9782f24 395 x=1;
jiuk 2:484928b9a5b5 396 }
jiuk 0:2dabb9782f24 397 return x;
jiuk 2:484928b9a5b5 398 }
jiuk 2:484928b9a5b5 399
jiuk 0:2dabb9782f24 400
jiuk 2:484928b9a5b5 401 //--------------------------------------------------
jiuk 2:484928b9a5b5 402 //-------------------VALIDAR_CONTRINCANTE------------------------
jiuk 2:484928b9a5b5 403 //--------------------------------------------------
jiuk 0:2dabb9782f24 404 int validar_contrincante(){
jiuk 0:2dabb9782f24 405 int x=com_tar.getc();
jiuk 0:2dabb9782f24 406 return x;
jiuk 0:2dabb9782f24 407 }
jiuk 0:2dabb9782f24 408
jiuk 4:3426b2472f99 409
jiuk 4:3426b2472f99 410 //--------------------------------------------------
jiuk 4:3426b2472f99 411 //-------------------MAIN------------------------
jiuk 4:3426b2472f99 412 //--------------------------------------------------
jiuk 0:2dabb9782f24 413 int main ()
jiuk 0:2dabb9782f24 414 {
jiuk 0:2dabb9782f24 415 int guarda_linea,x=0;
jiuk 5:f8ca9f670abe 416 timer.attach(&attime, 0.1);
jiuk 0:2dabb9782f24 417 test();
jiuk 0:2dabb9782f24 418 borrar();
jiuk 0:2dabb9782f24 419 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 420 generar_punto();
jiuk 0:2dabb9782f24 421 while(1)
jiuk 0:2dabb9782f24 422 {
jiuk 0:2dabb9782f24 423 buscador();
jiuk 0:2dabb9782f24 424 //validar_contrincante();
jiuk 0:2dabb9782f24 425 x=comer();
jiuk 2:484928b9a5b5 426 //command.printf("\n comer:\n %d",x);
jiuk 0:2dabb9782f24 427 if (x==0)
jiuk 0:2dabb9782f24 428 {
jiuk 0:2dabb9782f24 429 if(fil==fila){
jiuk 0:2dabb9782f24 430 guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 431 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 432 }
jiuk 0:2dabb9782f24 433 else
jiuk 0:2dabb9782f24 434 {
jiuk 0:2dabb9782f24 435 guarda_linea=columna;
jiuk 0:2dabb9782f24 436 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 437 sendSPI(fil,colum);
jiuk 0:2dabb9782f24 438 }
jiuk 0:2dabb9782f24 439 }
jiuk 0:2dabb9782f24 440 else{
jiuk 0:2dabb9782f24 441 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 442 }
jiuk 0:2dabb9782f24 443 //generar_punto();
jiuk 0:2dabb9782f24 444 }
jiuk 0:2dabb9782f24 445 }