Loading...
Logo
Processing Forum
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);
  }
}

________________________________________

Replies(2)

I don't have an Arduino hooked up so I commented out some of the code. I replaced reading with a transformation of cos(countingNumber) to show the rect()s working. Hopefully this is what you are looking for:
Copy code
  1. int moduleCount;
  2. int moduleCount_ = 50;
  3. int count;
  4. int reading;
  5. int i;

  6. //Arduino arduino;
  7. Module [] moduleCollection = new Module[moduleCount_];

  8. void setup() {
  9.   size(600, 600);
  10.   //Locate Arduino Port
  11.   //println("COM Ports");
  12.   //println(Arduino.list());
  13.   //println("=========");
  14.   //arduino = new Arduino(this, Arduino.list()[5], 57600);
  15.   for (int i = 0; i<moduleCount_; i++) {
  16.     moduleCollection[i]=new Module(width/2, height*0.9-i*10, random(100, 200));
  17.   }
  18. }

  19. void loop() {
  20. }

  21. float mimicReadingCount = 0;
  22. void draw() {
  23.   background(255);
  24.   //Read from Arduino
  25.   //reading=arduino.analogRead(0);
  26.   
  27.   reading = (int)((cos(mimicReadingCount)+1)*512);
  28.   mimicReadingCount += 0.01;
  29.   println(reading);
  30.   
  31.   //Map resistance value to Count
  32.   float count = map(reading, 0, 1023, 1, 50);
  33.   moduleCount=int(count);
  34.   //println(moduleCount);
  35.   for (int i = 0; i < moduleCount_; i++) {
  36.     moduleCollection[i].display();
  37.   }
  38.   moduleCount_=moduleCount;
  39. }

  40. class Module {
  41.   //GLOBALS VALUES
  42.   float xpos, ypos; //xy position
  43.   float cFill;

  44.   Module(float xpos_, float ypos_, float cFill_) {
  45.     xpos = xpos_;
  46.     ypos = ypos_;
  47.     cFill=cFill_;
  48.   }
  49.   //Drawing of Modules
  50.   void display() {
  51.     rectMode(CENTER);
  52.     fill(cFill);
  53.     rect(xpos, ypos, 10, 10);
  54.   }
  55. }
Thanks! asimes! You are legend!

Now i need to get the squares to expand in both x and y directions randomly, but attaching to each other.