Pages: [1] 2   Go Down
Author Topic: How to use use output.write(byte myByte) ?  (Read 540 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

John
Logged

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

What is Txtzyme ? a library ?

Is this solution usable in java ?
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://lmgtfy.com/?q=txtzyme&l=1
Logged

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

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" ??
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

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 ...
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

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 ?
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1206
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

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 :
Code:
Byte b= new Byte("e");
            output.write(b);
            output.flush();

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

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

Code:
Byte b= new Byte("e");
Is the string appropriate input? Or, should it be a character (single quotes)?
Logged

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

It should be a character ... =/
Logged

Pages: [1] 2   Go Up
Jump to: