Browse Source

fix dynamic percentage

add arduino pwm
develop
Carmine De Rosa 6 years ago
parent
commit
4087922c67
  1. 2
      .gitignore
  2. 56
      arduino/arduino.ino
  3. BIN
      ephimera/data/shot.gif
  4. 3
      ephimera/ephimera.pde
  5. 34
      ephimera/percentage.pde
  6. 9
      ephimera/serial.pde

2
.gitignore

@ -5,3 +5,5 @@ application.linux64
application.windows32
application.windows64
application.macosx
ephimera/data/

56
arduino/arduino.ino

@ -1,4 +1,3 @@
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
@ -7,12 +6,19 @@ 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)
String inputString = ""; // a String to hold incoming data
boolean stringComplete = false; // whether the string is complete
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
@ -21,31 +27,33 @@ void setup() {
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
if(Serial.available()){
int incomingValue = Serial.read();
values[currentValue] = incomingValue;
currentValue++;
if(currentValue > 8){
currentValue = 0;
}
cycle = 0;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
while(cycle != 8) {
for(int i=0; i<8; i++) {
if(values[i]>0) {
values[i]--;
digitalWrite(atomPins[i], HIGH);
pwm.setPWM(i, 0, 400);
if(values[i]==0) {
digitalWrite(atomPins[i], LOW);
pwm.setPWM(i, 0, 100);
cycle++;
}
}
}
delay(100);
}
}
void svuota(){
}

BIN
ephimera/data/shot.gif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

3
ephimera/ephimera.pde

@ -26,11 +26,8 @@ void setup() {
void draw() {
ephPercentage();
ephSerial();
}
void keyPressed() {
if (key == 'S' || key == 's') {
video.stop();

34
ephimera/percentage.pde

@ -2,7 +2,10 @@ int cellSize = 20;
int cols = videoWidth / cellSize;
int rows = videoHeight / cellSize;
int cellN = (cols)*(rows);
byte[] colorValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int[] colorValues = {0, 0, 0, 0, 0, 0, 0, 0};
byte[] colorByteValues = {0, 0, 0, 0, 0, 0, 0, 0};
int colorsN = colorValues.length;
float colorsRange = float(100)/float(colorsN-2);
void ephPercentage() {
@ -10,7 +13,7 @@ void ephPercentage() {
video.read();
video.loadPixels();
for(int i = 0; i<colorValues.length; i++) {
for(int i = 0; i<colorsN; i++) {
colorValues[i] = 0;
}
@ -22,9 +25,9 @@ void ephPercentage() {
int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
// Get hue and brightness values
float hueVal = hue(video.pixels[loc]);
float briVal = brightness(video.pixels[loc]);
float satVal = saturation(video.pixels[loc]);
float hueVal = constrain(hue(video.pixels[loc]),0,100);
float briVal = constrain(brightness(video.pixels[loc]),0,100);
float satVal = constrain(saturation(video.pixels[loc]),0,100);
// Set pixel color
color c = color(hueVal, satVal, briVal);
@ -34,11 +37,11 @@ void ephPercentage() {
// if lower then 20% increace black counter,
// otherwise increase color counters
if(briVal > 20 && briVal < 80) {
colorValues[int(hueVal/8.3)]++;
colorValues[constrain(int(hueVal/colorsRange),0,colorsN-3)]++;
} else if(briVal <= 20) {
colorValues[12]++;
} else if(briVal >= 20) {
colorValues[13]++;
colorValues[colorsN-2]++;
} else if(briVal >= 80) {
colorValues[colorsN-1]++;
}
pushMatrix();
@ -52,17 +55,14 @@ void ephPercentage() {
}
// Print colors percentage values
for(int i = 0; i<colorValues.length-2; i++) {
//print(colorValues[i]/(float(cellN)/100) + "\t");
fill(color((i*8.3)+5, 100, 100));
rect(0, (i*5)+5, colorValues[i], 5);
for(int i = 0; i<colorsN-2; i++) {
fill(color((i*colorsRange), 100, 100));
rect(0, (i*5)+5, map(colorValues[i]/(cellN/100),0,100,0,width), 5);
}
fill(0);
rect(0, 65, colorValues[12], 5);
rect(0, 5*(colorsN-2)+5, map(colorValues[colorsN-2]/(cellN/100),0,100,0,width), 5);
fill(100);
rect(0, 70, colorValues[13], 5);
println("");
rect(0, 5*(colorsN-1)+5, map(colorValues[colorsN-1]/(cellN/100),0,100,0,width), 5);
}
}

9
ephimera/serial.pde

@ -1,4 +1,11 @@
void ephSerial() {
port.write(colorValues);
for(int i = 0; i<colorsN; i++) {
colorByteValues[i] = byte(colorValues[i]/(cellN/100));
print(colorByteValues[i] + "\t");
}
println("");
port.write(colorByteValues);
printArray(colorByteValues);
}

Loading…
Cancel
Save