From 87acb7470a0db3a5370ca732b7335c3fdc917d4b Mon Sep 17 00:00:00 2001 From: Carmine De Rosa Date: Sat, 28 Sep 2019 12:25:24 +0200 Subject: [PATCH 1/4] servo setup --- arduino/arduino.ino | 21 ++++++++-- percentage/percentage.pde | 85 --------------------------------------- servoSetup/servoSetup.ino | 84 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 89 deletions(-) delete mode 100644 percentage/percentage.pde create mode 100644 servoSetup/servoSetup.ino diff --git a/arduino/arduino.ino b/arduino/arduino.ino index 7d4c5e0..e6ee21d 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -3,8 +3,8 @@ Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); -#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096) -#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096) +#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}; @@ -25,6 +25,19 @@ void setup() { 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()){ @@ -44,10 +57,10 @@ void loop() { if(values[i]>0) { values[i]--; digitalWrite(atomPins[i], HIGH); - pwm.setPWM(i, 0, 400); + pwm.setPWM(i, 0, SERVOMIN); if(values[i]==0) { digitalWrite(atomPins[i], LOW); - pwm.setPWM(i, 0, 100); + pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN); cycle++; } } diff --git a/percentage/percentage.pde b/percentage/percentage.pde deleted file mode 100644 index 6488c47..0000000 --- a/percentage/percentage.pde +++ /dev/null @@ -1,85 +0,0 @@ -import processing.serial.*; -import processing.video.*; - -int cellSize = 20; -int cols, rows, cellN; -int[] colorValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - -Capture video; - -void setup() { - size(640, 480); - frameRate(10); - colorMode(HSB, 100, 100, 100); - cols = width / cellSize; - rows = height / cellSize; - cellN = (cols)*(rows); - //colorMode(RGB, 255, 255, 255, 100); - - video = new Capture(this, width, height); - video.start(); - - background(0); -} - -void draw() { - if (video.available()) { - video.read(); - video.loadPixels(); - - for(int i = 0; i 20 && briVal < 80) { - colorValues[int(hueVal/8.3)]++; - } else if(briVal <= 20) { - colorValues[12]++; - } else if(briVal >= 20) { - colorValues[13]++; - } - - pushMatrix(); - translate(x+cellSize/2, y+cellSize/2); - rectMode(CENTER); - fill(c); - noStroke(); - rect(0, 0, cellSize+6, cellSize+6); - popMatrix(); - } - } - - // Print colors percentage values - for(int i = 0; i http://www.adafruit.com/products/815 + + These drivers use I2C to communicate, 2 pins are required to + interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + +#include +#include + +// called this way, it uses the default address 0x40 +Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); +// you can also call it with a different address you want +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41); +// you can also call it with a different address and I2C interface +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, &Wire); + +// Depending on your servo make, the pulse width min and max may vary, you +// want these to be as small/large as possible without hitting the hard stop +// for max range. You'll have to tweak them as necessary to match the servos you +// have! + +#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) + +// our servo # counter +uint8_t servonum = 0; + +void setup() { + Serial.begin(9600); + Serial.println("8 channel Servo test!"); + + pwm.begin(); + + pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates + +pinMode(8, OUTPUT); + delay(10); +} + +// you can use this function if you'd like to set the pulse length in seconds +// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise! +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() { + // Drive each servo one at a time + Serial.println(servonum); + + + for(int x=0; x<8; x++) { + + pwm.setPWM(x, 0, SERVOMIN); + delay(1000); + pwm.setPWM(x, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN); + + delay(1000); + } + + +} From 30f7237c3efbc0388dcfc568fa2b5c313a4e868c Mon Sep 17 00:00:00 2001 From: Carmine De Rosa Date: Sat, 28 Sep 2019 17:29:56 +0200 Subject: [PATCH 2/4] 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 Date: Sat, 28 Sep 2019 18:18:35 +0200 Subject: [PATCH 3/4] optimization --- arduino/arduino.ino | 48 +++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/arduino/arduino.ino b/arduino/arduino.ino index f77f3a9..4965250 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -5,6 +5,8 @@ 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 const char HEADER = 'H'; const char A_TAG = 'M'; @@ -64,32 +66,32 @@ void loop() { } } - - 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); + pwm.setPWM(i, 0, SERVOMIN); + delay(values[i]*SERVODIVIDER); + pwm.setPWM(i, 0, ((SERVOMAX-SERVOMIN)/2)+SERVOMIN); + } - //digitalWrite(13, LOW); + 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); + if(values[i]==0) { + cycle++; + digitalWrite(atomPins[i], LOW); + } + } + } + + Serial.println(""); + delay(TIMER); + + //digitalWrite(13, LOW); + } } } - } } From 476a8f1895a0756caf146b5bf610334ce8892f5a Mon Sep 17 00:00:00 2001 From: Carmine De Rosa Date: Sat, 28 Sep 2019 18:43:36 +0200 Subject: [PATCH 4/4] fine tuning --- ephimera/ephimera.pde | 2 +- ephimera/frames.pde | 3 ++- ephimera/serial.pde | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ephimera/ephimera.pde b/ephimera/ephimera.pde index 5dd2233..0a39e8d 100644 --- a/ephimera/ephimera.pde +++ b/ephimera/ephimera.pde @@ -26,7 +26,7 @@ void setup() { } void draw() { - //ephPercentage(); + ephPercentage(); } void keyPressed() { diff --git a/ephimera/frames.pde b/ephimera/frames.pde index e6a1950..24b236e 100644 --- a/ephimera/frames.pde +++ b/ephimera/frames.pde @@ -1,8 +1,9 @@ void makePicture() { saveFrame("data/shot.gif"); img = loadImage("shot.gif"); - ephPercentage(); + //ephPercentage(); ephSerial(); + video.start(); } void backupFrame() { diff --git a/ephimera/serial.pde b/ephimera/serial.pde index 6b0d38b..37f2076 100644 --- a/ephimera/serial.pde +++ b/ephimera/serial.pde @@ -10,8 +10,6 @@ void ephSerial() { } println(""); sendMessage(A_TAG, colorByteValues); - //sendMessage(A_TAG, 5,55,0,33,0,55,0,55); - //printArray(colorByteValues); } @@ -23,6 +21,7 @@ void sendMessage(char tag, byte[] cbv){ for(int i=0; i