Привет, други и подруги. Есть код для управления из JAVA ардуино платой. И код для ардуино. Для управления. Вопрос как передавать значение “Hi somebody” при нажатии на LEFT, RIGHT кнопки в переменную java. И наоборот.
Нам понадобятся библы из arduino.jar
arduino jSerialComm-1.3.11.jar
Java code:
package javaapplicatidelete;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import arduino.*;
/*
* IMPORTANT: Please download and import the Arduino.jar and jSerialComm-1.3.11.jar files from my GitHub or SourceForge before running.
* NOTE: Before running, please upload and run the attached Arduino code to your board.
*/
public class GUI {
static Arduino arduino;
static JFrame frame = new JFrame("An Led Controller");
static JButton Send_to_Blue = new JButton("Send_To_Arduion");
static JButton Read_to_Java = new JButton("Read_to_Java");
static JButton btnRefresh;
static public String MySSS="Nukll";
public static void ReadString0000(){
MySSS=arduino.serialRead();
//System.out.println(MySSS);
}
public static void main(String[] args) {
setUpGUI(); // refer to this function only if you have knowledge of JAVA swing classes and GUI elements.
frame.setResizable(false);
Send_to_Blue.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e) {
String ttt="Hello Areg";
arduino.serialWrite(ttt);
System.out.println(ttt);
}
});
Read_to_Java.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e) {
//arduino.serialWrite("0");
//arduino.serialWrite("Hello, Lara");
ReadString0000();
System.out.println(MySSS);
}
});
}
public static void populateMenu(){ //gets the list of available ports and fills the dropdown menu
final PortDropdownMenu portList = new PortDropdownMenu();
portList.refreshMenu();
final JButton connectButton = new JButton("Connect");
//ImageIcon refresh = new ImageIcon("/Users/HirdayGupta/Documents/workspace/Java-Arduino-Communication-Library/src/examples/refresh.png");
//btnRefresh = new JButton(refresh);
JPanel topPanel = new JPanel();
topPanel.add(portList);
//topPanel.add(btnRefresh);
topPanel.add(connectButton);
// populate the drop-down box
connectButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(connectButton.getText().equals("Connect")){
arduino = new Arduino(portList.getSelectedItem().toString(),9600);
if(arduino.openConnection()){
connectButton.setText("Disconnect");
portList.setEnabled(false);
Send_to_Blue.setEnabled(true);
Read_to_Java.setEnabled(true);
frame.pack();
}
}
else {
arduino.closeConnection();
connectButton.setText("Connect");;
portList.setEnabled(true);
Send_to_Blue.setEnabled(false);
btnRefresh.setEnabled(true);
Read_to_Java.setEnabled(false);
}
}
});
//topPanel.setBackground(Color.BLUE);
frame.add(topPanel, BorderLayout.NORTH);
}
public static void setUpGUI(){
frame.setSize(600, 600);
frame.setBackground(Color.black);
frame.setForeground(Color.black);
//frame.setPreferredSize(new Dimension(600,600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
Send_to_Blue.setForeground(Color.GREEN);
Send_to_Blue.setEnabled(false);
Read_to_Java.setForeground(Color.RED);
Read_to_Java.setEnabled(false);
JPanel pane = new JPanel();
//pane.setBackground(Color.blue);
pane.add(Send_to_Blue);
pane.add(Read_to_Java);
frame.add(pane, BorderLayout.CENTER);
populateMenu();
frame.pack();
frame.getContentPane();
frame.setVisible(true);
}
}
Ардуино code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define BTN_UP 1
#define BTN_DOWN 2
#define BTN_LEFT 3
#define BTN_RIGHT 4
#define BTN_SELECT 5
#define BTN_NONE 10
int detectButton() {
int keyAnalog = analogRead(A0);
if (keyAnalog < 100) {
// Значение меньше 100 – нажата кнопка right
return BTN_RIGHT;
} else if (keyAnalog < 200) {
// Значение больше 100 (иначе мы бы вошли в предыдущий блок результата сравнения, но меньше 200 – нажата кнопка UP
return BTN_UP;
} else if (keyAnalog < 400) {
// Значение больше 200, но меньше 400 – нажата кнопка DOWN
return BTN_DOWN;
} else if (keyAnalog < 600) {
// Значение больше 400, но меньше 600 – нажата кнопка LEFT
return BTN_LEFT;
} else if (keyAnalog < 800) {
// Значение больше 600, но меньше 800 – нажата кнопка SELECT
return BTN_SELECT;
} else {
// Все остальные значения (до 1023) будут означать, что нажатий не было
return BTN_NONE;
}
}
void clearLines(){
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
void printDisplay(String message){
clearLines();
Serial.print(message);
lcd.setCursor(0, 0);
lcd.print(message);
// delay(1000);
// clearLine(1);
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 1 );
lcd.print("Art Master");
//delay(1000);
//delay(3000);
//lcd.setCursor(0, 0);
//lcd.print("Arduino Master");
}
String s="null";
void loop() {
if (Serial.available()) {
s = Serial.readString();
//lcd.setCursor(0, 0); // Первый символ первой строки
//lcd.setCursor(0, 1); // Первый символ второй строки
clearLines();
printDisplay(s);
}
int button = detectButton();
switch (button) {
case BTN_UP:
printDisplay("Up");
delay(1000);
break;
case BTN_DOWN:
printDisplay("DOWN");
break;
case BTN_LEFT:
printDisplay("LEFT");
break;
case BTN_RIGHT:
printDisplay("RIGHT");
break;
case BTN_SELECT:
printDisplay("Se");
//Serial.print("Se");
break;
default:
//printDisplay("Press any key");
break;
}
}