You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
3.9 KiB
171 lines
3.9 KiB
#include <Wire.h>
|
|
#include <Adafruit_PWMServoDriver.h>
|
|
|
|
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
|
|
|
|
#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)
|
|
#define TIMER 200
|
|
#define SERVODIVIDER 5
|
|
#define STRIP 10
|
|
|
|
const char HEADER = 'H';
|
|
const char A_TAG = 'M';
|
|
const char B_TAG = 'X';
|
|
const char C_TAG = 'U';
|
|
const char F_TAG = 'F';
|
|
|
|
const int TOTAL_BYTES = 10 ; // the total bytes in a message
|
|
|
|
int atomPins[] = {2,3,4,5,6,7,8,9};
|
|
int currentValue = 0;
|
|
int values[] = {0,0,0,0,0,0,0,0};
|
|
|
|
int cycle = 0;
|
|
boolean fadeDirection = 0; // 0 grow
|
|
byte fadeStatus = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
for(int i=0; i<8; i++) {
|
|
pinMode(atomPins[i], OUTPUT);
|
|
pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN);
|
|
}
|
|
|
|
pinMode(STRIP, OUTPUT);
|
|
fadeIn();
|
|
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() {
|
|
|
|
cycle = 0;
|
|
|
|
for(int i=0; i<8; i++) {
|
|
values[i] = 0;
|
|
}
|
|
|
|
if(Serial.available() >= TOTAL_BYTES) {
|
|
if( Serial.read() == HEADER) {
|
|
char tag = Serial.read();
|
|
|
|
if(tag == F_TAG) {
|
|
fadeOut();
|
|
}
|
|
|
|
if(tag == A_TAG || tag == B_TAG) {
|
|
for(int i=0; i<8; i++) {
|
|
values[i] = Serial.read();
|
|
}
|
|
}
|
|
|
|
for(int i=0; i<8; i++) {
|
|
if(tag == A_TAG) {
|
|
pwm.setPWM(i, 0, SERVOMAX+50);
|
|
delay(100*SERVODIVIDER);
|
|
pwm.setPWM(i, 0, SERVOMAX-50);
|
|
delay(100*SERVODIVIDER);
|
|
}
|
|
pwm.setPWM(i, 0, SERVOMIN);
|
|
delay(values[i]*SERVODIVIDER);
|
|
pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN);
|
|
}
|
|
|
|
if(tag == C_TAG) {
|
|
|
|
for(int i=0; i<8; i++) {
|
|
pwm.setPWM(i, 0, SERVOMAX+50);
|
|
delay(100*SERVODIVIDER);
|
|
pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN);
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
for(int i=0; i<8; i++) {
|
|
Serial.print(values[i]);
|
|
Serial.print(" ");
|
|
if(values[i]>0) {
|
|
values[i]--;
|
|
digitalWrite(atomPins[i], HIGH);
|
|
if(values[i]==0) {
|
|
cycle++;
|
|
digitalWrite(atomPins[i], LOW);
|
|
}
|
|
}
|
|
}
|
|
|
|
if(tag == A_TAG) {
|
|
while(cycle < 8) {
|
|
cycle = 0;
|
|
for(int i=0; i<8; i++) {
|
|
Serial.print(values[i]);
|
|
Serial.print(" ");
|
|
if(values[i]>0) {
|
|
values[i]--;
|
|
digitalWrite(atomPins[i], HIGH);
|
|
if(values[i]==0) {
|
|
cycle++;
|
|
digitalWrite(atomPins[i], LOW);
|
|
}
|
|
} else {
|
|
cycle++;
|
|
}
|
|
|
|
for(int f=0;f<4;f++){
|
|
fadeGradient();
|
|
delay(TIMER/(8*4));
|
|
}
|
|
}
|
|
Serial.println("");
|
|
}
|
|
|
|
fadeIn();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void fadeIn() {
|
|
for(int i=0; i<255; i++) {
|
|
analogWrite(STRIP, i);
|
|
delay(10);
|
|
}
|
|
}
|
|
|
|
void fadeOut() {
|
|
for(int i=255; i>0; i--) {
|
|
analogWrite(STRIP, i);
|
|
delay(10);
|
|
}
|
|
}
|
|
|
|
void fadeGradient() {
|
|
if(fadeDirection == 0) {fadeStatus++;}
|
|
if(fadeDirection == 1) {fadeStatus--;}
|
|
|
|
if(fadeStatus == 0) {fadeDirection = 0;}
|
|
if(fadeStatus == 255) {fadeDirection = 1;}
|
|
|
|
analogWrite(STRIP, fadeStatus);
|
|
}
|
|
|