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
}
}
}