Using processing to drive relays.

HI, so basically I'm made this simple interface using processing to drive two relays via ardiuno. When I press '1' the first relay should turn on and when I press '2' the second relay turns on. The relays work the way I want to when tested on their on but it doesn't work when connected to the ardiuno. Only one pin sends an output the other pins doesn't do it. I had some luck when I changed the code of ardiuno to when one pin turns on the other goes off- but thats not how I want it. I'll include a copy of my codes both processing and ardiuno

Processing

import processing.serial.*;

Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200, 200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[5]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  printArray(Serial.list());
}
void draw() {
    if ((keyPressed == true)&&(key == '4'))
    {                           //if we clicked in the window
      myPort.write('1');         //send a 1
      println("1");
    } 
    if ((keyPressed == true)&&(key == '2')) {
      myPort.write('2');
      println("2");
    } else 
    {                           //otherwise
      myPort.write('0');          //send a 0
    }
  }

//  if (mouseButton == LEFT) {
//    myPort.write('1');
//    println("1");
//  } else if (mouseButton == RIGHT) {
//    myPort.write('2');
//    println("2");
//  } else {
//    myPort.write('0');
//  }
//}

Ardiuno

char val;

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

}

void loop() {
  if (Serial.available()) {
    val = Serial.read();
    
  }
  if (val == '1') {
  digitalWrite(6, HIGH);
  

  }
  if (val == '2') {
  digitalWrite(5, HIGH);
  
  

  

  }
  else {
    
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);


  }
  
  delay(10);

}

the way you code your else is the issue in processing because it only applies to the second if

So if I press 4 you send '1' and then the next if is false so you send '0' too which will be caught by the arduino side and immediately turn off both relays

If I press '2' in processing then you send '2' but you don't send the zero

Need to fix this.

Test if key is pressed, if it is send the right command and remember you did send it for that key press and wait until key is no longer pressed to be ready to send something else. Only send 0 for example if you don't type the right key, or upon key release (only once, need to memorize this too)

Your assessment of the Problem is probably wrong. I suspect that pressing 1 or 2 will both turn a pin on. The Problem is that if 1 is pressed, the first if turns pin 6 on. The second if tests for a 2 and 1 is not 2 so the else fires which immediately turns the pin back off.

I suggest you refer to switch...case - Arduino Reference and learn how to use the Switch case construct. case 1 turn pin 6 on, case 2 turn pin 5 on, Default (any key which is not 1 or 2) turns both Pins off.

I would like the pin to turn on for a just a little bit. The relay latches manually. When a '1' is sent to ardiuno nothing happens but when it receives a '2' it works fine. :confused:

I suspect that you really want if/else if/else, not if/if/else.

If you receive a '1', you turn pin 6 on. But, then, '1' is not '2', so you turn pin 6 off again.

Yami89:
I would like the pin to turn on for a just a little bit. The relay latches manually. When a '1' is sent to ardiuno nothing happens but when it receives a '2' it works fine. :confused:

have you read and understood post #1?