2021年2月14日日曜日

ArduinoChallenge AriiTankWiredNano


















//AriiTankWiredNano
//by Kim Grossa

#include <CytronMotorDriver.h>
#include <Servo.h>

//Maker Drive
CytronMD motor1(PWM_PWM, 3, 9);   // PWM 1A = Pin 3, PWM 1B = Pin 9.
CytronMD motor2(PWM_PWM, 10, 11); // PWM 2A = Pin 10, PWM 2B = Pin 11.

//servo
int ServoSpeed=1500;
Servo myServo;
int pos;

int Firepin=7;

//Joystick1
int x1Pin=A0;
int y1Pin=A1;
int sw1Pin=2;
int x1Pos;
int y1Pos;
int sw1Pos;
//Joystick2
int x2Pin=A2;
int y2Pin=A3;
int sw2Pin=4;
int x2Pos;
int y2Pos;
int sw2Pos;

void setup()
{
  //DC motor
  motor1.setSpeed(0);  
  motor2.setSpeed(0);

  //servo 360degree
  myServo.attach(8,1000,2000);

  pinMode(Firepin, OUTPUT);
  
  //Joystick1
  pinMode(x1Pin,INPUT);
  pinMode(y1Pin,INPUT);
  pinMode(sw1Pin,INPUT_PULLUP);
  //Joystick2
  pinMode(x2Pin,INPUT);
  pinMode(y2Pin,INPUT);
  pinMode(sw2Pin,INPUT_PULLUP);


  Serial.begin(9600);



}

void loop() 
{
  //Joystick1
  x1Pos=analogRead(x1Pin);
  y1Pos=analogRead(y1Pin);
  sw1Pos=digitalRead(sw1Pin);
  //Joystick2
  x2Pos=analogRead(x2Pin);
  y2Pos=analogRead(y2Pin);
  sw2Pos=digitalRead(sw2Pin);

  if(x1Pos<=600 || x1Pos>=400 )
  {
    center1();//Joystick1 center
  }

  if(x2Pos<=600 || x2Pos>=400 )
  {
    center2();//Joystick2 center
  }
  
  if(x1Pos>800)
  {
    turnLeft1();//Joystick1 left
  }

  if(x1Pos<200)
  {
    turnRight1();//Joystck1 right
  }

  if(y1Pos>800)
  {
    goForward1();//Joystick1 up
  }

  if(y1Pos<200)
  {
    goBack1();//Joystick1 down
  }

  if(x2Pos<200)
  {
    turnLeft2();//Joystick2 left
  }

  if(x2Pos>800)
  {
    turnRight2();//Joystci2 right
  }

  if(y2Pos<200)
  {
    goForward2();//Joystick2 up
  }

  if(y2Pos>800)
  {
    goBack2();//Joystick2 down
  }

  if(sw1Pos==0)
  {
    sw1ON();//Joystck1 sw on
  }

  if(sw2Pos==0)
  {
    sw2ON();//Joystick2 sw on
  }

}

  void center1()
  {
    //Serial.println("I am center1");
    motor1.setSpeed(0);
    motor2.setSpeed(0);  
  }
  
  void center2()
  {
    //Serial.println("I am center2"); 
    myServo.write(90);     
  }
   
  void turnLeft1()
  {    
    //Serial.println("I am turnLeft1");
    motor1.setSpeed(-255);  
    motor2.setSpeed(0);
  }

  void turnRight1()
  {
    //Serial.println("I am turnRight1");
    motor1.setSpeed(0);  
    motor2.setSpeed(-255);
  }

   void goForward1()
  {
    //Serial.println("I am goForward1");
    motor1.setSpeed(-255);  
    motor2.setSpeed(-255);
  }

  void goBack1()
  {
    //Serial.println("I am goBack1");
    motor1.setSpeed(255);  
    motor2.setSpeed(255); 
  }

   void turnLeft2()
  {
    //Serial.println("I am turnLeft2");
    myServo.write(180);
  }

  void turnRight2()
  {
    //Serial.println("I am turnRight2");
    myServo.write(0); 
  }

   void goForward2()
  {
    //Serial.println("I am goForward2");
  }

  void goBack2()
  {
    //Serial.println("I am goBack2");
  }

    void sw1ON()
  {
    //Serial.println("I am sw1ON");
  }

  void sw2ON()
  {
    //Serial.println("I am sw2ON");
    digitalWrite(Firepin,HIGH);
    delay(1000);
    digitalWrite(Firepin,LOW);
  }


 

0 件のコメント:

コメントを投稿