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

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{

リチウムイオン電池自走ロボット

include "Servo.h" //サーボモータのヘッダーファイルの読み込み
Servo myservo;
int VAL=0;
void setup(){
Serial.begin(9600);
myservo.attach(6,500,2400); //10ピンからサーボモーターの回転信号をPWM出力
pinMode(13 , INPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9 , OUTPUT);
pinMode(6 , OUTPUT);
}
//sensa-nohannouwosirberu LEDde
void loop(){
myservo.write(45); //45度に回転
delay(300); //1000㎳待つ
myservo.write(135);
delay(10);
VAL=digitalRead(13);
if(VAL==0){
Serial.print(0);
MOVE(100);
delay(10);
}else{
Serial.print(1);
BACK(2000);
//RIGHT(1500);
//MOVE (1000);
LEFT(1500);
//MOVE(1000);
//LEFT(1500);
//MOVE (1000);
//RIGHT(1500);
//MOVE(1000);
delay(10);
}}
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);
}

サーボモーター 3個同時制御

/*
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test - this will drive 8 servos, one after the other on the
first 8 pins of the PCA9685
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These drivers use I2C to communicate, 2 pins are required to
interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**/
include
include
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, Wire);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
define USMIN 600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
define USMAX 2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates
// our servo # counter
uint8_t servonum = 0;
int LED = 13;
int isObstaclePin = 2; // 入力ピンです
int isObstacle = HIGH; // 障害物が無い場合
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("8 channel Servo test!");
pinMode(isObstaclePin, INPUT);
pwm.begin();
/*
In theory the internal oscillator (clock) is 25MHz but it really isn't
that precise. You can 'calibrate' this by tweaking this number until
you get the PWM update frequency you're expecting!
The int.osc. for the PCA9685 chip is a range between about 23-27MHz and
is used for calculating things like writeMicroseconds()
Analog servos run at ~50 Hz updates, It is importaint to use an
oscilloscope in setting the int.osc frequency for the I2C PCA9685 chip.
1) Attach the oscilloscope to one of the PWM signal pins and ground on
the I2C PCA9685 chip you are setting the value for.
2) Adjust setOscillatorFrequency() until the PWM update frequency is the
expected value (50Hz for most ESCs)
Setting the value here is specific to each individual I2C PCA9685 chip and
affects the calculations for the PWM update frequency.
Failure to correctly set the int.osc value will cause unexpected PWM results
*/
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
delay(10);
}
// You can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000000; // convert input seconds to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW){
digitalWrite(LED, HIGH);
// Drive each servo one at a time using setPWM()
Serial.println(servonum);
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(servonum, 0, pulselen); } }else{ //以下の数字が動作の秒数 digitalWrite(LED, LOW); digitalWrite(servonum, LOW); servonum++; //if (servonum > 10)servonum = 0;
delay(500);
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
pwm.setPWM(servonum, 0, pulselen);
}
delay(500);
}
// Drive each servo one at a time using writeMicroseconds(), it's not precise due to calculation rounding!
// The writeMicroseconds() function is used to mimic the Arduino Servo library writeMicroseconds() behavior.
///for (uint16_t microsec = USMIN; microsec < USMAX; microsec++) {
/// pwm.writeMicroseconds(servonum, microsec);
/// }
/// delay(500);
///for (uint16_t microsec = USMAX; microsec > USMIN; microsec--) {
/// pwm.writeMicroseconds(servonum, microsec);
/// }
///delay(500);
//以下の数字が動作の秒数
servonum++;
if (servonum > 3) servonum = 0; // Testing the first 8 servo channels
}

サーボモーター20Kg赤外線センサー

/*
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test – this will drive 8 servos, one after the other on the
first 8 pins of the PCA9685
Pick one up today in the adafruit shop!
——> http://www.adafruit.com/products/815
These drivers use I2C to communicate, 2 pins are required to
interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**/
include
include
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, Wire);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You’ll have to tweak them as necessary to match the servos you
// have!
define SERVOMIN 150 // This is the ‘minimum’ pulse length count (out of 4096)
define SERVOMAX 600 // This is the ‘maximum’ pulse length count (out of 4096)
define USMIN 600 // This is the rounded ‘minimum’ microsecond length based on the minimum pulse of 150
define USMAX 2400 // This is the rounded ‘maximum’ microsecond length based on the maximum pulse of 600
define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates
// our servo # counter
uint8_t servonum = 0;
int LED = 13;
int isObstaclePin = 2; // 入力ピンです
int isObstacle = HIGH; // 障害物が無い場合
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println(“8 channel Servo test!”);
pinMode(isObstaclePin, INPUT);
pwm.begin();
/*
In theory the internal oscillator (clock) is 25MHz but it really isn’t
that precise. You can ‘calibrate’ this by tweaking this number until
you get the PWM update frequency you’re expecting!
The int.osc. for the PCA9685 chip is a range between about 23-27MHz and
is used for calculating things like writeMicroseconds()
Analog servos run at ~50 Hz updates, It is importaint to use an
oscilloscope in setting the int.osc frequency for the I2C PCA9685 chip.
1) Attach the oscilloscope to one of the PWM signal pins and ground on
the I2C PCA9685 chip you are setting the value for.
2) Adjust setOscillatorFrequency() until the PWM update frequency is the
expected value (50Hz for most ESCs)
Setting the value here is specific to each individual I2C PCA9685 chip and
affects the calculations for the PWM update frequency.
Failure to correctly set the int.osc value will cause unexpected PWM results
*/
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates delay(10);
}
// You can use this function if you’d like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It’s not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
Serial.print(pulselength); Serial.println(” us per period”);
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(” us per bit”);
pulse *= 1000000; // convert input seconds to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW){
digitalWrite(LED, HIGH);
// Drive each servo one at a time using setPWM()
Serial.println(servonum);
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(servonum, 0, pulselen); } }else{ //以下の数字が動作の秒数 digitalWrite(LED, LOW); digitalWrite(servonum, LOW); servonum++; //if (servonum > 10)servonum = 0;
delay(500);
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen–) {
pwm.setPWM(servonum, 0, pulselen);
}
delay(500);
}
// Drive each servo one at a time using writeMicroseconds(), it’s not precise due to calculation rounding!
// The writeMicroseconds() function is used to mimic the Arduino Servo library writeMicroseconds() behavior.
///for (uint16_t microsec = USMIN; microsec < USMAX; microsec++) {
/// pwm.writeMicroseconds(servonum, microsec);
/// }
/// delay(500);
///for (uint16_t microsec = USMAX; microsec > USMIN; microsec–) {
/// pwm.writeMicroseconds(servonum, microsec);
/// }
///delay(500);
//以下の数字が動作の秒数
servonum++;
if (servonum > 3) servonum = 0; // Testing the first 8 servo channels
}

