Wiring multiple buttons from arduino to processing

Hello people,

I’m pretty new to Arduino and have a problem with my Arduino Uno:

The idea is to make a circuit that holds 6 buttons on it and that can talk to Processing so I can move a player around (as in RPG-style so also diagonal). I want it to have Up-Down-Left-Right and a combination of those would move the player diagonal.

Right now I have two buttons :

  • Down
  • Right

A combination of both will move the player.

For some reason, once I hook up a third button the controls start giving me combinations of all controls (I should only get one) and I have no idea what I’m doing wrong… (the upState variable is causing the problem of giving me combinations of all controls ~ which is the second button on my board)

My question now is: How do I make it so that Processing will understand that all the buttons on the board are unique and thus will not collide with other inputs?
Arduino code:

const int led = 13;
const int right = 2;
const int down = 3;
const int up = 5;

void setup(){
  pinMode(led, OUTPUT);
  pinMode(right, INPUT);
  pinMode(down, INPUT);
  pinMode(up, INPUT);
  Serial.begin(9600);
}

void loop(){
  if(Serial.available() > 0) {
    char ledState = Serial.read();
    if (ledState == '1') {
      digitalWrite(led, ledState);      
    }
    if (ledState == '0') {
      digitalWrite(led, LOW);
    }
  }
  int rightState = digitalRead(right) ; 
  int downState = digitalRead(down);
  int upState = digitalRead(up);

  
  if (rightState > 0 || downState > 0 || upState > 0){
    
    if (upState == HIGH) {
      Serial.println("up");
    }

    if (rightState == LOW) {
      Serial.println("down");
    }
    
    if (downState == LOW) {
      Serial.println("right");
    }
    
    if (rightState ==HIGH && downState ==HIGH ) {
      Serial.println("combirightdown");
    }
  } else {
    Serial.println("nothing");
  }
  
}

Processing Code

import processing.serial.*;

Serial myPort;
String inString;  
int lf = 10; 
boolean contact = false;


void setup() {
  size (500,500);
  println("Available serial ports:");
  for(int i = 0; i < Serial.list().length; i++){
    println("[" + i + "]" + Serial.list()[i]);
  }
  if (Serial.list().length > 0){
    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
  }
  myPort.bufferUntil(lf);
}

void serialEvent (Serial p) {
  inString = p.readString();
  
}

void draw(){
  background(0,0,0);
  if (inString != null) {
    inString = trim(inString);
  }
  
if (contact == false) {
    if (inString != null && !inString.isEmpty() && inString != " " && inString != ""){
      inString = trim(inString);
      if (inString.equals("A")) {
        myPort.write("nothing");
      }
      if (inString.equals("down")) {
        rect(10,10,10,10);
      }
      
      if (inString.equals("right")) {
        rect(20,10,10,10);
      }
      
      if (inString.equals("up")) {
        rect(20,120,10,10);
      }
      
      if (inString.equals("combirightdown")) {
        rect(30,50,10,10);
      }
    }
    println(inString);
  }

  if (mousePressed && (mouseButton == LEFT)){
    myPort.write('1');
  }
  if (mousePressed && (mouseButton == RIGHT)) {
    myPort.write('0');
  }
}



Could someone help me?

Processing really doesn’t care if you send “up” or “u”, so make it easy. Send ONE letter.

 if (rightState > 0 || downState > 0 || upState > 0){

The state will be HIGH or LOW, so compare to that, NOT 0. The state will EQUAL HIGH or LOW.

   if (upState == HIGH) {
      Serial.println("up");
    }

    if (rightState == LOW) {
      Serial.println("down");
    }

WTF?

   if (downState == LOW) {
      Serial.println("right");
    }

Same question.

 println("Available serial ports:");
  for(int i = 0; i < Serial.list().length; i++){
    println("[" + i + "]" + Serial.list()[i]);
  }

The println() function is overloaded to print a list.

Using the internal pullup resistors makes for MUCH easier wiring.

By the way, the state change detection example REALLY bears looking into. Flooding Processing with serial data hundreds of times a second does not make sense.

um -

if (rightState == LOW) {
      Serial.println("down");
    }
    
    if (downState == LOW) {
      Serial.println("right");
    }