turn on leds using processing

Hi guys, after look for some solutions I come here without answers I'd like to have.

I have a sensor to interact with some leds and I am reading the data of it through processing.

The problem is for processing recognize the led terminals as outputs.

I think I need to add or download some library but I am not right if I already have.

here the code:

import processing.serial.*;
//10 leds in total
int pin1 = 2, pin2 = 3, pin3 = 4, pin4 = 5, pin5 = 6;
int pin6 = 7, pin7 = 8, pin8 = 9, pin9 = 10, pin10 = 11;

Serial port;

void setup()
{
  port = new Serial(this, Serial.list()[0], 115200);
  
  port.pinMode(pin1, Serial.OUTPUT);
  port.pinMode(pin2, Serial.OUTPUT);
  port.pinMode(pin3, Serial.OUTPUT);
  port.pinMode(pin4, Serial.OUTPUT);
  port.pinMode(pin5, Serial.OUTPUT);
  port.pinMode(pin6, Serial.OUTPUT);
  port.pinMode(pin7, Serial.OUTPUT);
  port.pinMode(pin8, Serial.OUTPUT);
  port.pinMode(pin9, Serial.OUTPUT);
  port.pinMode(pin10, Serial.OUTPUT);
  
}

...

the error:

Cannot find anything named "Serial.OUTPUT"

I'm sorry... what?

I'm lost... you want to write java on the PC that directly manipulates the IO ports of the Arduino...?

And how do you plan to do that?

I'm a beginner...

I saw this example in the site and tried to follow...

Example

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int ledPin = 13;

void setup()
{
  //println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
}

void draw()
{
  arduino.digitalWrite(ledPin, Arduino.HIGH);
  delay(1000);
  arduino.digitalWrite(ledPin, Arduino.LOW);
  delay(1000);
}

Hi Francisco

Are you using the Processing language on the PC? And you want to control Arduino pins from Processing?

Look at Firmata - it may help you do what you want.

Regards

Ray

I saw this example in the site and tried to follow...

That example creates an instance of the Arduino class, not the Serial class. They are not interchangeable.

This may be a better link to start with, Francisco.

http://playground.arduino.cc/Interfacing/Processing

Francisco can you explain better what are you trying to do? If you want do something with LEDs reading values from a sensor, why do you need for communication with a PC?

I am trying to change things radically here.

Actually I just want get the data from the sensor and turn leds on or off by so. I want to use processing as well.

Now what I have is this:

The sensor is working fine in the processing but leds don’t change. I bet I am doing something really wrong. Just don’t know what and how to fix it.

arduino code:

  int pin1 = 2, pin2 = 3, pin3 = 4, pin4 = 5, pin5 = 6;
  int pin6 = 7, pin7 = 8, pin8 = 9, pin9 = 10, pin10 = 11; 

void setup()
{
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(pin8, OUTPUT);
  pinMode(pin9, OUTPUT);
  pinMode(pin10, OUTPUT);
  
}

void loop()
{
   int data = Serial.read();
      
  if (data < 24)
    digitalWrite(pin1, HIGH);
    else
      digitalWrite(pin1, LOW);
      
  if (data < 48)
    digitalWrite(pin2, HIGH);
    else
      digitalWrite(pin2, LOW);
      
  if (data < 72)
    digitalWrite(pin3, HIGH);
    else
      digitalWrite(pin3, LOW);
      
  if (data < 96)
    digitalWrite(pin4, HIGH);
    else
      digitalWrite(pin4, LOW);
      
  if (data < 120)
    digitalWrite(pin5, HIGH);
    else
      digitalWrite(pin5, LOW);
      
  if (data < 144)
    digitalWrite(pin6, HIGH);
    else
      digitalWrite(pin6, LOW);
      
  if (data < 168)
    digitalWrite(pin7, HIGH);
    else
      digitalWrite(pin7, LOW);
      
  if (data < 192)
    digitalWrite(pin8, HIGH);
    else
      digitalWrite(pin8, LOW);
      
  if (data < 216)
    digitalWrite(pin9, HIGH);
    else
      digitalWrite(pin9, LOW);
      
  if (data < 240)
    digitalWrite(pin10, HIGH);
    else
      digitalWrite(pin10, LOW);
}

Processing code:

import processing.serial.*;

int val = 0; // To store data from serial port, used to color background

Serial port; // The serial port object

void setup()
{
  size(800,400);
  port = new Serial(this, Serial.list()[0], 115200);
}

void draw()
{
  // The serial data is used to color the background. 
 background(val); 
}

void serialEvent(Serial Port) {
  // read a byte from the serial port:
  int inByte = Port.read();
  val = inByte;

  // print the values (for debugging purposes only):
  println("Output:  " + val);
  port.write(val);
}
int data = digitalRead(0);

One time, read the status of one of the hardware serial pins…

void setup()
{
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(pin8, OUTPUT);
  pinMode(pin9, OUTPUT);
  pinMode(pin10, OUTPUT);
}

