### B4J Switch Led On/Off

Example turning Arduino LED 13 On and Off using a B4J Application.
Setup
The Arduino is connected to the device via USB.
For this example, the devices used for tested:
• PC running Windows 8.1
• Raspberry Pi 2 model B running Raspian.
Arduino Sketch written in C
const int LED = 13; // define the Arduino onboard pin 13 as LED
void setup()
{
Serial.begin(9600); // open the serial line at 9600 bps
pinMode(LED,OUTPUT); // define pin 13 as output (using constant LED)
Serial.print("Awaiting Input... \n"); // display initial message in the Arduino Serial Monitor
}
//Global vars
String txtCmd = ""; // The command received from the connected device
char s; // Char read from the serial line
//Loop = awaiting data from connected device, analyse and execute command accordingly
void loop() {
// If the serial line is ready to go, then read the incoming data
while (Serial.available() > 0) {
// Get the incoming char
s=(char)Serial.read();
// Check if char is return (or EOL)
if (s == '\n') {
// End received, means a full command received.
// Check which command to execute, then execute, idc turn LED ON or OFF
// Write result back to the connected device via the serial line.
// Command ON = Turn LED ON, write 1 or ON back
if(txtCmd=="ON") {
setLed(true);
};
if(txtCmd=="OFF") {
setLed(false);
};
// Reset the command
txtCmd = "";
} else {
// Add the received char to the command
txtCmd +=s;
}
}
}
//Set the state of the led
//Parameter: state as boolean true or false
void setLed(boolean state){
if(state==false) {
digitalWrite(LED, LOW);
//Serial.write("0");
Serial.write("OFF");
} else {
digitalWrite(LED, HIGH);
//Serial.write("0");
Serial.write("ON");
}
}
B4J Code
Using B4J Version=2.8 and AppType=JavaFX
Libraries used: jSerial, jRandomAccessFile
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private txtFromArduino As TextField
Private btnLEDOn As Button
Private btnLEDOff As Button
Private sp As Serial
Private astream As AsyncStreams
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
MainForm.SetFormStyle("UTILITY")
MainForm.Resizable = False
MainForm.Show
MainForm.Title = "B4J Arduino Turn LED13 On/Off v20150221"
'Init the serial port and the asyncstream
SerialPortInit
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
txtFromArduino.Text = "Awaiting Selection"
End Sub`
'Init the serial port
`Sub SerialPortInit
Dim BaudRate As Int = 9600
Dim DataBits As Int = 8
Dim StopBits As Int = 1
Dim Parity As Int = 0
sp.Initialize("")
sp.Open("COM3")
sp.SetParams (BaudRate , DataBits , StopBits , Parity )
End Sub`
`Sub MainForm_Closed
Try
Log("Closing AStream")
If astream.IsInitialized Then astream.Close
Log("Closing Serial Port")
sp.Close
Log("Closed All")
Catch
Log(LastException.Message)
End Try
End Sub`
`Sub AStream_Error
Log("Error: " & LastException.Message)
astream.Close
End Sub`
'Handle newdata from the arduino coming in via the serial line.
'Parameter: Buffer as byte. The buffer needs to be converted to a string
`Sub AStream_NewData (Buffer() As Byte)
Dim S As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Newdata: " & S)
Select S
Case "0":
txtFromArduino.Text = "Arduino Replied with 0 : LED=OFF"
Case "1":
txtFromArduino.Text = "Arduino Replied with 1 : LED=ON"
Case "ON":
txtFromArduino.Text = "Arduino Replied with 1 : LED=ON"
Case "OFF":
txtFromArduino.Text = "Arduino Replied with 1 : LED=OFF"
End Select
End Sub`
'Send a command to the arduino via the serial line using asynstream write
`Sub Command_Send_To_Arduino(cmd As String)
Dim bytes() As Byte = (cmd & CRLF).GetBytes("UTF8")
Log("Send Command to Arduino: " & cmd)
astream.Write(bytes)
End Sub`
`Sub btnLEDOff_Action
Command_Send_To_Arduino("OFF")
End Sub`
`Sub btnLEDOn_Action
Command_Send_To_Arduino("ON")
End Sub`

Changed: , Category: Arduino, File: T6C161M1201.htm, ID: 1201