|
|
|
Below please find our Arduino code:
int val[16];
int analog1[5];
int h, j;
long average1;
int analog2[5];
int p, q;
long average2;
int a= 3; // SELECT PIN A0 GOING INTO PIN 2
int b= 4; // SELECT PIN A1 GOING INTO PIN 3
int c= 5; // SELECT PIN A2 GOING INTO PIN 4
int d= 6; // SELECT PIN A3 GOING INTO PIN 5
//we are using three analog pins
int analog0 = 0;
//int analog1 = 0;
//int analog2 = 0;
void setup() {
Serial.begin(9600);
pinMode(analog0, INPUT);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
//pinMode(analog1, INPUT);
//pinMode(analog2, INPUT);
}
void loop() {
for (int i = 0; i < 16; i++) {
// determine the four address pin values from i:
// mask off bit 0:
int pinOne = 1 & i;
// shift value 1 bit to the right, and mask all but bit 0:
int pinTwo = 1 & (i >> 1) ;
// shift value 2 bits to the right, and mask all but bit 0:
int pinThree = 1 & (i >> 2);
// shift value 3 bits to the right, and mask all but bit 0:
int pinFour = 1 & (i >> 3);
// set the address pins:
digitalWrite(a,pinOne);
digitalWrite(b,pinTwo);
digitalWrite(c,pinThree);
digitalWrite(d,pinFour);
// read the analog input and store it in the value array:
val[i] = analogRead(analog0);
//val[i] = i;
Serial.print(val[i], DEC);
if (i < 15) {
Serial.print(”,”);
//For IR 1
analog1[(h++ %5)] = analogRead(1);
average1 = 0;
for (j =0; j<5; j ++){
// add samples
average1 += analog1[j];
}
average1 = average1 / 5;
//For IR 2
analog2[(p++ %5)] = analogRead(2);
average2 = 0;
for (q =0; q<5; q ++){
// add samples
average2 += analog2[q];
}
average2 = average2 / 5;
//End Averaging
}
}
Serial.print(”*”);
Serial.print(average1, DEC);
Serial.print(”,”);
Serial.println(average2, DEC);
}
//END CODE
|
|
|
|
|
Here is our current Processing
code, which is still in development:
import processing.serial.*;
Serial myPort;
//Declare your main string5
String inString;
boolean gotFirstString = false;
int cols = 40; //# of columns
int rows = 40; //# of rows
int videoScale = width/cols; //resolution of the overall picture grid - how
wide a "bigPixel" is
int[] vals = new int[16]; //Arduino array of gray values coming in serially
int irOne; //IR sensor value for X
int irTwo; //IR sensor value for Y
int []pots; //the string leftover, what will contain IR x,y values
Cell[][] PicPixArray; //2D array of objects -- the overall grid of our image
plane
Cell[][] cursorArray; //2D array of objects -- the "cursor" array
int gridScale; //resolution of the overall picture grid - how wide a "bigPixel"
is
int gridCols = 4; //Number of columns in the grid
int gridRows = 4; //Number of rows in the grid
//int grayGridMasterValue = 5; //for NON-SERIAL COMMUN
//PImage img;
void setup() {
size (800, 800);
background(255);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
//set a buffer - serialEvent will not happen until \n (new line feed)
//so serialEvent will always start on the first of our values
myPort.bufferUntil('\n'); // SERIAL COMMUNICATION
gridScale = width/cols;
cursorArray = new Cell[gridCols][gridRows];
//this is where we initialize the cursorArray a.k.a "cursor" or
"photo sensors array"
for (int i = 0; i < gridCols; i++) {
for (int j = 0; j < gridRows; j++) {
//intialize each object
//int locLinearArray = i+j*4;
cursorArray [i][j] = new Cell(i*gridScale,j*gridScale,gridScale,gridScale,-1);//values[locLinearArray]);
//cursorArray[i][j].getArduinoValues();
//print(values[i] + "\t");
}
}
PicPixArray = new Cell[cols][rows];
//this is where we initialize the PicPixArray a.k.a "image plane"
or "the big picture"
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int x = i*gridScale; //x position
int y = j*gridScale; //y position
int tempbigPixelGray = 150;
PicPixArray [i][j] = new Cell(x, y, gridScale, gridScale, tempbigPixelGray);
//PicPixArray [i][j].fillinPicPix();}
//img = createImage(width,height,RGB);
}//failed attempts / other ideas below
// PicPixArray[x][y] = values[(x*4) + y];
//PicPixArray [values [16] / 10 + x][values [17] / 10 + y] = cursorArray [x][y];}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if(myString != null) {
myString = trim(myString);
//println(myString);
//split your longer string, using the asterisk as a delimiter, creating two
smaller strings
String[] strings = split(myString,'*');
if (strings.length > 1) {
//make an array of vals by splitting the first string wherever there is a
commma
//you know it's the first string cause it's called strings[0]
vals = int(split(strings[0], ','));
//do the same things for the pots, split the second string up
//wherever there is a comma
pots = int(split(strings[1], ','));
//irOne is a variable, whose value is going to be the first value from the
pots array
irOne = pots[0];
//irTwo is a variable, whose value is going to be the second value from the
pots array
irTwo = pots[1];
//same thing here with val0, etc... they will eventually be val0-val15, which
are variable
println(vals + "\t");
for(int i = 0; i < vals.length; i++){
vals[i] = vals[i]/4;
print(vals[i] + "\t");
}
println();
println("IRS");
println(irOne + "\t" + irTwo);
println();
//then print whichever cell values you would like to look at
println(vals[0] + "\t" + vals[1]);
}
}
}
//void keyPressed() {
void mousePressed() {
// img.save("test.jpg");
}
void draw() {
// background (0);
//irOne = mouseX; //change to include IR sensor value array value [16] - x
//irTwo = mouseY; //change to include IR sensor value array value [17] - y
//img.loadPixels();
noStroke();
for (int i = 0; i < gridCols; i++) {
for (int j = 0; j < gridRows; j++) {
int locLinearArray = i+j*4;
cursorArray [i][j].setArduinoValue(vals[locLinearArray]);
//cursorArray [i][j].setArduinoValue(150);
cursorArray [i][j].setPixel();
}
}
//img.updatePixels();
//image(img,0,0);
for (int i = 0; i < gridCols; i++) {
for (int j = 0; j < gridRows; j++) {
cursorArray [i][j].displayCursor();
}
}
/* for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
PicPixArray [i][j].display();
}
}*/
/*
for (int i = 0; i < gridCols; i++) {
for (int j = 0; j < gridRows; j++) {
int locLinearArrayCursor = i+j*4;
vals [i] = ((vals[locLinearArrayCursor])/4);
//this is the line that needs to be change to receive arduino bytes!
//vals[i] = int(random(255));//to test NON-SERIAL COMMUNICATION
// vals[i] = int(random(255)); //SERIAL COMMUNICATION
}
}*/
}
========
THE "CURSOR CELL" TAB//a cell object
class Cell {
float xcell,ycell; //x,y location
float w,h; //width and height
int grayGridValue; // gray value for fill for each cursor Cell//cell constructor
Cell(float tempxcell, float tempycell, float tempW, float tempH, int tempGrayGridValue)
{
xcell = tempxcell;
ycell = tempycell;
w = tempW;
h = tempH;
grayGridValue = tempGrayGridValue;
}void setArduinoValue(int temp) {
grayGridValue = temp;
}void setPixel() {
//rect(irOne + xcell, irTwo + ycell,gridScale,gridScale); //replace mouseX
with irOne value
/*int x = irOne + int(xcell)/gridScale;
int y = irTwo + int(ycell)/gridScale;
int loc = x+y*width; // NOT CORRECT
img.pixels[loc] = color(grayGridValue);*/PicPixArray[int(xcell+irOne)/gridScale][int(ycell+irTwo)/gridScale].setArduinoValue(grayGridValue);
}//void fillInPicPix() {
// //PicPixArray[i][j] = PicPixArray[irOne / videoScale + x][irTwo/ videoScale
+ y];
//void displayCursor () {
noStroke(); //color and move to mouse position
color c = grayGridValue;
fill(c);
rect(irOne + xcell, irTwo + ycell,gridScale,gridScale); //replace mouseX with
irOne value
}
void display () {
noStroke(); //color and move to mouse position
color c = grayGridValue;
fill(c);
rect(xcell,ycell,gridScale,gridScale); //replace mouseX with irOne value
}
}
|