#include #include 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) int atomPins[] = {2,3,4,5,6,7,8,9}; int currentValue = 0; int values[] = {0,0,0,0,0,0,0,0,0}; int cycle = 0; void setup() { Serial.begin(9600); for(int i=0; i<8; i++) { pinMode(atomPins[i], OUTPUT); } pwm.begin(); pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates delay(10); } void setServoPulse(uint8_t n, double pulse) { double pulselength; pulselength = 1000000; // 1,000,000 us per second pulselength /= 60; // 60 Hz 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 to us pulse /= pulselength; Serial.println(pulse); pwm.setPWM(n, 0, pulse); } void loop() { if(Serial.available()){ int incomingValue = Serial.read(); values[currentValue] = incomingValue; currentValue++; if(currentValue > 8){ currentValue = 0; } cycle = 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++; } } } delay(100); } }