kurbatov / firmata4j

Firmata client written in Java.
MIT License
87 stars 45 forks source link

Arduino resets the previous state #12

Closed aliassad closed 7 years ago

aliassad commented 7 years ago

when i send digital message to arduino setdititalpin 8 high its work but when i send setdititalpin 9 high the pin 8 goes low. The arduino resets its self before sending a new message.

kurbatov commented 7 years ago

Which version of firmata (.ino sketch) and firmata4j do you use? Please, provide a code snippet to reproduce the issue.

aliassad commented 7 years ago

Latest standardfirmata.ino and firmata4j 2.3.4

aliassad commented 7 years ago

device.getPin(8).sevalue(1); device.getPin(9).sevalue(1);

//the pin 9 gets high only

kurbatov commented 7 years ago

Are those pins in output mode?

aliassad commented 7 years ago

yes

kurbatov commented 7 years ago

BTW, your snippet is not compilable. Could you please, give some comprehensive snippet right from your code, where it is visible how do you know that something went wrong. Otherwise it's hardly possible to resolve the issue.

aliassad commented 7 years ago
   try{
         MainGUI.PORT_NAME=requestPort();

         device = new FirmataDevice(MainGUI.PORT_NAME); // construct the Firmata device instance using the name of a port
         device.start(); // initiate communication to the device
         device.ensureInitializationIsDone(); // wait for initialization is done        

         Pin p1=device.getPin(8);
         Pin p2=device.getPin(13);

            try {
     for(int i = 0; i < 1000; i++) {
        p1.setValue(1);
        Thread.sleep(500);
        p2.setValue(1);
        Thread.sleep(500);
         p1.setValue(0);
         Thread.sleep(500);
        p2.setValue(0);
        Thread.sleep(500);
     }
  } catch (IllegalStateException | IOException | InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }
aliassad commented 7 years ago

arduino resets after each intstruction.

kurbatov commented 7 years ago

It's stil not shown how do you know that it resets? Do you have a listener or do you observe some LED attached to the pins?

Try to set output mode to the pins explicitly.

    ...
    Pin p1=device.getPin(8);
    Pin p2=device.getPin(13);
    p1.setMode(Pin.Mode.OUTPUT);
    p2.setMode(Pin.Mode.OUTPUT);
    ...
aliassad commented 7 years ago

Same Issue

aliassad commented 7 years ago

I had attached LEDs on Arduino pins. the pin8 led gets low when the pin13 is high.

soundanalogous commented 7 years ago

It appears the value of the port is not being tracked across writes. I've produced a crude (I don't write much Java) example to illustrate this:

package com.testportwrite;

import org.firmata4j.IODevice;
import org.firmata4j.firmata.FirmataDevice;
import org.firmata4j.Pin;

import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        final IODevice device = new FirmataDevice("/dev/cu.usbmodem14231");
        try {
            device.start();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        try {
            device.ensureInitializationIsDone();
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.exit(1);
        }

        Pin pinA = device.getPin(8);
        Pin pinB = device.getPin(9);

        try {
            pinA.setValue(1);
            pinB.setValue(1);
            try {
                // keep LEDs on for 4 seconds
                Thread.sleep(4000);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        try {
            device.stop();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }
}

The call to pinB.setValue(1) will set pinA back to 0. However, the port value should be maintained.

kurbatov commented 7 years ago

I have released fixed version 2.3.4.1