Then, don’t bother opening the serial port. How do you expect Processing to receive any data? Where do you (try to) send any data?

  if (data < 24)

The value read from a digital pin is either 0 or 1.

The sensor is working fine in the processing

What sensor is working fine in Processing? You can’t connect a sensor to the PC, so Processing can’t (and doesn’t in that code) read any sensor.

Thanks.

I changed the way I will collect the data by

void loop()
{
    int data = Serial.read();
    
    if (data < 24)
        digitalWrite(pin1, HIGH);
...

The sensor of light in the processor changes the color of the 800x400 screen and gives me output between 0 and 255 as
the commands request. I would like to pass this data to arduino control the leds…

Also, the leds are always turned on.

Francisco

Can you post a diagram of how all the hardware (including PC, Arduino, sensor and LEDs are connected)?

You could draw one by hand and take a picture of it.

Thanks

Ray

Sure, here is the pictures:

I changed the way I will collect the data by

You need to post ALL of your code.

If you don't check for serial data, and there is nothing to read, you will get -1 in data. It seems to be you want to turn the pin off if the value is below the threshold, and on if it is above the threshold.

Ops, I am sorry…

Here is the entire code:

Code in Arduino:

 int pin1 = 2, pin2 = 3, pin3 = 4, pin4 = 5, pin5 = 6;
  int pin6 = 7, pin7 = 8, pin8 = 9, pin9 = 10, pin10 = 11; 

void setup()
{
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(pin8, OUTPUT);
  pinMode(pin9, OUTPUT);
  pinMode(pin10, OUTPUT);
  
}

void loop()
{
   int data = Serial.read();
      
  if (data < 24 && data > 1)
    digitalWrite(pin1, LOW);
    else
      digitalWrite(pin1, HIGH);
      
  if (data < 48)
    digitalWrite(pin2, LOW);
    else
      digitalWrite(pin2, HIGH);
      
  if (data < 72)
    digitalWrite(pin3, LOW);
    else
      digitalWrite(pin3, HIGH);
      
  if (data < 96)
    digitalWrite(pin4, LOW);
    else
      digitalWrite(pin4, HIGH);
      
  if (data < 120)
    digitalWrite(pin5, LOW);
    else
      digitalWrite(pin5, HIGH);
      
  if (data < 144)
    digitalWrite(pin6, LOW);
    else
      digitalWrite(pin6, HIGH);
      
  if (data < 168)
    digitalWrite(pin7, LOW);
    else
      digitalWrite(pin7, HIGH);
      
  if (data < 192)
    digitalWrite(pin8, LOW);
    else
      digitalWrite(pin8, HIGH);
      
  if (data < 216)
    digitalWrite(pin9, LOW);
    else
      digitalWrite(pin9, HIGH);
      
  if (data < 240)
    digitalWrite(pin10, LOW);
    else
      digitalWrite(pin10, HIGH);
}

Code in Processing:

import processing.serial.*;

int val = 0; // To store data from serial port, used to color background

Serial port; // The serial port object

void setup()
{
  size(800,400);
  port = new Serial(this, Serial.list()[0], 115200);
}

void draw()
{
  // The serial data is used to color the background. 
 background(val); 
}

void serialEvent(Serial Port) {
  // read a byte from the serial port:
  int inByte = Port.read();
  val = inByte;

  //print the values (for debugging purposes only):
  println("Output:  " + val);
  Port.write(val); 
}

I can see the data of the sensor in a screen in Processing. When my hand is near: 220, when it is far: 28.

Only LED 1 is high in this one.

You can’t call Serial.read() unless you have called Serial.begin(). Well, not meaningfully, anyway.

I don't know why, but if I use the Serial.begin() the sensor will only works if I press the reset button and the leds don't work too :frowning:

but if I use the Serial.begin() the sensor will only works if I press the reset button

What sensor are you talking about? The Arduino doesn't appear to have a sensor attached, and Processing CAN NOT have sensors attached.

Pressing the reset button resets the Arduino, and runs setup() again, where you should be calling Serial.begin(), so needing to press reset doesn't make sense.

Francisco

Your photo does not show the sensor, but I think there are wires going to it. Please give more details of sensor.

Also, you have sensor connected to Arduino pin 1? This will conflict with the serial monitor. Try moving the sensor to another pin and change the pin number in your program to match.

Ray

Pressing the reset button resets the Arduino, and runs setup() again, where you should be calling Serial.begin(), so needing to press reset doesn't make sense.

Yeah, I just don't know why that happens. (pressing reset button to sensor works in Processing).

What sensor are you talking about? The Arduino doesn't appear to have a sensor attached, and Processing CAN NOT have sensors attached.

Your photo does not show the sensor, but I think there are wires going to it. Please give more details of sensor.

In the pictures I sent, the sensor is the piece of hardware with a yellow wire attached to the arduino. I don't have any information about the sensor because it is a new hardware that the company I am working for is developing.