svnno****@sourc*****
svnno****@sourc*****
2011年 7月 9日 (土) 11:28:17 JST
Revision: 12 http://sourceforge.jp/projects/andy/svn/view?view=rev&revision=12 Author: yishii Date: 2011-07-09 11:28:17 +0900 (Sat, 09 Jul 2011) Log Message: ----------- Test sketch for AndyShield with Arduino Uno/Duemilanove Added Paths: ----------- trunk/AndyShieldTest/ trunk/AndyShieldTest/AndyShieldTest.pde -------------- next part -------------- Added: trunk/AndyShieldTest/AndyShieldTest.pde =================================================================== --- trunk/AndyShieldTest/AndyShieldTest.pde (rev 0) +++ trunk/AndyShieldTest/AndyShieldTest.pde 2011-07-09 02:28:17 UTC (rev 12) @@ -0,0 +1,142 @@ +// +// Andy Shield(Android Kobe original shield) test +// +// Coded by Yasuhiro ISHII +// + +#include <Servo.h> + +// ---------------------------------------------- + +#define PIN_MOTOR1_APWM 4 +#define PIN_MOTOR1_BPWM 5 +#define PIN_MOTOR2_APWM 6 +#define PIN_MOTOR2_BPWM 7 + +#define PIN_MOTOR1_AIN1 A0 +#define PIN_MOTOR1_AIN2 A1 +#define PIN_MOTOR1_BIN1 A3 +#define PIN_MOTOR1_BIN2 A2 + +#define PIN_MOTOR2_AIN1 13 +#define PIN_MOTOR2_AIN2 12 +#define PIN_MOTOR2_BIN1 11 +#define PIN_MOTOR2_BIN2 10 + +#define PIN_RCSERVO1_CNT 9 +#define PIN_RCSERVO2_CNT 8 + +// ------------------------------------------------ + +#define MOTORTYPE_MOTOR1_1 0 +#define MOTORTYPE_MOTOR1_2 1 +#define MOTORTYPE_MOTOR2_1 2 // not work +#define MOTORTYPE_MOTOR2_2 3 // not work + +#define MOTORACTION_MOTOR_STOP 0 +#define MOTORACTION_MOTOR_FORWARD 1 +#define MOTORACTION_MOTOR_REVERSE 2 +#define MOTORACTION_MOTOR_BRAKE 3 + +Servo servomotor_1; +Servo servomotor_2; + +void setup() +{ + ///////////////////////////////////////////////// + // GPIO SETUP + ///////////////////////////////////////////////// + + // pin io direction setup + pinMode(PIN_MOTOR1_AIN1,OUTPUT); + pinMode(PIN_MOTOR1_AIN2,OUTPUT); + pinMode(PIN_MOTOR1_BIN1,OUTPUT); + pinMode(PIN_MOTOR1_BIN2,OUTPUT); + pinMode(PIN_MOTOR2_AIN1,OUTPUT); + pinMode(PIN_MOTOR2_AIN2,OUTPUT); + pinMode(PIN_MOTOR2_BIN1,OUTPUT); + pinMode(PIN_MOTOR2_BIN2,OUTPUT); + +#if 0 + pinMode(PIN_MOTOR1_APWM,OUTPUT); + pinMode(PIN_MOTOR1_BPWM,OUTPUT); + pinMode(PIN_MOTOR2_APWM,OUTPUT); + pinMode(PIN_MOTOR2_BPWM,OUTPUT); +#endif + + pinMode(PIN_RCSERVO1_CNT,OUTPUT); + pinMode(PIN_RCSERVO2_CNT,OUTPUT); + + // pin output value setup + + digitalWrite(PIN_MOTOR1_AIN1,LOW); + digitalWrite(PIN_MOTOR1_AIN2,LOW); + digitalWrite(PIN_MOTOR1_BIN1,LOW); + digitalWrite(PIN_MOTOR1_BIN2,LOW); + digitalWrite(PIN_MOTOR2_AIN1,LOW); + digitalWrite(PIN_MOTOR2_AIN2,LOW); + digitalWrite(PIN_MOTOR2_BIN1,LOW); + digitalWrite(PIN_MOTOR2_BIN2,LOW); + + ///////////////////////////////////////////////// + // MISC SETUP + ///////////////////////////////////////////////// + + // initialize peripherals +#if 0 + analogWrite(PIN_MOTOR1_APWM,0); + analogWrite(PIN_MOTOR1_BPWM,0); + analogWrite(PIN_MOTOR2_APWM,0); + analogWrite(PIN_MOTOR2_BPWM,0); +#endif + + analogWrite(PIN_MOTOR1_APWM,150); + analogWrite(PIN_MOTOR1_BPWM,150); + analogWrite(PIN_MOTOR2_APWM,80); + analogWrite(PIN_MOTOR2_BPWM,80); + delay(50); + + // initialize servo motor + servomotor_1.attach(PIN_RCSERVO1_CNT,800,2200); + servomotor_2.attach(PIN_RCSERVO2_CNT,800,2200); +} + +void loop() +{ + + delay(500); + + while(1){ + controlMotor(MOTORTYPE_MOTOR1_1,MOTORACTION_MOTOR_FORWARD); + controlMotor(MOTORTYPE_MOTOR1_2,MOTORACTION_MOTOR_REVERSE); + delay(2000); + controlMotor(MOTORTYPE_MOTOR1_1,MOTORACTION_MOTOR_REVERSE); + controlMotor(MOTORTYPE_MOTOR1_2,MOTORACTION_MOTOR_FORWARD); + delay(2000); + } +} + +void controlMotor(int motortype,int motoraction) +{ + int pinno_1; + int pinno_2; + + const unsigned char cont[4][2] = { + { LOW , LOW }, // STOP + { HIGH , LOW }, // FORWARD + { LOW , HIGH }, // REVERSE + { HIGH , HIGH } // BRAKE + }; + + if(motortype == MOTORTYPE_MOTOR1_1){ + pinno_1 = PIN_MOTOR1_AIN1; + pinno_2 = PIN_MOTOR1_AIN2; + } else { + pinno_1 = PIN_MOTOR1_BIN1; + pinno_2 = PIN_MOTOR1_BIN2; + } + + digitalWrite(pinno_1,cont[motoraction][0]); + digitalWrite(pinno_2,cont[motoraction][1]); +} +