Brazo PIR - Piedra, papel o tigera

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.1 kB
5
Indexable
Never
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver servos = Adafruit_PWMServoDriver();

int pos0=102; //ancho de pulso en cueta para la posicion 0°
int pos180=512; //ancho de pulso en cueta para la posicion 180°

void setservo(uint8_t n_servo,int angulo){
  int duty;
  duty=map (angulo,0,180, pos0, pos180);
  servos.setPWM(n_servo,0,duty);
  }
int Pulgar= 5;
int Indice= 6;
int Mayor= 10;
int AnularM= 9;
int Pulgar2= 11;
int s=0;
#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <Dabble.h>

 
int pir =7; //pin de sensor inflarojo pasivo 


void setup() {
  servos.begin();
  servos.setPWMFreq(50); 
      
  pinMode(pir, INPUT);
  
  Serial.begin(9600);     
  Dabble.begin(9600);      //Enter baudrate of your bluetooth.Connect bluetooth on Bluetooth port present on evive.
}


void loop() {
  Dabble.processInput();             //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
  Serial.println("KeyPressed: ");
  Switch();
  if(s==1){
    Gamepad();//start
    }
  if(s==2){
    PIRV();//select
    }
}


void PIRV(){
  int n = random(1,31);
  if(digitalRead(pir)==HIGH){
    pulgararriba();
    delay(2000);
    cuenta();
    delay(500);
    if (n<=10){ //piedra
     piedra(); }
    if(n>10&&n<=20){//papel
     papel(); }
   if(n>20&&n<=30){
    tijera();
    }
   if(n==31){
      pistola();
      }
  delay(1000);
  }
  }

  
  void Gamepad(){
      if (GamePad.isUpPressed())
  {
     pulgararriba();
  }

  if (GamePad.isDownPressed())
  { 
    Serial.print("Down");
  }

  if (GamePad.isLeftPressed())
  { comiste();
    Serial.print("Left");
  }

  if (GamePad.isRightPressed())
  {
    Serial.print("Right");
  }

  if (GamePad.isSquarePressed())
  {    Serial.print("PAPEL");
    papel();
  }

  if (GamePad.isCirclePressed())
  {   Serial.print("PIEDRA");
    piedra();
  }

  if (GamePad.isCrossPressed())
  {     Serial.print("TIJERA");
    tijera();
   
  }

  if (GamePad.isTrianglePressed())
  {    Serial.print("PISTOLA");
    pistola();
  }
  if(GamePad.isDownPressed()&&GamePad.isRightPressed()){
    prohibido();
  }
}


void piedra(){
    setservo(Pulgar,40);
    setservo(Indice,180);
    setservo(Mayor,180);
    setservo(AnularM,180);  
    setservo(Pulgar2,90);    
  }
  void papel(){
    setservo(Pulgar,150);
    setservo(Indice,70);
    setservo(Mayor,40);
    setservo(AnularM,30);  
    setservo(Pulgar2,120);  
  }
  void tijera(){
    setservo(Pulgar,40);
    setservo(Indice,70);
    setservo(Mayor,40);
    setservo(AnularM,180);  
    setservo(Pulgar2,90);  
  }
    void pistola(){
    setservo(Pulgar,150);
    setservo(Indice,70);
    setservo(Mayor,160);
    setservo(AnularM,160);  
    setservo(Pulgar2,120);  
  }

  
   void pulgararriba(){
    setservo(Pulgar,150);
    setservo(Indice,180);
    setservo(Mayor,180);
    setservo(AnularM,180);  
    setservo(Pulgar2,120);  
  }
   void cuenta(){
    setservo(Pulgar,90);
    setservo(Indice,130);
    setservo(Mayor,90);
    setservo(AnularM,90);  
    setservo(Pulgar2,120);  
    delay(250);
    setservo(Pulgar,110);
    setservo(Indice,100);
    setservo(Mayor,70);
    setservo(AnularM,70);  
    setservo(Pulgar2,120);  
    delay (250);
    
    setservo(Pulgar,90);
    setservo(Indice,130);
    setservo(Mayor,90);
    setservo(AnularM,90);  
    setservo(Pulgar2,120);  
    delay(250);
    setservo(Pulgar,110);
    setservo(Indice,100);
    setservo(Mayor,70);
    setservo(AnularM,70);  
    setservo(Pulgar2,120);  
  }

  void comiste(){
    setservo(Pulgar,60);
    setservo(Indice,180);
    setservo(Mayor,40);
    setservo(AnularM,30);  
    setservo(Pulgar2,70);  
    }
    
void prohibido(){
    setservo(Pulgar,40);
    setservo(Indice,180);
    setservo(Mayor,20);
    setservo(AnularM,180);  
    setservo(Pulgar2,90); 
}

    
  void Switch(){
    if (GamePad.isStartPressed())
  { 
    s=1;
    Serial.print("START");
  }
  if (GamePad.isSelectPressed())
  { 
    s=2;
    Serial.print("Select");
  }
  }