Infrared sensor and PCA9685

Hi, I am working on my own interactive piece that uses a PCA9685 driver and an infrared sensor. Here is the situation that I want: servos rotate around randomly and independently when there is no interaction & when there is an interaction which is when an infrared sensor senses the distance of the human hand, it can control servos turn into a certain angle.

I have 8 servos here connected by PCA9685 and they move randomly and independently. I want to add an Infrared sensor to control them all to turn into a certain angel. But right now I only figure out the step of their automatic motions.

Here is the code that I program. I also try to add an infrared section there, divide it’s Val into two situations, but it is not working.


#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <Servo.h>
#include <SharpIR.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50

int IR = A0;
int val;

int myservo1 = 0;
int myservo2 = 1;
int myservo3 = 2;
int myservo4 = 3;
int myservo5 = 4;
int myservo6 = 5;
int myservo7 = 6;
int myservo8 = 7;

int pulseWidth(int angle) {
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
return analog_value;
}

int stopMotor(int IRIn, int myservoOut) {
// Read values from IR
val = analogRead(IR);
}

void setup() {
pwm.begin();
pwm.setPWMFreq(60);
}

void loop() {
//servo automatic motion
if (val == 0) {
pwm.setPWM(0, 0, pulseWidth(0));
delay(10);
pwm.setPWM(1, 0, pulseWidth(20));
delay(20);
pwm.setPWM(2, 0, pulseWidth(11));
delay(22);
pwm.setPWM(3, 0, pulseWidth(90));
delay(52);
pwm.setPWM(4, 0, pulseWidth(40));
delay(60);
pwm.setPWM(5, 0, pulseWidth(50));
delay(50);
pwm.setPWM(6, 0, pulseWidth(180));
delay(13);
pwm.setPWM(7, 0, pulseWidth(66));
delay(100);

pwm.setPWM(0, 0, pulseWidth(40));
delay(101);
pwm.setPWM(1, 0, pulseWidth(0));
delay(50);
pwm.setPWM(2, 0, pulseWidth(30));
delay(22);
pwm.setPWM(3, 0, pulseWidth(80));
delay(70);
pwm.setPWM(4, 0, pulseWidth(70));
delay(100);
pwm.setPWM(5, 0, pulseWidth(180));
delay(99);
pwm.setPWM(6, 0, pulseWidth(55));
delay(180);
pwm.setPWM(7, 0, pulseWidth(50));
delay(200);

pwm.setPWM(0, 0, pulseWidth(180));
delay(100);
pwm.setPWM(1, 0, pulseWidth(45));
delay(75);
pwm.setPWM(2, 0, pulseWidth(77));
delay(300);
pwm.setPWM(3, 0, pulseWidth(33));
delay(150);
pwm.setPWM(4, 0, pulseWidth(90));
delay(100);
pwm.setPWM(5, 0, pulseWidth(37));
delay(500);
pwm.setPWM(6, 0, pulseWidth(85));
delay(410);
pwm.setPWM(7, 0, pulseWidth(150));
delay(100);

pwm.setPWM(0, 0, pulseWidth(90));
delay(120);
pwm.setPWM(1, 0, pulseWidth(180));
delay(10);
pwm.setPWM(2, 0, pulseWidth(25));
delay(20);
pwm.setPWM(3, 0, pulseWidth(44));
delay(30);
pwm.setPWM(4, 0, pulseWidth(180));
delay(23);
pwm.setPWM(5, 0, pulseWidth(72));
delay(245);
pwm.setPWM(6, 0, pulseWidth(15));
delay(500);
pwm.setPWM(7, 0, pulseWidth(177));
delay(50);

pwm.setPWM(0, 0, pulseWidth(120));
delay(120);
pwm.setPWM(1, 0, pulseWidth(100));
delay(22);
pwm.setPWM(2, 0, pulseWidth(103));
delay(32);
pwm.setPWM(3, 0, pulseWidth(11));
delay(100);
pwm.setPWM(5, 0, pulseWidth(40));
delay(300);
pwm.setPWM(6, 0, pulseWidth(69));
delay(50);
pwm.setPWM(7, 0, pulseWidth(131));
delay(100);
}

// IR control Servo
if (val == 1) {
stopMotor(IR, myservo1);

//Control myservo2
stopMotor(IR, myservo2);

//Control myservo3
stopMotor(IR, myservo3);

//Control myservo4
stopMotor(IR, myservo4);

//Control myservo5
stopMotor(IR, myservo5);

//Control myservo6
stopMotor(IR, myservo6);

//Control myservo7
stopMotor(IR, myservo7);

//Control myservo8
stopMotor(IR, myservo8);
}
}

Tip 1: analogRead range is 0 ~ 1023. Check through the serial monitor, which value is being obtained, you will probably have to use a different value, 512 would be the center of 1024.

Tip 2: You can use a 'for' loop in conjunction with 'arrays' to do sequences instead of copying the same code multiple times.

@YUE134340

DUPLICATE DELETED

Please READ THIS POST then you know how to use the forum properly.

Bob.

@YUE134340

DUPLICATE DELETED AGAIN !

READ THIS POST NOW !

Then you know how to use the forum properly.

If you do this one more time you will be given a time out !

Bob.