Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ArduinoでPCA9685を使って複数のサーボモータを回してみる
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 150
#define SERVOMAX 600
#define CENTER 375
#define PIN_BUTTON1 8
#define PIN_BUTTON2 7
#define STEP 30
int target = 0;
int val = 90;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(PIN_BUTTON1, INPUT);
pinMode(PIN_BUTTON2, INPUT);
pwm.begin();
pwm.setPWMFreq(60);
pwm.setPWM(0, 0, CENTER);
pwm.setPWM(4, 0, CENTER);
pwm.setPWM(8, 0, CENTER);
}
void move(int target, int degree){
float pos = map(degree, 0, 180, SERVOMIN, SERVOMAX);
Serial.print(degree);
Serial.print(" : ");
Serial.println(pos);
pwm.setPWM(target, 0, pos);
}
void moveAll(int degree){
move(0, degree);
move(4, degree);
move(8, degree);
//delay(20);
}
void loop() {
// put your main code here, to run repeatedly:
int button1State = digitalRead(PIN_BUTTON1);
int button2State = digitalRead(PIN_BUTTON2);
if( (button1State == LOW) && (button2State == HIGH) ) {
val-=STEP;
moveAll(val);
}else if((button1State == HIGH) && (button2State == LOW)){
val+=STEP;
moveAll(val);
}else{
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment