8 changed files with 197 additions and 0 deletions
@ -0,0 +1,59 @@ |
|||||
|
#include <Wire.h> |
||||
|
#include <Adafruit_PWMServoDriver.h> |
||||
|
|
||||
|
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)
|
||||
|
|
||||
|
|
||||
|
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 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, 400); |
||||
|
if(values[i]==0) { |
||||
|
digitalWrite(atomPins[i], LOW); |
||||
|
pwm.setPWM(i, 0, 100); |
||||
|
cycle++; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
delay(100); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
import processing.io.*; |
||||
|
import processing.serial.*; |
||||
|
import processing.video.*; |
||||
|
import processing.serial.*; |
||||
|
|
||||
|
Serial port; |
||||
|
Capture video; |
||||
|
PImage img; |
||||
|
|
||||
|
int videoWidth = 640; |
||||
|
int videoHeight = 480; |
||||
|
|
||||
|
void setup() { |
||||
|
size(640, 480, P2D); |
||||
|
frameRate(10); |
||||
|
colorMode(HSB, 100, 100, 100); |
||||
|
|
||||
|
String portName = Serial.list()[0]; |
||||
|
port = new Serial(this, portName, 9600); |
||||
|
|
||||
|
video = new Capture(this, width, height); |
||||
|
video.start(); |
||||
|
|
||||
|
background(0); |
||||
|
} |
||||
|
|
||||
|
void draw() { |
||||
|
ephPercentage(); |
||||
|
} |
||||
|
|
||||
|
void keyPressed() { |
||||
|
if (key == 'S' || key == 's') { |
||||
|
video.stop(); |
||||
|
printArray(colorValues); |
||||
|
makePicture(); |
||||
|
} |
||||
|
if (key == 'B' || key == 'b') { |
||||
|
backupFrame(); |
||||
|
} |
||||
|
if (key == 'G' || key == 'g') { |
||||
|
video.start(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
void makePicture() { |
||||
|
saveFrame("data/shot.gif"); |
||||
|
img = loadImage("shot.gif"); |
||||
|
ephSerial(); |
||||
|
} |
||||
|
|
||||
|
void backupFrame() { |
||||
|
// save picture with results for backup |
||||
|
saveFrame("history/shot_" + |
||||
|
nf(year(), 4) + nf(month(), 2) + nf(day(), 2) + "_" + |
||||
|
nf(hour(), 2) + nf(minute(), 2) + nf(second(), 2) + |
||||
|
".gif"); |
||||
|
println("color count image saved"); |
||||
|
} |
After Width: | Height: | Size: 44 KiB |
After Width: | Height: | Size: 44 KiB |
@ -0,0 +1,68 @@ |
|||||
|
int cellSize = 20; |
||||
|
int cols = videoWidth / cellSize; |
||||
|
int rows = videoHeight / cellSize; |
||||
|
int cellN = (cols)*(rows); |
||||
|
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() { |
||||
|
|
||||
|
if (video.available()) { |
||||
|
video.read(); |
||||
|
video.loadPixels(); |
||||
|
|
||||
|
for(int i = 0; i<colorsN; i++) { |
||||
|
colorValues[i] = 0; |
||||
|
} |
||||
|
|
||||
|
for (int i = 0; i < cols; i++) { |
||||
|
for (int j = 0; j < rows; j++) { |
||||
|
|
||||
|
int x = i*cellSize; |
||||
|
int y = j*cellSize; |
||||
|
int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image |
||||
|
|
||||
|
// Get hue and brightness values |
||||
|
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); |
||||
|
|
||||
|
// Check brightness values, |
||||
|
// if greater then 80% increace white counter, |
||||
|
// if lower then 20% increace black counter, |
||||
|
// otherwise increase color counters |
||||
|
if(briVal > 20 && briVal < 80) { |
||||
|
colorValues[constrain(int(hueVal/colorsRange),0,colorsN-3)]++; |
||||
|
} else if(briVal <= 20) { |
||||
|
colorValues[colorsN-2]++; |
||||
|
} else if(briVal >= 80) { |
||||
|
colorValues[colorsN-1]++; |
||||
|
} |
||||
|
|
||||
|
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<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, 5*(colorsN-2)+5, map(colorValues[colorsN-2]/(cellN/100),0,100,0,width), 5); |
||||
|
fill(100); |
||||
|
rect(0, 5*(colorsN-1)+5, map(colorValues[colorsN-1]/(cellN/100),0,100,0,width), 5); |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
|
||||
|
void ephSerial() { |
||||
|
|
||||
|
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…
Reference in new issue