We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Making a traffic light with a random function
Page Index Toggle Pages: 1
Making a traffic light with a random function (Read 791 times)
Making a traffic light with a random function
Jun 29th, 2009, 5:57am
 
Hi everybody,

I really need some help. For a school assignment I have to make a sort of trafficlight with processing. It has to be a trafficlight with only red and green and a random function has to deside every 2 seconds which of those two colors the trafficlight will be. I only have a little experience with processing, but not much. It sounds so easy to make, but I just can't figure it out. I've been working on this for like 3 days and I still don't got it. Please could someone help me?

Laura
Re: Making a traffic light with a random function
Reply #1 - Jun 29th, 2009, 6:28am
 
Well I'm not sure anyone here is going to feel comfortable just giving you the answers for your assignment, especially when this is a pretty basic assignment, and everything you need is easily found in the reference. Instead, how about you put in the code that you have so far and tell us what you're not getting, and we'll help point you in the right direction and give you some hints.
Re: Making a traffic light with a random function
Reply #2 - Jun 29th, 2009, 6:51am
 
I know, you are totally right. It's just that I'm sort of desperate to find the answer after 3 days of trying and searching. This is what I have so far. In this code, I tried something different than the random function (that somehow just didn't work) Instead of that I used this:

int r;
int g;
int getal = 0;
int frameratecounter = 2;


void setup() {
 size(300, 300);
 r = color(255, 0, 0);
 g = color(0, 255, 0);
 frameRate(1);
}

void draw() {

 if (frameratecounter == 2){
   background (0,0,0);
   getal = int(random(1,3));
   println(getal);
   if (getal == 1) {
     
   
     ellipse(25,25,50,50);
     fill(r);
     }
     
   else {
     fill(g);
     ellipse(25,25,50,50);
   
     }
    frameratecounter = 1;
 }

 else{
   frameratecounter++;
 
 }  
}

By counting the frames, with only one frame per second, you are actually counting the seconds I figured. The problem is that this "traffic light" has to be put in a game which I made with processing, using my webcam. With such a low framerate, the video from my webcam is very slow too. So I don't know what to do next. I want to use the random function, and I know it is a vert basic thing, but I just can't figure it out. If you would have some tips for me, I would be very greatfull. Thanks in advance!
Re: Making a traffic light with a random function
Reply #3 - Jun 29th, 2009, 6:56am
 
By the way, this is another assignment I had to make.

int r;
int g;
int b;

void setup() {
  size(200,200);
  stroke(0,0,0);
  strokeWeight(5);
  background(255,255,255);
  frameRate(10);
}

void draw() {
fill(255, random(255), random(255));
 rect(25, 25, 100, 25);
 
 fill(random(255), 255, random(255));
 rect(25, 75, 100, 25);
 
 fill(random(255), random(255), 255);
 rect(25, 125, 100, 25);
}

void keyPressed(){
 r = int(random(255));
 g = int(random(255));
 b = int(random(255));
 background(r,g,b);
 println("De waarde voor rood is "+r);
 println("De waarde voor groen is "+g);
 println("De waarde voor blauw is "+b);
}

In this code, I'm using the random function as well, but I don't know how to apply this in the assignment I described to you. It's different, cause in the new assignment it's eather this (green) ot that (red) and nothing in between.
Re: Making a traffic light with a random function
Reply #4 - Jun 29th, 2009, 7:06am
 
    ellipse(25,25,50,50);
    fill(r);

the fill has so come first so it knows which colour to draw the ellipse

as for the framerate if you make the framerate 25 fps then you just have to change the colour every 50 frames. it's pretty much what you're doing at the moment, you're just counting higher.
Re: Making a traffic light with a random function
Reply #5 - Jun 29th, 2009, 7:11am
 
A useful hint: look at % (modulo) operator, it is extremely useful in Processing!
You can use it against frameCount, for example.

Now, a classical way to measure time in Processing is to use the millis() function, with the advantage of being independent of frameRate.
You set a startTime with it (in setup and at end of event), and when millis() - startTime is over a bound (2000 for 2s - check for being above, not equal), it is time to do something and reset the startTime.
Likewise, use random(2) and check if > 1 for example.

I hope this pushes you in the right direction. Don't hesitate to ask if you are blocked elsewhere.
Page Index Toggle Pages: 1