Array function with variable Count
in
Integration and Hardware
•
11 months ago
Hi All! I am new to processing.
I am try to control the number of squares array displayed on the screen by using a resister value from Arduino. But i cannot get it working!
When the moduleCount is 0, is fine, when it is more than 0, then it has problem displaying/drawing the square.
__________________________________
import processing.serial.*;
import cc.arduino.*;
int moduleCount;
int moduleCount_;
int count;
int reading;
int i;
Arduino arduino;
Module [] moduleCollection = new Module[moduleCount_];
void setup() {
size(600, 600);
//Locate Arduino Port
println("COM Ports");
println(Arduino.list());
println("=========");
arduino = new Arduino(this, Arduino.list()[5], 57600);
for (int i = 0; i<moduleCount_; i++) {
moduleCollection[i]=new Module(width/2, height*0.9-i*10, random(100, 200));
}
}
void loop() {
}
void draw() {
background(255);
//Read from Arduino
reading=arduino.analogRead(0);
//Map resistance value to Count
float count = map(reading, 0, 1023, 1, 50);
moduleCount=int(count);
println(moduleCount);
for (int i = 0; i < moduleCount_; i++) {
moduleCollection[i].display();
}
moduleCount_=moduleCount;
}
class Module {
//GLOBALS VALUES
float xpos, ypos; //xy position
float cFill;
Module(float xpos_, float ypos_, float cFill_) {
xpos = xpos_;
ypos = ypos_;
cFill=cFill_;
}
//Drawing of Modules
void display() {
rectMode(CENTER);
fill(cFill);
rect(xpos, ypos, 10, 10);
}
}
________________________________________
1