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?
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
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.
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;
}
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;
}
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)