サーボモーター20kgアルコール消毒

/*
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test – this will drive 8 servos, one after the other on the
first 8 pins of the PCA9685

Pick one up today in the adafruit shop!
——> http://www.adafruit.com/products/815

These drivers use I2C to communicate, 2 pins are required to
interface.

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**/

include

include

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, Wire);

// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You’ll have to tweak them as necessary to match the servos you
// have!

define SERVOMIN 150 // This is the ‘minimum’ pulse length count (out of 4096)

define SERVOMAX 600 // This is the ‘maximum’ pulse length count (out of 4096)

define USMIN 600 // This is the rounded ‘minimum’ microsecond length based on the minimum pulse of 150

define USMAX 2400 // This is the rounded ‘maximum’ microsecond length based on the maximum pulse of 600

define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

define echoPin 2 // Echo Pin

define trigPin 3 // Trigger Pin

double Duration = 0; //受信した間隔
double Distance = 0; //距離
// our servo # counter
uint8_t servonum = 0;

void setup() {

Serial.begin(9600);
Serial.println(“8 channel Servo test!”);
pinMode( echoPin, INPUT );
pinMode( trigPin, OUTPUT );
pwm.begin();
/*

  • In theory the internal oscillator (clock) is 25MHz but it really isn’t
  • that precise. You can ‘calibrate’ this by tweaking this number until
  • you get the PWM update frequency you’re expecting!
  • The int.osc. for the PCA9685 chip is a range between about 23-27MHz and
  • is used for calculating things like writeMicroseconds()
  • Analog servos run at ~50 Hz updates, It is importaint to use an
  • oscilloscope in setting the int.osc frequency for the I2C PCA9685 chip.
  • 1) Attach the oscilloscope to one of the PWM signal pins and ground on
  • the I2C PCA9685 chip you are setting the value for.
  • 2) Adjust setOscillatorFrequency() until the PWM update frequency is the
  • expected value (50Hz for most ESCs)
  • Setting the value here is specific to each individual I2C PCA9685 chip and
  • affects the calculations for the PWM update frequency.
  • Failure to correctly set the int.osc value will cause unexpected PWM results
    */
    pwm.setOscillatorFrequency(27000000);
    pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates delay(10);
    }

// You can use this function if you’d like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It’s not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;

pulselength = 1000000; // 1,000,000 us per second
pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
Serial.print(pulselength); Serial.println(” us per period”);
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(” us per bit”);
pulse *= 1000000; // convert input seconds to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite( trigPin, HIGH ); //超音波を出力
delayMicroseconds( 10 ); //
digitalWrite( trigPin, LOW );
Duration = pulseIn( echoPin, HIGH ); //センサからの入力
if (Duration > 0) {
Duration = Duration/2; //往復距離を半分にする
Distance = Duration340100/1000000; // 音速を340m/sに設定
// Drive each servo one at a time using setPWM()
if(Distance<15){ Serial.println(servonum); for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(servonum, 0, pulselen); } }else{ digitalWrite(servonum, LOW); servonum++; //if (servonum > 3)servonum = 0;

delay(500);
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen–) {
pwm.setPWM(servonum, 0, pulselen);
}
}

delay(500);

// Drive each servo one at a time using writeMicroseconds(), it’s not precise due to calculation rounding!
// The writeMicroseconds() function is used to mimic the Arduino Servo library writeMicroseconds() behavior.
///for (uint16_t microsec = USMIN; microsec < USMAX; microsec++) {
/// pwm.writeMicroseconds(servonum, microsec);
/// }

/// delay(500);
///for (uint16_t microsec = USMAX; microsec > USMIN; microsec–) {
/// pwm.writeMicroseconds(servonum, microsec);
/// }

///delay(500);
//以下の数字が動作の秒数
servonum++;
if (servonum > 3) servonum = 0; // Testing the first 8 servo channels
}}