|
@ -3,13 +3,17 @@ |
|
|
|
|
|
|
|
|
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); |
|
|
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); |
|
|
|
|
|
|
|
|
#define SERVOMIN 350 // this is the 'minimum' pulse length count (out of 4096)
|
|
|
|
|
|
#define SERVOMAX 385 // this is the 'maximum' pulse length count (out of 4096)
|
|
|
|
|
|
|
|
|
#define SERVOMIN 330 // this is the 'minimum' pulse length count (out of 4096) - increasing SERVOMIN decrease speed
|
|
|
|
|
|
#define SERVOMAX 390 // this is the 'maximum' pulse length count (out of 4096)
|
|
|
|
|
|
|
|
|
|
|
|
const char HEADER = 'H'; |
|
|
|
|
|
const char A_TAG = 'M'; |
|
|
|
|
|
const char B_TAG = 'X'; |
|
|
|
|
|
const int TOTAL_BYTES = 10 ; // the total bytes in a message
|
|
|
|
|
|
|
|
|
int atomPins[] = {2,3,4,5,6,7,8,9}; |
|
|
int atomPins[] = {2,3,4,5,6,7,8,9}; |
|
|
int currentValue = 0; |
|
|
int currentValue = 0; |
|
|
int values[] = {0,0,0,0,0,0,0,0,0}; |
|
|
|
|
|
|
|
|
int values[] = {0,0,0,0,0,0,0,0}; |
|
|
int cycle = 0; |
|
|
int cycle = 0; |
|
|
|
|
|
|
|
|
void setup() { |
|
|
void setup() { |
|
@ -17,8 +21,11 @@ void setup() { |
|
|
|
|
|
|
|
|
for(int i=0; i<8; i++) { |
|
|
for(int i=0; i<8; i++) { |
|
|
pinMode(atomPins[i], OUTPUT); |
|
|
pinMode(atomPins[i], OUTPUT); |
|
|
|
|
|
pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pinMode(13, OUTPUT); |
|
|
|
|
|
digitalWrite(13, HIGH); |
|
|
pwm.begin(); |
|
|
pwm.begin(); |
|
|
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
|
|
|
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
|
|
|
|
|
|
|
|
@ -40,33 +47,49 @@ void setServoPulse(uint8_t n, double pulse) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
if(Serial.available()){ |
|
|
|
|
|
int incomingValue = Serial.read(); |
|
|
|
|
|
|
|
|
|
|
|
values[currentValue] = incomingValue; |
|
|
|
|
|
|
|
|
cycle = 0; |
|
|
|
|
|
|
|
|
currentValue++; |
|
|
|
|
|
if(currentValue > 8){ |
|
|
|
|
|
currentValue = 0; |
|
|
|
|
|
} |
|
|
|
|
|
cycle = 0; |
|
|
|
|
|
|
|
|
for(int i=0; i<8; i++) { |
|
|
|
|
|
values[i] = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
while(cycle != 8) { |
|
|
|
|
|
for(int i=0; i<8; i++) { |
|
|
|
|
|
if(values[i]>0) { |
|
|
|
|
|
values[i]--; |
|
|
|
|
|
digitalWrite(atomPins[i], HIGH); |
|
|
|
|
|
pwm.setPWM(i, 0, SERVOMIN); |
|
|
|
|
|
if(values[i]==0) { |
|
|
|
|
|
digitalWrite(atomPins[i], LOW); |
|
|
|
|
|
pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN); |
|
|
|
|
|
cycle++; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(Serial.available() >= TOTAL_BYTES) { |
|
|
|
|
|
if( Serial.read() == HEADER) { |
|
|
|
|
|
char tag = Serial.read(); |
|
|
|
|
|
if(tag == A_TAG) { |
|
|
|
|
|
for(int i=0; i<8; i++) { |
|
|
|
|
|
values[i] = Serial.read(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(cycle < 8) { |
|
|
|
|
|
|
|
|
|
|
|
for(int i=0; i<8; i++) { |
|
|
|
|
|
Serial.print(values[i]); |
|
|
|
|
|
Serial.print(" "); |
|
|
|
|
|
if(values[i]>0) { |
|
|
|
|
|
values[i]--; |
|
|
|
|
|
digitalWrite(atomPins[i], HIGH); |
|
|
|
|
|
//pwm.setPWM(i, 0, SERVOMIN);
|
|
|
|
|
|
if(values[i]==0) { |
|
|
|
|
|
cycle++; |
|
|
|
|
|
digitalWrite(atomPins[i], LOW); |
|
|
|
|
|
//pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN);
|
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
//cycle++;
|
|
|
|
|
|
//digitalWrite(atomPins[i], LOW);
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
Serial.println(""); |
|
|
|
|
|
delay(500); |
|
|
|
|
|
|
|
|
|
|
|
//digitalWrite(13, LOW);
|
|
|
} |
|
|
} |
|
|
delay(100); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|