Processing and Division by zero

I'm working on an Arduino/Processing project. I have this line of code many time throughout the processing part:

millis()/3 % width

So I want to set it to a variable:

time_overflow = millis()/3 % width

When I try to compile, I get a divide by zero error, but no line is mentioned. If I take out that line and replace the variables with their values, everything works. How am I dividing by zero here?

Thanks!
baum

if width has a const value, or one that can be derived compile time it can happen, but I need to see the whole code to be true.

You could try to minimize the code in which this happens, to get focus.

Rob

width is just the width of the window, set @ 1000. Because of the problem, I took out most of the instances, but here's the current code.

It's a random number graph.

int time = 2 ; //lower is faster
int lastX;
int lastY;
int newX;
int newY;
int low;
int high;

void setup() {
  size(1000, 500);
  background(0);
  lastY = height/2;
  lastX = 0;
  low = height / 8;
  high = height - (height / 8);
}

void draw() {
  stroke(255);
  newX = (millis()/time) % width; //I want to make this a variable, if I try, I get /0
  if(newX >= 990 || newX <= 10) {
    background(0);
    stroke(0);
  } else {
    stroke(255);
  }
  newY = int(random(low, high));
  line(lastX, lastY, newX, newY);
  lastX = newX;
  lastY = newY;
}

/ an % have the same precedence (level 5) and evaluates from left to right -> C++ Operator Precedence - cppreference.com -

does this give the same error?

void draw() {
  newX = millis()/3  % width; 
 }

Your complete program works. So what is the problem again? :roll_eyes:

Your complete program works. So what is the problem again?

Understanding why it happened

The program I posted works*.... I just want to make the noted line into a variable.

*Sorry, forgot to explain. When assigning the value to newX, everything works. But not if I try to set it outside all functions, as a global variable.

This does not work (and is what I want):

int time = 3 ; //lower is faster
int lastX;
int lastY;
int newX = millis()/time % width; //assigned here
int newY;
int low;
int high;


void setup() {
  size(1000, 500);
  background(0);
  lastY = height/2;
  lastX = 0;
  low = height / 8;
  high = height - (height / 8);
}

void draw() {
  stroke(255);
  if(newX >= 990 || newX <= 10) {
    background(0);
    stroke(0);
  } else {
    stroke(255);
  }
  newY = int(random(low, high));
  line(lastX, lastY, newX, newY);
  lastX = newX;
  lastY = newY;
}
int newX = millis()/time % width; //assigned here

How about adding parentheses?

int newX = (millis()/time) % width; //assigned here

Nope. Is it something to do with the fact that the program hasn't "started" outside of a function and therefore has no value assigned to millis()?

baum, now you're talking real problems. Before you were not clear.

The variable "width" is not defined outside of the loops etc. It is initialized by the line inside the setup() size(1000, 500). The line essentially creates a drawing canvas with the size specified. Try putting your problematic line before and after the size() function and see how it compiles, before, no go, after, yes. That's your problem. 8)

I was using Processing regularly a while back, will be next semester when I teach optics again. I've got some experience with it, worth maybe 20,000 lines of code. This question belongs to interfacing with software board BTW :wink:

This compiles:

/* Grapher
 * 
 * Graphs changing values, resetting after graph reaches edge.
 * 
 */
 
// Define Y-axis variable. 
//int YAXIS = int(random(low, high));

/* Define speed. 1 is the fastest, and is equal to a speed of drawing one graph 
 * in the width in milliseconds.
 */
int SPEED = 5;

int lastX, lastY, newY, low, high;
int newX;


void setup() {
  newX =  millis()/SPEED % width; //<------ before size!
  size(1900,1000);
  background(0);
  lastY = height/2;
  lastX = 0;
  low = height / 8;
  high = height - (height / 8);
}

void draw() {
  stroke(255);
  if(newX >= width-50 || newX <= 50) {
    background(0);
    stroke(0);
  } else {
    stroke(255);
  }
  newY = int(random(low, high));
  line(lastX, lastY, newX, newY);
  lastX = newX;
  lastY = newY;
}

It doesn't do anything, but it compiles. I ONLY get the error when it is outside any functions.

And maybe an admin can move this topic...?

Can I do something like #define? This would not have any problems, as it is not evaluated, only substituted, but it is only for C.

Looks like at least you know the source of the problem is using width before it is assigned a value XD

No you can't use any precompiler directives in java. You can on the other hand make some class with static member variables. But, as for processing, the entire thing is wrapped in a class so static member variables may not work.

ok. thx.

From your code in #2 of this thread:

Declare the global variable.

Calculate the new value each time through the loop (as you do with newY, for example).

// A processing sketch to graph some "random" stuff.
// Should be in the "Processing" forum???

int time = 3 ; //lower is faster
int lastX;
int lastY;
int newX; // Declare it here
int newY;
int low;
int high;


void setup() {
  size(1000, 500);
  background(0);
  lastY = height/2;
  lastX = 0;
  low = height / 8;
  high = height - (height / 8);
}

void draw() {
  stroke(255);
  newX = (millis() / time) % width; // New value each time through the loop
  if(newX >= 990 || newX <= 10) {
    background(0);
    stroke(0);
  } else {
    stroke(255);
  }
  newY = int(random(low, high));
  line(lastX, lastY, newX, newY);
  lastX = newX;
  lastY = newY;
}

Regards,

Dave

That's what I had. But I wanted a way in which I could "define" the Y-axis variable at the top of the code, allowing users to changing "settings" such as these without navigating through the rest of the code.

baum:
That's what I had. But I wanted a way in which I could "define" the Y-axis variable at the top of the code, allowing users to changing "settings" such as these without navigating through the rest of the code.

Are you talking about making it a function???

int time = 2 ;
int lastX;
int lastY;
int newX;
int newY;
int low;
int high;

int nextX()
{
  return (millis()/time) % width;
}


void setup() {
  size(1000, 500);
  background(0);
  lastY = height/2;
  lastX = 0;
  low = height / 8;
  high = height - (height / 8);
}

void draw() {
  stroke(255);
  newX = nextX(); // Function call to get value each time through the loop
  if(newX >= 990 || newX <= 10) {
    background(0);
    stroke(0);
  } else {
    stroke(255);
  }
  newY = int(random(low, high));
  line(lastX, lastY, newX, newY);
  lastX = newX;
  lastY = newY;
}

Regards,

Dave

Yes... That looks promising. Because this way, the user could easily define the Y-axis value. (Yes, I made a mistake. I meant to be talking about the Y-axis, but I can still use all this)

Thanks a lot!
baum