How to use use output.write(byte myByte) ?

Hi everyone !

I try to use the output.write(byte myByte) method in my Java program, but I don't know what I must put into myByte ...

What is the format ? and the sequence , like for turning on an output ?

I want to
-make pin 8 as an output
-put value "HIGH" in pin 8
-put value "LOW" in pin 8

I success in Arduino language , but in java it's really hard when i try to understand byte meaning...

I work on an Arduino mega 2560.

Thanks guys !

Mehbee could use Txtzyme and

output.write( "0b 1o 0o"); // pin 8 to output, turn on pin 8, turn off pin 8
or
output.write( "5{ 0b 1o 100m 0o 100m }"); // blink pin 8 5 times in 1 second

HTH :slight_smile:

John

What is Txtzyme ? a library ?

Is this solution usable in java ?

Thanks John xD

So if I don't want to use Txtzyme, because I want to interface arduino with many other java projects , how can I fill my "myByte" ??

Sorry, I am somewhat poking fun at you because your question seems to me a bit unclear and "off-the-wall".

This is what I understand you are are trying to do:

"I want to have a program on my pc written in Java, which will communicate to a program running on the Arduino to make it turn things on and off".

Is that what you're trying to do? And you are asking, "how do I do that?"

John

Yes lmgtfy was fun =)

So my problem is : I need to send byte to my arduino , to turn on and off pins. But I use Java , and I have to send byte via output.write(byte) , but I dont know what I have to put in my byte.

I dont know what is the correct order in bytes , which values ect ...

Kpro81:
I dont know what I have to put in my byte.

I dont know what is the correct order in bytes , which values ect ...

What you send from the PC and what you receive at the Arduino must be the same. It is up to you to decide what message format you are going to use and create code to send and receive the messages on both sides. There is no 'correct' answer - the answer is whatever message format you choose to use.

Ok.

The answer depends very much on what program you are running on your Arduino. You have to put a program on your Arduino--either one that you write yourself or one that someone else wrote.

If you write the program that runs on the Arduino, the protocol (what you put in your java output.write()) is up to you
e.g.
If you write program on Aduino like this : if readFromPC()=="LEDEIGHTON" then digitalWrite(8,HIGH);
then
you write program on PC (java) like this: output.write("LEDEIGHTON"); // turn on Led 8

If someone else writes the program that you put on the arduino, that program will specify how it will turn on and off pins based on what you send it from your pc java program. I gave you the example of how you would do that if you had txtzyme running on your Arduino. Others in this forum can probably suggest other programs that could do similar. ("bitlash"?)

But the main point is, it's the program running on the ARduino (not Arduino itself), which determines what you should send it from your pc output.write(...).

Hope this helps,
John

Thanks a lot for theses explication , I now understand the link between Arduino and java.

Java just call function or instruction which are on the arduino program already loaded on the board. Am i allright ? ^^

But the readFromPC function is not native to Arduino. What is the code of this function ?

That function was (probably) invented to show the programming principle. You can write your own with Serial.read and a buffer.

You may find that you can make it very simple - if you only turn a few pins on/off, then you send a simple binary byte and in the Arduino program use the bit operators; each bit in the byte is the state of a pin.

the Firmata software package does roughly the same as the teensy/Txtzyme, just a differnet protocol. Load software in the Arduino, then writethe program in Java on the host pc. The Arduino is just an IO-slave.

No, the linkage between your PC application (written in java or any PC programming language) and the sketch code running on your arduino board is just simple serial communications via a PC COM port. Serial communication allows 8 bit characters to be sent in either direction between a PC application and the arduino sketch. So what you send and how you interrupt the meaning of the serial characters is called a protocol and is up to you to define it in detail in both the PC program and in your Arduino sketch. Assuming that any sending functions defined in your PC java program are automatically understood by your arduino sketch code is a false assumption. You must make all that happen in the coding of both the PC application and the arduino sketch.

Lefty

So now I try to send 'e' or 101 to my arduino board , but I got the exception : java.lang.NumberFormatException: For input string: "e"

my code is :

Byte b= new Byte("e");
            output.write(b);
            output.flush();

What must I send to my board to trig the "e" event ?

Byte b= new Byte("e");

Is the string appropriate input? Or, should it be a character (single quotes)?

It should be a character ... =/

Problems solved !! Thank you all for your explications =)