0

Please help

I have created an Arduino program that will switch the pins when I send a char. It works fine with serial monitor, but the problems come when I send chars with Eclipse using RXTX serial communication. When I click the button to send the char, at first click it does not switch the Arduino pins; it just senses the Rx and Tx LED but the second time it switches on. But when I tried to switch the second LED it first turns off the first LED although that was not my aim, and then it switches the intended LED. Here is my Arduino code.

const int lamp=2;
const int fan=3;
const int pump=4;
const int fridge=5;
char val;
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(lamp,OUTPUT);
 pinMode(fan,OUTPUT);
 pinMode(pump,OUTPUT);
 pinMode(fridge,OUTPUT);
}
void loop() {
 // put your main code here, to run repeatedly:
 if(Serial.available()) {
 val=Serial.read();
 if(val=='a') {
 digitalWrite(lamp,HIGH);
 Serial.print(val);
 delay(100);
 } else if(val=='b') {
 digitalWrite(lamp,LOW);
 Serial.print(val);
 delay(100);
 } else if(val=='c') {
 digitalWrite(fan,HIGH);
 Serial.print(val);
 delay(100);
 } else if(val=='d') {
 digitalWrite(fan,LOW);
 Serial.print(val);
 delay(100);
 } else if(val=='e') {
 digitalWrite(pump,HIGH);
 Serial.print(val);
 delay(100);
 } else if(val=='f') {
 digitalWrite(pump,LOW);
 Serial.print(val);
 delay(100);
 } else if(val=='g') {
 digitalWrite(fridge,HIGH);
 Serial.print(val);
 delay(100);
 } else if(val=='h') {
 digitalWrite(fridge,LOW);
 Serial.print(val);
 delay(100);
 }
 } else {
 int sensorValue = analogRead(A0);
 Serial.println(sensorValue/10);
 delay(100);
 }
}

Here is a part of my Java code

public boolean identifyport() throws PortInUseException, IOException, TooManyListenersException {
 this.setVisible(true);
 try {
 Enum=CommPortIdentifier.getPortIdentifiers();
 while(Enum.hasMoreElements())
 portname=(CommPortIdentifier)Enum.nextElement();
 if(portname.getPortType()==CommPortIdentifier.PORT_SERIAL)
 port=portname.open(this.getClass().getName(), 2000);
 serialport=(SerialPort)port;
 serialport.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_2,SerialPort.PARITY_NONE);
 input=((CommPort) serialport).getInputStream();
 output=((CommPort)serialport).getOutputStream();
 serialreader=new BufferedReader(new InputStreamReader(input));
 serialport.addEventListener(this);
 serialport.notifyOnDataAvailable(true);
 progressBar.setBackground(Color.GREEN);
 portlist.addItem(portname.getName());
 progressBar.setValue(i);
 slider_3.setValue(value);
 Thread.sleep(1000);
 prg();
 } catch (UnsupportedCommOperationException e1) {
 // TODO Auto-generated catch block
 JOptionPane.showMessageDialog(contentPane, "Check USB cable connection", "No port available ", 0);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 JOptionPane.showMessageDialog(null, "Port status", "Please connect the device....!!!!", day, null);
 }
 return rootPaneCheckingEnabled;
 }
 @Override public void serialEvent(SerialPortEvent e) {
 // TODO Auto-generated method stub
 if(e.getEventType()==SerialPortEvent.DATA_AVAILABLE){
 try {
 serial= serialreader.read();
 } catch(Exception e1) {
 e1.printStackTrace();
 }
 }
 }
 public void writedata(int v1) {
 try {
 output.write(v1);
 output.hashCode();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
*And this is part of the switch button;*
Button button_1 = new Button("switch");
button_1.setFont(new Font("Tahoma", Font.BOLD, 13));
button_1.setBounds(10, 10, 117, 42);
panel_3.add(button_1);
button_1.addActionListener(new ActionListener() {
 int count=0;
 public void actionPerformed(ActionEvent e) {
 writedata(v1);
 if(count==0) {
 canvas.setBackground(Color.RED);
 canvas.setForeground(Color.GREEN);
 v1='b';
 count++;
 } else {
 canvas.setBackground(Color.YELLOW);
 canvas.setForeground(Color.GREEN);
 v1='a';
 count--;
 }
 }
});

Sorry I can not write everything because there is so much code.

dlu
1,6612 gold badges15 silver badges29 bronze badges
asked Nov 17, 2015 at 21:48
1
  • use pastbin.com to give us all the java code so anyone can test it. Commented Nov 19, 2015 at 20:33

2 Answers 2

0

The java code you wrote should send just 'a' and 'b' so you should see just lamp turning on and off.

Move writedata invocation just before count++ and count-- statements to solve your "first time". The problem could be the initialization of v1.

Into writedata method why do you call hashcode on the output stream? Maybe flush?

answered Nov 20, 2015 at 15:55
0

Thanks my friends, I had already solved the problem, the problem was this.... I made the method that ran after pressing each button, which sent the data at particular button, it seems the data interfered each other but I do not know why, but I had deleted that method and I used to send data using the object I created at each button.

Button button_1 = new Button("switch");
 button_1.setFont(new Font("Tahoma", Font.BOLD, 13));
 button_1.setBounds(10, 10, 117, 42);
 panel_3.add(button_1);
 button_1.addActionListener(new ActionListener() {
 int count=0;
 public void actionPerformed(ActionEvent e) {
 if(count==0){
 canvas.setBackground(Color.RED);
 canvas.setForeground(Color.GREEN);
 char v2 = 'a';
 try {
 output.write(v2);
 } catch (IOException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 }
 count++;
 }
 else{
 canvas.setBackground(Color.YELLOW);
 canvas.setForeground(Color.GREEN);
 char v1 = 'b';
 try {
 output.write(v1);
 } catch (IOException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 }
 count--;
 }
 }

Now it worked fine....!!!!.

answered Nov 21, 2015 at 4:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.