Connection to Processing not working

Hi!

I can turn on the led light on the Arduino with the 'button' example from Arduino, but can't get the connection to Processing right.

Arduino gets values from the switch, but Processing doesn't. This method worked with other buttons, why not with this switch? Are my cables correct?

Arduino code:

// variables will change:
int buttonState = 0; 
int inByte = 0;// variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:

  // initialize the pushbutton pin as an input:
  pinMode(2, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
 // get incoming byte:
 inByte = Serial.read();
  // read the state of the pushbutton value:
  buttonState = digitalRead(2);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  Serial.write(buttonState);
}
}

Processing code:

import processing.serial.*;

Serial myPort;
int[] serialInArray = new int[2];
int serialCount = 0;
boolean firstContact = false;

int waarde;
int HIGH;

void setup() {
  size(displayWidth, displayHeight);

  myPort = new Serial(this, "COM4", 9600);

  frameRate(12);
}

void draw() {
  if( serialInArray[0] == 1){
    ellipse(200,200,200,200);
  }
}


void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();   // clear the serial port buffer
      firstContact = true;  // you've had first contact from the microcontroller
      myPort.write('A');  // ask for more
    }
  } else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;
    // If we have 3 bytes:
    if (serialCount > 0 ) {
      waarde = serialInArray[0];
     
      // print the values (for debugging purposes only):
      println(waarde+"t");


      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

And here are my cables:

Thanks for any help or thoughts!

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

1 Like

Sorry! Will pay attention to it next time

I'm not familiar with Processing. What is the symptom? Does it not print waarde?

Note:
Het is zo'n 20 jaar geleden dat ik een fatsoenlijk brrodje kroket gegeten heb :frowning: Mis het :cry:

Ik ook. :pensive:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.