Pages: [1]   Go Down
Author Topic: Delay when reading button input  (Read 1000 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a button connected to my arduino, I use stream.print() to send the button's state and I use java to listen on the port and read from the stream. I get a slight but noticeable delay. I play a note on the computer whenever the button is pressed and I can hear the button click first and then the note. Is there any way around this? am I doing something wrong?
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34209
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you are not telling us the whole tale sp it is hard to say.
Quote
I use stream.print()
So where did this come from?
What baud rate have you got things set to?

It sounds like you have quite a chain of applications, the delay could be in any one of them.
It could be in your arduino code but being the weekend, my crystal ball is stuck at work and I can't get to it.
How about posting the code and describing what you are doing, with links.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the aruduino code:

Code:
int digin1 = 50;
int baudrate = 9600;

void setup()
{
  Serial.begin(baudrate);
  pinMode(digin1, INPUT);
}

void loop()
{
  int val1 = digitalRead(digin1);
  if (val1 == HIGH){
    Serial.print(val1);
  }
}

and this is the java code:
Code:
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
if (available > 0) {
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
                                        playNote();    // play immediately when data is available
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

By the way, I have tried different baud rates and got the same result.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is providing the input to the digital pin? If it is a switch (buttons are for shirts), is there an external pulldown resistor? There needs to be one, or the pin is floating.

The Arduino sends a value when the pin is HIGH (not when it goes HIGH). It sends nothing when the pin goes/is LOW.

Code:
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
if (available > 0) {
The method was called because there was some activity on the serial port. The type of activity was determined to be data arriving. If that is the case, is it really possible that available will be 0 or less?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it currently only sends HIGH because I'm just trying to figure the delay issue.
The data available is higher than 0 when the arduino sends data, it arrives fine - I get what I want but with a slight delay.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I get what I want but with a slight delay.
Then you need to determine whether that delay is a result of extraneous processing in the Java application, the slow baud rate you are using, or the fact that playNote() takes some time to start making noise.

Start by abandoning the Java application for now. Open the Serial Monitor. Press the switch. How long does it take from switch press to '1' on the serial monitor?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Instantly, it seems.

In the java application I also tried to print the input to the console and it had the same delay as the sound, so it's not playNote().
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In the java application I also tried to print the input to the console and it had the same delay as the sound, so it's not playNote().
So, it appears that there is some delay between when serial data arrives on the serial port, and when the snippet of code you posted gets executed.

Can you see why we don't like for people to post just snippets?

Can you determine what the next step is?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my code (I used the java example from arudino java interfacing tutorial):
Code:
public class SerialStream implements SerialPortEventListener {

        SerialPort serialPort;

private static final String PORT_NAME = "COM4";

private InputStream input;

private OutputStream output;

private static final int TIME_OUT = 2000;

private static final int DATA_RATE = 9600;

public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if (currPortId.getName().equals(PORT_NAME)) {
portId = currPortId;
break;
}
}

if (portId == null) {
System.out.println("Could not find COM port.");
return;
} else {
System.out.println("COM port found: " + PORT_NAME);
}

try {
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);

serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

input = serialPort.getInputStream();
output = serialPort.getOutputStream();

serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);

} catch (Exception e) {
e.printStackTrace();
}
}

public synchronized void close() {
if (serialPort != null) {
serialPort.close();
}
}

public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
if (available > 0) {
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
                                        playNote();    // play immediately when data is available
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

this is the main class:
Code:
public class Main {

private SerialStream _stream;

public static void main(String[] args) {
new Main().start();
}

public void start() {
try {
_stream = new SerialStream();
_stream.initialize();
} catch (Exception e) {
e.printStackTrace();
}
}

}

The code that reads from the stream is executed whenever there's an event. I thought that maybe there's some sort of delay in the notification of the event so I also tried this:
Stream class:
Code:
public void event() {
try {
int available = input.available();
if (available > 0) {
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
                                        playNote();    // play immediately when data is available
}
} catch (Exception e) {
e.printStackTrace();
}
}

Main:
Code:
public static void main(String[] args) {
new Main().start();
}

public void start() {
try {
_stream = new SerialStream();
_stream.initialize();
while (true) {
_stream.event();
}
} catch (Exception e) {
e.printStackTrace();
}
}

But that didn't help either.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
private static final int TIME_OUT = 2000;
How long is the delay between pressing the switch and the note starting?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The timeout is for opening the port (as in how long should the application wait for the port to open).
The delay is probably something like 0.1 seconds (it's hard to tell since it's very short).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The timeout is for opening the port (as in how long should the application wait for the port to open).
No. It is used for more than that. Any time you call a read function, the timeout value defines how long the call will block.

As I said, this looks like strictly a Java issue, not an Arduino issue.
Logged

Pages: [1]   Go Up
Jump to: