C-Day demo NXShield-Mx &Arduino adk

C-Day arduino競賽組的demo。使用Arduino adk、NXShield-Mx和樂高NXT馬達以及零件,有循跡並夾取放下方塊的功能

先來看影片吧





Arduino程式碼

//BBS1右馬達
//BBS2左馬達
//BAS1夾馬達
#include 
#include 
#include 
NXShield          nxshield;
NXTLight    light1;
NXTLight    light2;
int lv1 = 0;//1
int lv2 = 0;//2光感量測值
int lmv1 = 550;//1光感中間值
int lmv2 = 550;//2
int ld = 50;
int rd = 50;
void setup()
{
  Serial.begin(115200); 
  nxshield.init( SH_HardwareI2C );
  light1.init( &nxshield, SH_BAS1 );
  light2.init( &nxshield, SH_BAS2 );
  light1.setReflected();
  light2.setReflected();
  Serial.println ("Press GO button to continue");
  nxshield.waitForButtonPress(BTN_GO);
  nxshield.bank_a.motorReset();
  nxshield.bank_b.motorReset();
  lv1 = light1.readRaw();
  lv2 = light2.readRaw();
   nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward,75);
  delay(500);
    nxshield.bank_a.motorStop(SH_Motor_Both, SH_Next_Action_Float);
   while(lmv1>lv1 || lmv2>lv2)
  {
    lv1 = light1.readRaw();
    lv2 = light2.readRaw();
    if(lmv1
    { 
       nxshield.bank_b.motorStop(SH_Motor_2, SH_Next_Action_BrakeHold); 
       nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,75);
    }
    else if(lmv2
    { 
       nxshield.bank_b.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold); 
       nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,75);
    }
    else
    {
       nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,50);
       nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,50);
    }
  }
  Stop();
  delay(100);
  nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,50);
  nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,50);
  delay(1000);
  nxshield.bank_b.motorStop(SH_Motor_2, SH_Next_Action_BrakeHold);
  nxshield.bank_b.motorRunDegrees(SH_Motor_1, SH_Direction_Reverse,75,100,SH_Completion_Wait_For,SH_Next_Action_BrakeHold);
  nxshield.bank_b.motorStop(SH_Motor_Both, SH_Next_Action_BrakeHold);
  delay(200);
  nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,100);
  delay(1000);
  nxshield.bank_b.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold);
  nxshield.bank_b.motorRunDegrees(SH_Motor_1, SH_Direction_Forward,75,75,SH_Completion_Wait_For,SH_Next_Action_BrakeHold);
  Stop();
  delay(200);
  lv1 = light1.readRaw();
  lv2 = light2.readRaw();
  while(lmv1>lv1 || lmv2>lv2)
  {
    lv1 = light1.readRaw();
    lv2 = light2.readRaw();
    if(lmv1
    { 
       nxshield.bank_b.motorStop(SH_Motor_2, SH_Next_Action_BrakeHold); 
       nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,75);
    }
    else if(lmv2
    { 
       nxshield.bank_b.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold); 
       nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,75);
    }
    else
    {
       nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,50);
       nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,50);
    }
  }
  Stop();
  delay(100);
  nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,75);
  nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,75);
  delay(200);
  Stop();
  delay(100);
  lv1 = light1.readRaw();
    lv2 = light2.readRaw();
  while(lmv1>lv1 || lmv2>lv2)
  {
    lv1 = light1.readRaw();
    lv2 = light2.readRaw();
    if(lmv1
    { 
       nxshield.bank_b.motorStop(SH_Motor_2, SH_Next_Action_BrakeHold); 
       nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,100);
    }
    else if(lmv2
    { 
       nxshield.bank_b.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold); 
       nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,100);
    }
    else
    {
       nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,50);
       nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,50);
    }
  }
  Stop();
  delay(100);
  nxshield.bank_b.motorRunUnlimited(SH_Motor_1, SH_Direction_Reverse,75);
  nxshield.bank_b.motorRunUnlimited(SH_Motor_2, SH_Direction_Reverse,75);
  delay(500);
  Stop();
  delay(100);
  nxshield.bank_a.motorRunUnlimited(SH_Motor_1, SH_Direction_Forward,75);
  delay(200);
  nxshield.bank_a.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold);
  Stop();
  delay(100);
}
void loop()
{
}//空

About CAVEDU 阿吉 - 雜工 (2520 Articles)
CAVEDU 教育團隊 打雜
Contact: Website

Leave a comment

Your email address will not be published.


*