リチウムイオン電池自走ロボット前後サーボモーター

include “Servo.h” //サーボモータのヘッダーファイルの読み込み

include

//PCA9685 pwm = PCA9685(0x40);

include

include

define SERVO_CH 0

//PCA9685のアドレス指定(アドレスジャンパ未接続時)
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

define SERVOMIN 150

define SERVOMAX 600

//#define CENTER 375
//Servo myservo;
//int VAL=0;
//int target = 2;
//float movetopos;
//char cmd;
int sensa01=HIGH;
int sensa02=HIGH;
void setup(){
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(50);
//for (int i=0;i<3;i++)
//pwm.setPWM(i,0,CENTER);
// Serial.println(“Ready [t###]/[m####]”);
//myservo.attach(6,500,2400); //10ピンからサーボモーターの回転信号をPWM出力
pinMode(13 , INPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9 , OUTPUT);
pinMode(8 , INPUT);
}
int n=0;

void loop(){
sensa01=digitalRead(13);
sensa02=digitalRead(8);
if ((sensa01 == LOW)||(sensa02 == LOW)){
Serial.print(0);
MOVE(100);
//delay(10);
for(int i=0; i<2; i++) servo_write(i,n); n=n+90; if(n>=180)n=0;
delay(10);

//delay(10);

}else{

Serial.print(1);
BACK(2000);
//RIGHT(1500);
//MOVE (1000);
LEFT(1500);
//MOVE(1000);
//LEFT(1500);
//MOVE (1000);
//RIGHT(1500);
//MOVE(1000);
for(int i=0; i<2; i++) servo_write(i,n); n=n+90; if(n>=180)n=0;
delay(10);
//delay(10);
}

//servo_write(SERVO_CH, n);
//n=n+10;
//if(n>=180){
// n=0;
//}
}
void servo_write(int ch, int ang){ /////動かすサーボチャンネルと角度を指定
ang = map(ang, 0, 180, SERVOMIN, SERVOMAX); /////角度(0~180)をPWMのパルス幅(150~500)に変換
pwm.setPWM(ch, 0, ang);
}
void BACK(int ms){
digitalWrite(9, HIGH);
digitalWrite(11 , HIGH);
delay(ms);
digitalWrite(9, LOW);
digitalWrite(11 , LOW);
delay(ms);
}
void MOVE(int ms){
digitalWrite(10, HIGH);
digitalWrite(12 , HIGH);
delay(ms);
digitalWrite(10, LOW);
digitalWrite(12 , LOW);
delay(ms);
}
void LEFT(int ms){
digitalWrite(10, HIGH);
digitalWrite(11 , HIGH);
delay(ms);
digitalWrite(10, LOW);
digitalWrite(11 , LOW);
delay(ms);
}
void RIGHT(int ms){
digitalWrite(12, HIGH);
digitalWrite(9 , HIGH);
delay(ms);
digitalWrite(12, LOW);
digitalWrite(9 , LOW);
delay(ms);
}

//if (Serial.available()){
//int pwmValue;
//cmd = Serial.read();
// movetopos = Serial.parseInt();
//if (cmd == ‘t’){
//target = movetopos;
//Serial.print(“target “);
//Serial.println(target);
//Serial.println(servonum);
//for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { // pwm.setPWM(servonum, 0, pulselen); //} //}else if (cmd == ‘m’){ //digitalWrite(servonum, LOW); //servonum++; //if (servonum > 10)servonum = 0;

//delay(500);
//for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen–) {
// pwm.setPWM(servonum, 0, pulselen);
// }
//movetopos=40;
//servo_write(SERVO_CH, n);
//n=n+10;
//if(n>=180){
// n=0;
//}
//delay(500);

// servonum++;
// delay(500);
//Serial.print(“move “);
// Serial.println(movetopos);
//pwmValue = map(movetopos,0,180,SERVOMIN,SERVOMAX);
//pwm.setPWM(target,2, pwmValue);
//}

//myservo.write(45); //45度に回転
//delay(300); //()㎳待つ

//myservo.write(135);
//delay(10);

//VAL=digitalRead(13);
//if(VAL==0){

//}else{

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です