From 30f7237c3efbc0388dcfc568fa2b5c313a4e868c Mon Sep 17 00:00:00 2001 From: Carmine De Rosa Date: Sat, 28 Sep 2019 17:29:56 +0200 Subject: [PATCH] atomizers timer --- arduino/arduino.ino | 77 ++++++++++++++++++++++++++++--------------- ephimera/ephimera.pde | 6 ++-- ephimera/frames.pde | 1 + ephimera/serial.pde | 28 ++++++++++++++-- 4 files changed, 80 insertions(+), 32 deletions(-) diff --git a/arduino/arduino.ino b/arduino/arduino.ino index e6ee21d..f77f3a9 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -3,13 +3,17 @@ 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 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; void setup() { @@ -17,8 +21,11 @@ void setup() { for(int i=0; i<8; i++) { pinMode(atomPins[i], OUTPUT); + pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN); } - + + pinMode(13, OUTPUT); + digitalWrite(13, HIGH); pwm.begin(); pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates @@ -40,33 +47,49 @@ void setServoPulse(uint8_t n, double pulse) { } void loop() { - if(Serial.available()){ - int incomingValue = Serial.read(); - - values[currentValue] = incomingValue; - currentValue++; - if(currentValue > 8){ - currentValue = 0; - } - cycle = 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(); } - } - } - delay(100); + } + + + 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); + } + } } - } diff --git a/ephimera/ephimera.pde b/ephimera/ephimera.pde index d974ae4..5dd2233 100644 --- a/ephimera/ephimera.pde +++ b/ephimera/ephimera.pde @@ -9,10 +9,11 @@ PImage img; int videoWidth = 640; int videoHeight = 480; +int sending = 0; void setup() { size(640, 480, P2D); - frameRate(10); + frameRate(2); colorMode(HSB, 100, 100, 100); String portName = Serial.list()[0]; @@ -25,13 +26,12 @@ void setup() { } void draw() { - ephPercentage(); + //ephPercentage(); } void keyPressed() { if (key == 'S' || key == 's') { video.stop(); - printArray(colorValues); makePicture(); } if (key == 'B' || key == 'b') { diff --git a/ephimera/frames.pde b/ephimera/frames.pde index 0ba497e..e6a1950 100644 --- a/ephimera/frames.pde +++ b/ephimera/frames.pde @@ -1,6 +1,7 @@ void makePicture() { saveFrame("data/shot.gif"); img = loadImage("shot.gif"); + ephPercentage(); ephSerial(); } diff --git a/ephimera/serial.pde b/ephimera/serial.pde index f3d74ad..6b0d38b 100644 --- a/ephimera/serial.pde +++ b/ephimera/serial.pde @@ -1,3 +1,6 @@ +public static final char HEADER = 'H'; +public static final char A_TAG = 'M'; +public static final char B_TAG = 'X'; void ephSerial() { @@ -6,6 +9,27 @@ void ephSerial() { print(colorByteValues[i] + "\t"); } println(""); - port.write(colorByteValues); - printArray(colorByteValues); + sendMessage(A_TAG, colorByteValues); + //sendMessage(A_TAG, 5,55,0,33,0,55,0,55); + //printArray(colorByteValues); +} + + +void sendMessage(char tag, byte[] cbv){ + // send the given index and value to the serial port + port.write(HEADER); + port.write(tag); + + for(int i=0; i