2021年4月10日土曜日

Arduino Challenges MarukaTruckcrane Wired Nano








 //MarukaTruckcraneWiredNano

//by Kim Grossa

//Two DC 5V motors

//One Servo motor

//One Stepper

//Two Joysticks


#include <CytronMotorDriver.h>

#include <VarSpeedServo.h>

#include <Stepper.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.


//Joystick

int x1Pos,y1Pos,x2Pos,y2Pos;


//Servo

VarSpeedServo myservo;

int servoVal;


//Stepper

Stepper mystepper(32,4,6,5,7);

int stepperSpeed;


void setup()

{

  //DC motor

  motor1.setSpeed(0);  

  motor2.setSpeed(0);

  

  //Joystick

  pinMode(14,INPUT);

  pinMode(15,INPUT);

  pinMode(16,INPUT);

  pinMode(17,INPUT);


  //Servo

  myservo.attach(8);

  //servoVal=90;

 

  //Stepeer

  mystepper.setSpeed(100);

  Serial.begin(9600);

}


void loop() 

{

  x1Pos=analogRead(14);

  y1Pos=analogRead(15);

  x2Pos=analogRead(16);

  y2Pos=analogRead(17);

  

  if((x2Pos<=300)&&(x2Pos>=200))

  {

    digitalWrite(4,LOW);

    digitalWrite(5,LOW);

    digitalWrite(6,LOW);

    digitalWrite(7,LOW);

  }


  if (x2Pos <100)

    {     

      mystepper.step(85);

    }

  if (x2Pos >400)

    {   

      mystepper.step(-85);

    }

 

  if((x1Pos<=500)&&(x1Pos>=50))

  {

    TruckStop();

  }


  if((y1Pos<=500)&&(y1Pos>=50))

  {

    TruckStop();

  }

 

  if (x1Pos >500)

    {

    TruckForward();

   }


  if (x1Pos <50)

    {

      TruckBack();

    }


  if (y1Pos >500)

    {

      TruckRight();

    }


  if (y1Pos <50)

    {

      TruckLeft();

    }

    

  servoVal=map(y2Pos,0,1023,0,180);

  myservo.write(servoVal,10);

  

  //Serial.print("x1Pos=   ");

  //Serial.print(x1Pos);

  //Serial.println();


  //Serial.print("y1Pos=   ");

  //Serial.print(y1Pos);

  //Serial.println();


  //Serial.print("x2Pos=   ");

  //Serial.print(x2Pos);

  //Serial.println();


  //Serial.print("y2Pos=   ");

 // Serial.print(y2Pos);

  //Serial.println();  

}


  void TruckStop()

  {

    motor1.setSpeed(0);

    motor2.setSpeed(0);  

  }

    

  void TruckLeft()

  {    

    motor1.setSpeed(-255);  

    motor2.setSpeed(0);                                                                                                                                                                                                                                                                                                                                                                                      

  }


  void TruckRight()

  {

    motor1.setSpeed(0);  

    motor2.setSpeed(255);

  }


   void TruckForward()

  {

    motor1.setSpeed(-255);  

    motor2.setSpeed(255);

  }


  void TruckBack()

  {

    motor1.setSpeed(255);  

    motor2.setSpeed(-255); 

  }


  


   

  

 

0 件のコメント:

コメントを投稿