Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INSTITUTO POLITECNICO NACIONAL
- ESCUELA SUPERIOR DE INGENIERIA MECANICA Y ELECTRICA
- UNIDAD ZACATENCO
- ING. EN CONTROL Y AUTOMATIZACION
- ASIGNATURA: INTERFASES Y MICROCONTROLADORES
- M.C LUIS ARTURO BENITEZ VELAZQUEZ
- GRUPO 7AM1
- AUTORES:
- ROSALES HERNANDEZ JOSE EDUARDO
- RUIZ ESTUDILLO MARIA FERNANDA
- ***********************************************************************************
- #include "LedControl.h"
- #include "LiquidCrystal.h"
- LedControl lc=LedControl(8,7,6,1); //Declaraci'on del objeto de control de la matriz
- int POT=A5; //Control del jugador 1
- int POT2=A4; //Control del jugador 2
- int POT_IN; //Variable analogica del jugador 1
- int POT_IN2; //Variable analogica del jugador 2
- int x; //Coord x de la pelota
- int y; //Coord y de la pelota
- int x_a; //Coord x anterior
- int y_a; //Coord y anterior
- int x_a1;
- int angulo; //Angulo de movimiento
- int j1x; //Posicion central de la barra 1
- int j2x; //Posicion central de la barra 2
- int j1; //Marcador del jugador 1
- int j2; //Marcador del jugador 2
- int boton=9; //Pin del boton de inicio
- int enclave=0; //Variable de enclave
- int inicio=0; //Variable de inicio
- int punt=5; //Puntaje ganador
- //LCD
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- int player1=j1;
- int player2=j2;
- void setup()
- {
- Serial.begin(9600);
- lc.shutdown(0,false); //Encendido del Driver
- lc.setIntensity(0,10); //Intensidad de brillo de la matriz
- lcd.begin(16, 2);
- lcd.setCursor(0,0);
- lcd.print("IPN ESIME ZAC");
- lcd.setCursor(0,1);
- lcd.print("P I N G P O N G");
- TransPort(5);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("ROSALES H. LALO");
- lcd.setCursor(0,1);
- lcd.print("RUIZ E. MAFER");
- TransPort(5);
- lcd.clear();
- pinMode(POT, INPUT);
- pinMode(POT2,INPUT);
- pinMode(boton,INPUT);
- // for(int i=0;i<=7;i++) //Apagado de toda la matriz
- // for(int j=0;j<=7;j++)
- // lc.setLed(0,i,j,false);
- Serial.begin(9600);
- }
- void loop()
- {
- while(enclave==0) //Estado de Inicio
- {
- inicio=digitalRead(boton);
- lc.setColumn(0,7,B00000000); //Carita Feliz
- lc.setColumn(0,6,B00100100);
- lc.setColumn(0,5,B00100100);
- lc.setColumn(0,4,B00000000);
- lc.setColumn(0,3,B01000010);
- lc.setColumn(0,2,B01000010);
- lc.setColumn(0,1,B00111100);
- lc.setColumn(0,0,B00000000);
- // LCD: Presione Start
- lcd.setCursor(0,0);
- lcd.print("P I N G P O N G");
- lcd.setCursor(0,1);
- lcd.print(" Push START ");
- if(inicio==1) //Inicio del Juego
- {
- lcd.clear();
- enclave++;
- for(int i=0;i<=7;i++) //Apagado de toda la Matriz
- for(int j=0;j<=7;j++)
- lc.setLed(0,i,j,false);
- reset();
- }
- }
- while(enclave>=1) //Estado de Juego
- {
- Pelota();
- lcdwhileplay();
- }
- }
- void Jugador1()
- {
- POT_IN=analogRead(POT);
- j1x=map(POT_IN,0,1025,1,7);
- lc.setLed(0,0,j1x+1,true);
- lc.setLed(0,0,j1x,true);
- lc.setLed(0,0,j1x-1,true);
- for(int i=0;i<=7;i++)
- if(i!=j1x&&i!=j1x-1&&i!=j1x+1)
- lc.setLed(0,0,i,false);
- }
- void Jugador2()
- {
- POT_IN2=analogRead(POT2);
- j2x=map(POT_IN2,0,1025,1,7);
- lc.setLed(0,7,j2x+1,true);
- lc.setLed(0,7,j2x,true);
- lc.setLed(0,7,j2x-1,true);
- for(int i=0;i<=7;i++)
- if(i!=j2x&&i!=j2x-1&&i!=j2x+1)
- lc.setLed(0,7,i,false);
- }
- void Pelota()
- {
- CalcAngulo();
- lc.setLed(0,x,y,true);
- lc.setLed(0,x_a,y_a,false);
- delay(75);
- Jugador1();
- Jugador2();
- delay(75);
- Jugador1();
- Jugador2();
- delay(75);
- x_a=x;
- y_a=y;
- }
- void CalcAngulo()
- {
- if(x==6)
- {
- if((angulo==225||angulo==315)&&(y==j2x+1||y==j2x-1))
- angulo=90;
- else if(angulo==225&&y==j2x)
- angulo=135;
- else if(angulo==315&&y==j2x)
- angulo=45;
- else if(angulo==270&&(y==j2x+1||y==j2x||y==j2x-1))
- angulo=random(0,2) == 0 ? 45:135;
- else if(angulo==225&&y==j2x+2)
- angulo=45;
- else if(angulo==315&&y==j2x-2)
- angulo=135;
- else if(y!=j2x+1||y!=j2x||y!=j2x-1)
- {
- if(angulo==225)
- lc.setLed(0,x+1,y-1,true);
- if(angulo==270)
- lc.setLed(0,x+1,y,true);
- if(angulo==315)
- lc.setLed(0,x+1,y+1,true);
- //lc.setLed(0,x,y,false);
- j1=j1+1;
- delay(500);
- if(j1>=punt)
- {
- enclave=0;
- winner(1);
- Trans();
- delay (3000);
- return;
- }
- x=1;
- y=random(1,6);
- angulo=random(0,2) == 0 ? 225 : 315;
- }
- }
- else if(x==1)
- {
- if((angulo==45||angulo==135)&&(y==j1x+1||y==j1x-1))
- angulo=270;
- else if(angulo==135&&y==j1x)
- angulo=225;
- else if(angulo==45&&y==j1x)
- angulo=315;
- else if(angulo==90&&(y==j1x+1||y==j1x||y==j1x-1))
- angulo=random(0,2) == 0 ? 225:315;
- else if(angulo==135&&y==j1x+2)
- angulo=315;
- else if(angulo==45&&y==j1x-2)
- angulo=225;
- else if(y!=j1x+1||y!=j1x||y!=j1x-1)
- {
- if(angulo==45)
- lc.setLed(0,x-1,y+1,true);
- if(angulo==90)
- lc.setLed(0,x-1,y,true);
- if(angulo==135)
- lc.setLed(0,x-1,y-1,true);
- //lc.setLed(0,x,y,false);
- j2=j2+1;
- Serial.println(j2);
- delay(500);
- if(j2>=punt)
- {
- enclave=0;
- winner(2);
- Trans();
- delay (3000);
- return;
- }
- x=6;
- y=random(1,6);
- angulo=random(0,2) == 0 ? 45 : 135;
- }
- }
- if(y==7)
- {
- if(angulo==45)
- angulo=135;
- else if(angulo==315)
- angulo=225;
- }
- else if(y==0)
- {
- if(angulo==135)
- angulo=45;
- else if(angulo==225)
- angulo=315;
- }
- MovimientoPelota();
- }
- void MovimientoPelota()
- {
- if(angulo==45)
- {
- x=x-1;
- y=y+1;
- }
- else if(angulo==90)
- {
- x=x-1;
- }
- else if(angulo==135)
- {
- x=x-1;
- y=y-1;
- }
- else if(angulo==225)
- {
- x=x+1;
- y=y-1;
- }
- else if(angulo==270)
- {
- x=x+1;
- }
- else if(angulo==315)
- {
- x=x+1;
- y=y+1;
- }
- }
- void reset()
- {
- j1=0;
- j2=0;
- x=random(3,4);
- y=random(2,6);
- angulo=random(0,1) == 0 ? 90 : 270;
- }
- void Trans()
- {
- for(int i=0;i<=7;i++)
- for(int j=0;j<=7;j++)
- {
- lc.setLed(0,i,j,true);
- delay(20);
- }
- for(int i=0;i<=7;i++)
- for(int j=0;j<=7;j++)
- {
- lc.setLed(0,i,j,false);
- delay(20);
- }
- }
- void lcdwhileplay()
- {
- lcd.setCursor(0,0);
- lcd.print("PLAYER1");
- lcd.setCursor(4,1);
- lcd.print(j1);
- lcd.setCursor(8,0);
- lcd.print("PLAYER2");
- lcd.setCursor(12,1);
- lcd.print(j2);
- }
- void winner(int p)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("W I N N E R");
- lcd.setCursor(0,1);
- if(p==1)
- {
- lcd.print("P L A Y E R 1");
- }
- else if(p==2)
- {
- lcd.print("P L A Y E R 2");
- }
- }
- void TransPort(int a)
- {
- int j=0;
- a=a*2;
- while(j<=a){
- for(int i=0;i<=6;i=i+2){
- lc.setColumn(0,i,B10101010);
- lc.setColumn(0,i+1,B01010101);
- }
- delay(250);
- for(int i=0;i<=6;i=i+2){
- lc.setColumn(0,i+1,B10101010);
- lc.setColumn(0,i,B01010101);
- }
- delay(250);
- j++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment