Reading serial value - Major problems

Hi I have took apart an air swimmer ( inflAtable shark controller) and I have soldered a few wires on to bypass each of the buttons (left right up and down) I need to send a LOW signal when a keyboard button is pressed. The LOW signal allows the logic board on the controller to send the correct infrared signal.

I have no idea why my code isn't working and I have went through 3 controllers trying to get this right. Does anyone have any ideas?

import processing.serial.*;
import processing.video.*;
Serial port;
Capture cam;
int val;
color fillVal = color(126);

void setup() {
    String portName = Serial.list()[0];
    port = new Serial(this, portName, 9600);
      size(550, 400);

  // If no device is specified, will just use the default.
  cam = new Capture(this, 550 , 400);
}
//Key Detection Begin
void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      port.write(65);
      fillVal = 255;
    } else if (keyCode == DOWN) {
      port.write(66);
      fillVal = 0;
    } else if (keyCode == RIGHT) {
      port.write(67);
      fillVal = 100;
    } else if (keyCode == LEFT) {
     port.write(68);
      fillVal = 200;
    } 
  } else {
    fillVal = 126;
  }
}

//* void keyReleased() {
 // if (key == CODED) {
 //   if (keyCode == UP) {
 //     port.write(69);
 //     fillVal = 0;
 //   } else if (keyCode == DOWN) {
 //     port.write(70);
 //     fillVal = 255;
 //   } else if (keyCode == RIGHT) {
 //     port.write(71);
 //     fillVal = 200;
 //   } else if (keyCode == LEFT) {
 //    port.write(72);
 //     fillVal = 100;
 //   } 
//  } else {
//    fillVal = 126;
//  }
//}

//Key Detection End

void draw() {
  //Camera Code Begin
  if (cam.available() == true) {
  cam.read();
  image(cam, 0, 0);
  //Camera Code End
  //Dashboard Interface Begin
  fill(100); //Left Big Circle
  ellipse(80,350,100,100); // Left Big Circle
  fill(100); // Right Big Circle
  ellipse(475,350,100,100); // Right Big Circle
  fill(100); // Large Rectangle
  stroke(100); // Large Rectangle
  rect(0, 350, 550, 75); // Large Rectangle
  fill(0,100,0); // Left Radar Circle
  stroke(0); // Left Radar Circle
  ellipse(80,350,90,90); // Left Radar Circle
  fill(200); // Left Control Stick
  stroke(0); // Left Control Stick
  rect(180, 360, 10, 75); // Left Control Stick
  fill(200); // Left Control Stick
  stroke(0); // Left Control Stick
  rect(205, 360, 10, 75); // Left Control Stick
  fill(200); // Right Control Stick
  stroke(0); // Right Control Stick
  rect(320, 360, 10, 75); // Right Control Stick
  fill(200); // Right Control Stick
  stroke(0); // Right Control Stick
  rect(355, 360, 10, 75); // Right Control Stick
  fill(255,0,0); // Right Control Stick
  stroke(0); // Right Control Stick
  rect(175, 360, 45, 10); // Right Control Stick
  fill(0,100,0); // Right Control Stick
  ellipse(475,350,90,90); // Right Control Stick
  fill(200); // Right Control Stick
  stroke(0); // Right Control Stick
  rect(180, 360, 10, 75); // Right Control Stick
  fill(200); // Right Control Stick
  stroke(0); // Right Control Stick
  rect(205, 360, 10, 75); // Right Control Stick
  fill(255,0,0); // Right Control Stick
  stroke(0);// Right Control Stick
  rect(320, 360, 45, 10); // Right Control Stick
  fill(255,0,0); // Left Control Stick
  stroke(0); // Left Control Stick
  rect(175, 360, 45, 10); // Left Control Stick
  fill(100); //entre Clock
  rect(220, 320,100,30);// Centre Clock
  fill(100); //entre Clock
  stroke(0);
  rect(225, 325,90,20);// Centre Clock
  //Interface End

}



}

Arduino Code -

 char val; // Data received from the serial port

void setup(){
  pinMode(3,OUTPUT);//add an LED on PIN 13 for 
  pinMode(5,OUTPUT);//add an LED on PIN 13 for kicks
  pinMode(7,OUTPUT);//add an LED on PIN 13 for kicks
  pinMode(9,OUTPUT);//add an LED on PIN 13 for kicks
  Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate)
}

void loop() {
 digitalWrite(3, HIGH); // turn the LED on
 digitalWrite(5, HIGH); // turn the LED on
 digitalWrite(7, HIGH); // turn the LED on
 digitalWrite(9, HIGH); // turn the LED on
 if (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }
 if (val == 'A') { // If H was received
 digitalWrite(3, LOW); // turn the LED on
 digitalWrite(5, HIGH); // turn the LED on
 digitalWrite(7, HIGH); // turn the LED on
 digitalWrite(9, HIGH); // turn the LED on
 } else {
 digitalWrite(3, HIGH); // Otherwise turn it OFF
 }
   if (val == 'B') { // If H was received
 digitalWrite(3, HIGH); // turn the LED on
 digitalWrite(5, LOW); // turn the LED on
 digitalWrite(7, HIGH); // turn the LED on
 digitalWrite(9, HIGH); // turn the LED on
 } else {
 digitalWrite(5, HIGH); // Otherwise turn it OFF
 }
   if (val == 'C') { // If H was received
 digitalWrite(3, HIGH); // turn the LED on
 digitalWrite(5, HIGH); // turn the LED on
 digitalWrite(7, LOW); // turn the LED on
 digitalWrite(9, HIGH); // turn the LED on
 } else {
 digitalWrite(7, HIGH); // Otherwise turn it OFF
 }
   if (val == 'D') { // If H was received
 digitalWrite(3, HIGH); // turn the LED on
 digitalWrite(5, HIGH); // turn the LED on
 digitalWrite(7, HIGH); // turn the LED on
 digitalWrite(9, LOW); // turn the LED on
 } else {
 digitalWrite(9, HIGH); // Otherwise turn it OFF
 }

}

anyone with any ideas would be helpful Im loosing the plot

I have no idea why my code isn't working a

Can you tell more explicit what you did expect and what you got?

A quick look at the code reveals

if (Serial.available()) { // If data is available to read, val = Serial.read(); // read it and store it in val }

I allways use if (Serial.available() > 0) - is more explicit

      port.write(65);

65? What the heck is 65 supposed to mean?

 if (val == 'A') { // If H was received

Is this checking for A, H or 65? Consistency would be most beneficial.

This might help. I prefer to check for the same int numbers you are sending to Arduino, not for the char value. I’ve done it that way and works perfectly. Using int numbers <255 will keep it simple and you can send a whole lot of orders that way. You only have to read one at a time. You can use any numbers from 1 to 255, just make sure they are not been sent for another reason or use.
I would clear the buffer once its read. (Serial.flush():wink:

int val; // Data received from the serial port

void setup(){
pinMode(3,OUTPUT);//add an LED on PIN 13 for
pinMode(5,OUTPUT);//add an LED on PIN 13 for kicks
pinMode(7,OUTPUT);//add an LED on PIN 13 for kicks
pinMode(9,OUTPUT);//add an LED on PIN 13 for kicks
Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate)
}

void loop() {
digitalWrite(3, HIGH); // turn the LED on
digitalWrite(5, HIGH); // turn the LED on
digitalWrite(7, HIGH); // turn the LED on
digitalWrite(9, HIGH); // turn the LED on
if (Serial.available()>0) { // If data is available to read,
val = Serial.read(); // read it and store it in val
Serial.flush(); //Clears the buffer.
}
if (val == 65) { // If 65 was received. I don’t know the secuence you are sending them in. This is just an example.
val=0; //clears val once it is recognized. This prevents Arduino from repeating the commands on the next loop if there is no new data available.
digitalWrite(3, LOW); // turn the LED on
digitalWrite(5, HIGH); // turn the LED on
digitalWrite(7, HIGH); // turn the LED on
digitalWrite(9, HIGH); // turn the LED on
} else {
digitalWrite(3, HIGH); // Otherwise turn it OFF
}
}

i saw that Serial.flush() in the code… it’s not clearing the buffer anymore now, it waits untill all data is transmitted… just to let you know

He is sending one byte at a time (number<255). Once he reads the number in the buffer, there is nothing else he wants to read until a new byte arrives. It worked great for me…