Ping Pong Robot V1 DIY
Interactive robot to train ping pong - V1.You will never be alone again in this life, if you, at least , have one TTR (Table Tennis Robot)
Components and supplies
Arduino Uno Rev3
EZO™ Oxidation-Reduction Potential Circuit
HC-05 Bluetooth Module
9V 1A Switching Wall Power Supply
Capacitor 100 µF
Breadboard (generic)
Stepper motor driver board A4988
Stepper Motor, Bipolar
Jumper wires (generic)
Servo MG995
DC Motor, Miniature
Dual H-Bridge motor drivers L298
Tools and machines
Multitool, Screwdriver
Drill, Screwdriver
Hot glue gun (generic)
Apps and platforms
TTR v1
Project description
Code
Table Tennis Robot
c_cpp
This code run with the APP developed in APP inventor . The APP is the HMI only use the reset button of the arduino to restart the TTR. check the app here: https://gallery.appinventor.mit.edu/?galleryid=3f3c208a-559f-4aaa-9d3c-0d556000bcbb
1//* Ping Pong Robot Inicio de codigo: version 1- Julio 28-2022 2// * by hernando Bolaños 3// * 4// * 01-08-2022- servo variacion update 5//* Pruebas comunicaciones con APP 6//*15-08-2022 , agregar botones de iz-centro-derecha y random - reciben pero interfieren con servo en oscilación 7//*15-08-2022 -primeras pruebas de sliders- no operan correctamente con parseint() -debido a que estamos trabajando con multitasking 8// 9 10 11////////////////////////////////////////////////////////////////////////////////////////////////////// 12////WARNING: DRAFT CODE////The robot must need adult supervision if are kids playing with it 13///////////////////////////////////////////////////////////////////////////////////////////////////// 14//-- Zona de librerías -- 15// Comunicación serie por software 16#include <SoftwareSerial.h> 17 18 19//-- Zona de definiciones -- 20#define LED 21#define RX 11 22#define TX 10 23const int pinENA = 3; // Motor UP PWM 24const int pinIN1 = 2;// motor UP 25const int pinIN2 = 7; // Motor UP 26const int pinENB = 9;// Motor Down PWM 27const int pinIN3 = 8;//Motor Down 28const int pinIN4 = 13;//Motor Down 29 30const int speed = 250; //velocidad de giro 80% (200/255) 31 32const int speedoff = 0; //velocidad de giro 80% (200/255) 33 34 35long unsigned valor=0; 36 37 38 39//-- Zona de variables globales 40SoftwareSerial BT1(RX,TX); 41 42#define STEP_L 4 // pin STEP de A4988 a pin 4manejando la cara left del cubo 43#define DIR_L 5 // pin DIR de A4988 a pin 5 manejandola cara left del cubo 44#define ENAB 12 // pIN ENAB 45int pasos = 200; // LATERALES pasos 90 grados con motor Hanpose 17HS3430 : 1.8 grados por paso en condicion Full step- Se probo comportamiento en otras configuraciones de pasos 46int tiempo_step = 1800; //es invertido-entre menos es el valor mas rapido va el stepper a variar con HMI - microseconds -segun DS minimo tiempo permitido del step de motores 1 microsegundo 47 48// 49 50#include <Servo.h> 51 52Servo myservo; // create servo object to control a servo 53int servo_angle = 90;//90 54int servo_angle1 = 60;// valor para quedar en posicion central fija 55int variacion = 10;/// con esta variable se puede hacer mas lenta o mas rapida la osiclacion 56unsigned long startTime_servo = 0; 57unsigned long interval_servo = 1100;//t1100 58int latchball = 0; 59int latchservo = 0; 60int latchup = 0; 61int latchdown =0; 62int latchservoizq =0; 63int latchservocen =0; 64int latchservoder =0; 65int latchservorandom =0; 66 67 68 69 70void setup() 71{ 72 73 pinMode(STEP_L, OUTPUT); // pin 4 como salida 74 pinMode(DIR_L, OUTPUT); // pin 5 como salida 75 pinMode(ENAB, OUTPUT); // pIN 12 como salida para driver A4998 76 pinMode(pinIN1, OUTPUT); 77 pinMode(pinIN2, OUTPUT); 78 pinMode(pinENA, OUTPUT);//PWM 79 pinMode(pinIN3, OUTPUT); 80 pinMode(pinIN4, OUTPUT); 81 pinMode(pinENB, OUTPUT);//PWM 82 83 myservo.attach(6); // attaches the servo on pin 6 to the servo object 84 Serial.begin(9600); 85 86myservo.write(90); // usado al inicio para verificar si el servo esta activo 87myservo.write (0); // ir a posicion de inicio en el centro 88myservo.write(60); // usado al inicio para verificar si el servo esta activo 89 90BT1.begin(9600); 91char recibido=0; 92int valor=0; 93int i=0; 94} 95 96 97 98void loop() 99{ 100 101 102unsigned currentTime = millis(); 103char recibido=0; 104 105long unsigned valor=0; 106 107//Si se ha recibido un dato 108 if(BT1.available()==true){ 109 recibido=BT1.read(); //Se carga en la variable recibido 110 Serial.print(recibido,0); 111 //long unsigned valor = BT1.parseInt(); 112 113 114 115 116 // int speed= valor; 117 // Serial.print(valor); 118//Serial.print(speed); 119 120 } 121 122 123// 124 125 126 127if (recibido == 'A'){ 128 129 int latchrobot = 1; 130 131Serial.print("Robot Activado"); 132 133} 134 135 136if (recibido == 'B'){ 137 138 int latchrobot = 0; 139 140Serial.println("Robot Desactivado"); 141 142latchball = 0; //desenganchar 143latchservo =0; //desenganchar 144latchup=0;//desenganchar 145latchdown=0;//desenganchar 146latchservoizq=0;//desenganchar 147latchservoder=0;//desenganchar 148latchservocen=0;//desengachar 149digitalWrite(ENAB, HIGH); //aplicar Modo Disable del driver 150 151 } 152 153 154if (recibido == 'C'){ 155 156latchball= 1;} 157 158 159if (recibido == 'D'){ 160 161latchball= 0;} 162 163 164if (recibido == 'E'){ 165 166latchup= 1;} 167 168 169if (recibido == 'F'){ 170 171latchup= 0;} 172 173if (recibido == 'G'){ 174 175latchdown= 1;} 176 177 178if (recibido == 'H'){ 179 180latchdown= 0;} 181 182 183if (recibido == 'I'){ 184 185latchservo = 1;} 186 187if (recibido == 'J'){ 188 189latchservo = 0;} 190 191 192 193if (recibido == 'Z'){ 194 195latchservoizq = 1;} 196 197if (recibido == 'W'){ 198 199latchservoizq = 0;} 200 201if (recibido == 'T'){ 202 203latchservocen = 1;} 204 205if (recibido == 'X'){ 206 207latchservocen = 0;} 208 209 210if (recibido == 'R'){ 211 212latchservoder = 1;} 213 214if (recibido == 'Y'){ 215 216latchservoder = 0;} 217 218 219 220if (recibido == 'M'){ 221 222latchservorandom = 1;} 223 224if (recibido == 'N'){ 225 226latchservorandom = 0;} 227 228//if (((valor >=20) and (valor <=25)) and(( latchup=1) and (latchdown =1) )){ const int speed=valor; Serial.print(speed);} // tomando trama de los slider up y down 229 230 231if (latchball == 1){ 232 //Serial.print("Dispensar Bolas activado"); 233 alimbolas(); 234 //BT1.write('C'); 235 236 } 237 238 239 240 241if (latchball == 0){ 242 //Serial.println("parar Dispensar Bolas activado"); 243 //BT1.write('D'); 244 245 } 246 247 248if (latchservo == 1){ 249 250 //Serial.print("Oscilar activado"); 251 oscilar(); 252 //BT1.write('I');} 253 254} 255 256if (latchservo == 0){ 257 258 //Serial.println("Oscilar desactivado"); 259 260 //BT1.write('J'); 261 262 } 263 264 265if (latchup == 1){ 266 267 uprob(); 268 269} 270 271if (latchup == 0){ 272 273upoff(); 274//Serial. println("Up desactivado) 275//BTI.write(' '); 276 277} 278 279if (latchdown == 1){ 280 281down(); 282 283} 284 285if (latchdown == 0){ 286 287downoff(); 288//Serial. println("Up desactivado) 289//BTI.write(' '); 290 291} 292 293 294 295if (latchservoizq== 1){ 296 297// 298int i=0; 299 300for (int i = 0; i <= 1; i++) { 301 oscizq(); 302 } 303 304if (i>=1){ 305latchservoizq == 0;} 306//Serial.println("Fijo en izquierda Activado"); 307//BTI.write(' '); 308 309} 310 311if (latchservoizq == 0){ 312 313//oscizqoff(); 314//Serial. println(" Fijo en ziquiera desActivado") 315//BTI.write(' '); 316 317} 318 319if (latchservocen== 1){ 320 321int i=0; 322 323 324for (int i = 0; i <= 1; i++) { 325 osccentro(); 326 } 327 328if (i>=1){ 329latchservocen == 0;} 330//Serial. println("Fijo en centro Activado"); 331//BTI.write(' '); 332 333 334} 335 336if (latchservocen == 0){ 337 338//osccentrooff(); 339//Serial. println(" Fijo en centro desaActivado) 340//BTI.write(' '); 341 342} 343 344 345if (latchservoder== 1){ 346 347int i=0; 348 349for (int i = 0; i <= 1; i++) { 350 oscder(); 351 } 352if (i>=1){ 353latchservoder == 0;} 354//Serial.println("Fijo en centro Activado"); 355//BTI.write(' '); 356 357} 358 359if (latchservoder == 0){ 360 361 //oscderoff(); 362 363//Serial. println(" Fijo en centro desaActivado) 364//BTI.write(' '); 365 366} 367} 368 369 370void alimbolas()// alimenta bolas giando en sentido NCW 371 372{ 373 374 375 Serial.print("Bolas NCW,"); 376 377 digitalWrite(ENAB, LOW); // Modo Enable del driver 378 379 digitalWrite(DIR_L, HIGH); // giro en sentido NCW 380 for (int i = 0; i < pasos; i++) { // 381 digitalWrite(STEP_L, HIGH); // nivel alto 382 delayMicroseconds(tiempo_step); // por x mseg 383 delayMicroseconds(tiempo_step); 384 digitalWrite(STEP_L, LOW); // nivel bajo 385 delayMicroseconds(tiempo_step); // por x mseg 386 delayMicroseconds(tiempo_step); 387 } 388 389} 390 391 392 393 394 395void oscilar() //activa modo oscilar en el robot - izquierda a derecha 396{ 397 unsigned currentTime = millis(); 398 399 400 //Serial.println("servo"); 401 if (currentTime - startTime_servo >= interval_servo){ 402 startTime_servo=currentTime; 403 myservo.write(servo_angle); 404 servo_angle = servo_angle + variacion; 405 if (servo_angle == 160) servo_angle = 30; 406 //Serial.println(servo_angle); 407 } 408} 409 410 411 412void oscizq() //activa modo oscilar en el robot - izquierda a derecha 413{ 414 unsigned currentTime = millis(); 415 416 417 //Serial.println("servo"); 418 if (currentTime - startTime_servo >= interval_servo){ 419 startTime_servo=currentTime; 420 myservo.write(160); 421 422 //Serial.println(servo_angle); 423 } 424} 425 426 427 428void oscder() //activa modo oscilar en el robot - izquierda a derecha 429{ 430 unsigned currentTime = millis(); 431 432 433 //Serial.println("servo"); 434 if (currentTime - startTime_servo >= interval_servo){ 435 startTime_servo=currentTime; 436 myservo.write(0); 437 438 } 439} 440 441 442void osccentro() //activa modo oscilar en el robot - izquierda a derecha 443{ 444 unsigned currentTime = millis(); 445 446 447 //Serial.println("servo"); 448 if (currentTime - startTime_servo >= interval_servo){ 449 startTime_servo=currentTime; 450 myservo.write(90); 451 452 } 453} 454 455 456 457 458 459void oscizqoff() //activa modo oscilar en el robot - izquierda a derecha 460{ 461 unsigned currentTime = millis(); 462 463 464 //Serial.println("servo"); 465 if (currentTime - startTime_servo >= interval_servo){ 466 startTime_servo=currentTime; 467 myservo.write(90); 468 469 //Serial.println(servo_angle); 470 } 471} 472 473 474 475void oscderoff() //activa modo oscilar en el robot - izquierda a derecha 476{ 477 unsigned currentTime = millis(); 478 479 480 //Serial.println("servo"); 481 if (currentTime - startTime_servo >= interval_servo){ 482 startTime_servo=currentTime; 483 myservo.write(90); 484 485 } 486} 487 488 489void osccentrooff() //activa modo oscilar en el robot - izquierda a derecha 490{ 491 unsigned currentTime = millis(); 492 493 494 //Serial.println("servo"); 495 if (currentTime - startTime_servo >= interval_servo){ 496 startTime_servo=currentTime; 497 myservo.write(90); 498 499 } 500} 501 502 503 504void uprob() // 505{ 506 //Serial.println("corriendo rutina UP,"); 507 // Serial.println(speed); 508 digitalWrite(pinIN1, HIGH); 509 digitalWrite(pinIN2, LOW); 510 analogWrite(pinENA, speed); 511 //delay(1000); 512} 513 514 515void upoff() 516{ //Serial.println("corriendo rutina upoff"); 517 518 digitalWrite(pinIN1, LOW); 519 digitalWrite(pinIN2, LOW); 520 analogWrite(pinENA, speedoff); 521 //delay(1000); 522} 523 524 525 526void down() 527{ 528 digitalWrite(pinIN3, LOW); 529 digitalWrite(pinIN4, HIGH); 530 analogWrite(pinENB, speed); 531 //delay(1000); 532} 533 534 535void downoff() 536{ 537 digitalWrite(pinIN3, LOW); 538 digitalWrite(pinIN4, LOW); 539 analogWrite(pinENB, speedoff); 540 //delay(1000); 541} 542 543 544
Downloadable files
Schematics
Schematics
Schematics
Schematics
Comments
Only logged in users can leave comments