Hi Guy's,
I'm almost there! This code works OK ( sort of) untill I try to write to the digitl pots and compile, I just get an expected declaration before ' } ' token ... What dose this mean? Any help please.. Plus the code for the "prossessing is comented out as it was just to full of bugs !! Heres the code I have.
/*Wii Camera demo using Stephen Hobley's PVision library
Use serial_display 1, processing_display 0
for standard ASCII output to Arduino serial monitor
Use serial_display 0, processing_display 1
for 2-blob communication and plotting to processing application
Author: Johnnyonthespot
*/
#include <Wire.h>
#include <PVision.h>
#include <SPI.h>
//Select one of the following to be 0, and the other 1
//'0': ON, '1': OFF
#define serial_display 1
#define processing_display 0
#define channelx 1
#define channely 2
PVision ircam;
byte result;
// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
int digpot_x = 500;
int digpot_y = 500;
}
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
// initialize SPI:
SPI.begin();
//MUX testing
int S0 = 5;
int S1 = 6;
void setup()
{
Serial.begin(115200);
ircam.init();
// while (Serial.available() <= 0) {
//Serial.print("AE v1.0"); // send a capital A
// delay(300);
//}
}
void loop()
{
result = ircam.read();
if(serial_display == 1){
if (result & BLOB1)
{
digpot_y = ircam.Blob1.X >> 2;
digpot_x = ircam.Blob1.Y >> 2;
Serial.print("BLOB1 detected. X:");
Serial.print(digpot_x);
Serial.print(" Y:");
Serial.print(digpot_y);
Serial.print(" Size:");
Serial.println(ircam.Blob1.Size);
}
if (result & BLOB2)
{
Serial.print("BLOB2 detected. X:");
Serial.print(ircam.Blob2.X >> 2);
Serial.print(" Y:");
Serial.print(ircam.Blob2.Y >> 2);
Serial.print(" Size:");
Serial.println(ircam.Blob2.Size);
}
if (result & BLOB3)
{
Serial.print("BLOB3 detected. X:");
Serial.print(ircam.Blob3.X >> 2);
Serial.print(" Y:");
Serial.print(ircam.Blob3.Y >> 2);
Serial.print(" Size:");
Serial.println(ircam.Blob3.Size);
}
if (result & BLOB4)
{
Serial.print("BLOB4 detected. X:");
Serial.print(ircam.Blob4.X >> 2);
Serial.print(" Y:");
Serial.print(ircam.Blob4.Y >> 2);
Serial.print(" Size:");
Serial.println(ircam.Blob4.Size);
}
digitalPotWrite (channelx,(int)attX);
digitalPotWrite (channely,(int)attY);
}
int digitalPotWrite(int address, int value) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
}
}//endif serial_display
else if(processing_display == 1){
if( Serial.available() > 0){
// print header
uint8_t XL1 = ircam.Blob1.X & 0xFF;
uint8_t XH1 = (ircam.Blob1.X >> 8) & 0x03;
uint8_t YL1 = ircam.Blob1.Y & 0xFF;
uint8_t YH1 = (ircam.Blob1.Y >> 8) & 0x03;
uint8_t XL2 = ircam.Blob2.X & 0xFF;
uint8_t XH2 = (ircam.Blob2.X >> 8) & 0x03;
uint8_t YL2 = ircam.Blob2.Y & 0xFF;
uint8_t YH2 = (ircam.Blob2.Y >> 8) & 0x03;
Serial.write(XL1);
Serial.write(XH1);
Serial.write(YL1);
Serial.write(YH1);
Serial.write(XL2);
Serial.write(XH2);
Serial.write(YL2);
Serial.write(YH2);
}//end ifSerial.available
}//endif processing_display
}
/*
Processing code for 2-blob output
import processing.serial.*;
int bgcolor = 250;
int fill_color = 255; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[8]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos_1, xpos_2, ypos_1, ypos_2; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
float angle, hyp;
int last_update = 0;
int this_update = 0;
//Blob data
int ycoord_max = 747;
int xcoord_max = 1023;
int[] xcoord = new int[4];
int[] ycoord = new int[4];
void setup() {
size(1024, 750); // Stage size
noStroke(); // No border on the next thing drawn
frameRate(400);
// Set the starting position of the ball (middle of the stage)
xpos_1 = width/2 - width/4;
ypos_1 = height/2 - width/4;
xpos_2 = width/2 + width/4;
ypos_2 = height/2 + width/4;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, "COM4", 115200);
}
void draw() {
background(bgcolor);
fill(0);
// Draw the shape
if(xcoord[0] < xcoord_max && ycoord[0] < ycoord_max && xcoord[1] < xcoord_max && ycoord[1] < ycoord_max){
//hyp = sqrt( sq(xcoord[1] - xcoord[0]) + sq(ycoord[1] - ycoord[0]));
// angle = degrees( acos((xcoord[1]-xcoord[0])/hyp));
strokeWeight(2);
stroke(255,10,10);
line(xpos_1, ypos_1, xpos_2, ypos_2);
stroke(0);
line(xpos_1, ypos_1, xpos_2, ypos_1);
stroke(0);
line(xpos_2, ypos_1, xpos_2, ypos_2);
}
else{
angle = 0;
}
ellipse(xpos_1, ypos_1, 30, 30);
ellipse(xpos_2, ypos_2, 30, 30);
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 7 ) {
//Parse incoming data
xcoord[0] = serialInArray[0] + (serialInArray[1] << 8);
ycoord[0] = serialInArray[2] + (serialInArray[3] << 8);
xcoord[1] = serialInArray[4] + (serialInArray[5] << 8);
ycoord[1] = serialInArray[6] + (serialInArray[7] << 8);
//Update coordinate data
xpos_1 = xcoord[0]*width/xcoord_max;
ypos_1 = ycoord[0]*height/ycoord_max;
xpos_2 = xcoord[1]*width/xcoord_max;
ypos_2 = ycoord[1]*height/ycoord_max;
// print the values (for debugging purposes only):
// println(xcoord[0] + "\t" + ycoord[0] + "\t" + xcoord[1]+"\t" + ycoord[1] + "\tangle: " + angle + "\tRate: " + (this_update - last_update));
// Send a capital A to request new sensor readi
// myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
*/