Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Integration >> Activating digital ports with processing
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1200949471

Message started by hype1 on 01/21/08 at 21:04:30

Title: Activating digital ports with processing
Post by hype1 on 01/21/08 at 21:04:30
Hi,

While looking at the example for serial_inputfor Processing, I wanted to activate not one LED, but multiple LED's on multiple ports.

I am trying to activate multiple LED's at once, connected to ports 0-7 on my Wiring-board. The code below seems to work, but there is a problem. The led's on pin 2 and 6 are blinking real fast, while that on port 1 is more (but not perfect) lightened continuous.

I have looked everywhere on this site, but I can't find an example. Basically I want Wiring to open up several ports for digital output, and Processing to activate one or more of those ports.

Can anyone give me some help please?

Regards,

Jeroen

(part of) processing code:

Code:
void draw()
{
 background(#222222);
 if(mouseOverRect())      // if mouse is over square
 {
   fill(#BBBBB0);         // change color
   port.write(int(2));    // activate multiple ports
   port.write(int(6));    
   port.write(int(1));
 } else {
   fill(#666660);         // change color
   port.write(int(3));       // activate other port
 }
 rect(50, 50, 100, 100);  // draw square
}


Wiring code:


Code:
int val;         // variable to receive data from the serial port

void setup()
{
 int i;
 for(i=0; i<8; i++)           // initializes pins 0 to 7 as outputs
 {
   pinMode(i, OUTPUT);  
 }
 Serial.begin(9600);       // start serial communication at 9600bps
}

void turn_all_off()            // function to turn off all the lights
{                              // connected to digital pins 0 to 7
 int i;
 for(i=0; i<8; i++)
 {
   digitalWrite(i, LOW);
 }
}

void loop() {
 turn_all_off();
 if( Serial.available() )       // if data is available to read
 {
   val = Serial.read();       // read it and store it in 'val'
 }
 int i;
 for (i=0; i<8; i++){        // check for each port if val (number) is same
   if( val == i ) {            
     digitalWrite(val, HIGH);  // turn ON the LED
   }
 }
}

Title: Re: Activating digital ports with processing
Post by Titus on 01/22/08 at 16:04:27
well, instead of using a "void void turn_all_off()"
you could use an int, like this:

int leds_off (int State){

if (state==1){

digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}
else
{
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}


and use the leds_off(//0 or 1);
to turn them off or on.

Hope this works

Titus

Title: Re: Activating digital ports with processing
Post by hype1 on 01/22/08 at 19:27:04
Well, where I want to go is that processing should be able to control each port independly. In some cases one, in some cases more LED's have to turn on. Basically what I need Processing to tell Wiring is: turn on port (x,x,x,x).

Your solution won't help me with that I guess. Thanks anyway ;) Have you got any other thoughts on this?

Regards,

Jeroen

Title: Re: Activating digital ports with processing
Post by hype1 on 01/22/08 at 19:53:18
I think I got it now. I was turning all led's off and on in the loop. I had to built a function in Processing that tell's Wiring to turn all led's off only when changing the led's which have to be turned on.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.