Arduino to processing

Hello!

I am trying to send info from arduino to processing but my code is not working, it worked before! =[

Does the board need to be plugged in to work?
Can someone please explain whats wrong?

Processing Code

import processing.serial.*;

Serial port;
int val;

void setup(){
  size(200, 200);
  println(Serial.list());
  String arduinoPort = Serial.list()[0];
  port = new Serial(this, arduinoPort, 9600);
}

void draw()
{
  background(128);
  if(port.available()>0)
  {
    val = port.read();
    println(val);
    if(val == 1)
    {
      fill(255,0,0);
    }
    if(val == 0)
    {
      fill(0,255,0);
    }
    
  }
  ellipse(100, 100, 30,30);
}

Arduino code

int val;


void setup() {
  pinMode(6, INPUT);
  Serial.begin(9600);



}

void loop() {
  // put your main code here, to run repeatedly:
  val =  digitalRead(6);

  //Serial.println(val);
  Serial.write(val);

  delay(100);

}

Thank you!

1 Like

Your Arduino MUST be connected to the PC via the serial port which is how they (Processing and Arduino) communicate.

You do not have to plug it into DC power if you are getting enough power from the USB port.

Processing MUST connect to the correct serial port in the code.

This works on my PC with Arduino connected to COM7:

void setup(){
  size(200, 200);
  //println(Serial.list());
  printArray(Serial.list());
  
  //String arduinoPort = Serial.list()[2];
  String arduinoPort = "COM7";
  port = new Serial(this, arduinoPort, 9600);
}

My Arduino in on COM7.

I showed 2 ways to identify the COM port in the code; one is commented out.

:slight_smile:

2 Likes