3 changed files with 101 additions and 89 deletions
@ -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<colorValues.length; 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 = hue(video.pixels[loc]); |
|
||||
float briVal = brightness(video.pixels[loc]); |
|
||||
float satVal = saturation(video.pixels[loc]); |
|
||||
|
|
||||
// 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[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<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); |
|
||||
} |
|
||||
|
|
||||
fill(0); |
|
||||
rect(0, 60, colorValues[12], 5); |
|
||||
fill(100); |
|
||||
rect(0, 65, colorValues[13], 5); |
|
||||
println(""); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,84 @@ |
|||||
|
/***************************************************
|
||||
|
This is an example for our Adafruit 16-channel PWM & Servo driver |
||||
|
Servo test - this will drive 8 servos, one after the other on the |
||||
|
first 8 pins of the PCA9685 |
||||
|
|
||||
|
Pick one up today in the adafruit shop! |
||||
|
------> 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 <Wire.h> |
||||
|
#include <Adafruit_PWMServoDriver.h> |
||||
|
|
||||
|
// 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); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
Loading…
Reference in new issue