SINTEF-9012 / JArduino

Program your Arduino in Java
https://github.com/SINTEF-9012/JArduino/wiki
176 stars 63 forks source link

DigitalState.HIGH is not outputting correct value equivalent of (byte)127 #55

Closed lobsteroh closed 8 years ago

lobsteroh commented 8 years ago

creating a DigitalWriteMsg(DigitalPin pin, DigitalState value) with

digitalWrite(DigitalPin.PIN_11,DigitalState.HIGH);

is not turning on 5V - powering an led makes it only blink very dimly

in contrast using writing analog to PWM with

analogWrite(PWMPin.PWM_PIN_11,(byte)127)

makes it light up like a daemon. actually powering the PWMPin with (byte)1 gives the same brightness as DigitalState.HIGH.

i am thinking that writing DigitalState.HIGH to the DigitalPin ought to send (byte)127. several ways I can think of doing this ...

  1. obvious fix - defining DigitalState.HIGH with 127 (or 255 for that matter works as well although it runs into the signed/unsigned byte issue) instead of 1 in DigitalState.java
  2. creating a DigitalWriteMsg with value 127 when DigitalState.HIGH is provided

any prefs? obvious fix until then ... use PMWpins and output the (byte)127 to analogWrtite if you want the whole level of voltage on the pin

brice-morin commented 8 years ago

Yes, this issue is the same as #44

I guess I will go with option one and change this line

brice-morin commented 8 years ago

Hopefully it should work now

lobsteroh commented 8 years ago

looks like i need to create and send low level DigitalWriteMsg to figure out exactly where the thing goes wrong ... still only getting 2.5V on DigitalPin 8 with DigitalState.HIGH

brice-morin commented 8 years ago

Ok... Did you properly set the mode of that pin before setting it to High?

lobsteroh commented 8 years ago

Hi Brice, I understand that you want to include the type checking by declaring enums but it realy limits testing when the behavior is not what it is meant to be. on my copy of JArduino i have added the constructor

public DigitalWriteMsg(DigitalPin pin, byte byteValue) {
    setCommandID(JArduinoProtocol.DIGITAL_WRITE);
    setByteValue(pin.getValue());
    this.pin = pin;
    setByteValue(new Byte(byteValue));
            //this.value = byteValue;
}

and in AbstractJArduino

public void digitalWrite(DigitalPin pin, byte value) {
    // Create message using the factory
    FixedSizePacket p = JArduinoProtocol.createDigitalWrite(pin, value);
    // Send the message on the serial line
    serial.receiveMsg(p.getPacket());
}

so now i can send specific byte values to test digitalWrite(). This is the test class i am using where you can just comment

import org.sintef.jarduino.DigitalPin;
import org.sintef.jarduino.JArduino;
import org.sintef.jarduino.PWMPin;

public class ArduinoTestWrite extends JArduino {

//static Object outPin = PWMPin.PWM_PIN_11;
static Object outPin = DigitalPin.PIN_11;

static byte onVal = (byte)127;

public ArduinoTestWrite(String port) { super(port); }

@Override
protected void setup() {}

@Override
protected void loop() {}

public static void main(String[] args) {
    try {
        String serialPort = "/dev/tty.usbmodem1421";
        ArduinoTestWrite arduino = new ArduinoTestWrite(serialPort);
        arduino.runArduinoProcess();
        Thread.sleep(2000);

        if (outPin instanceof PWMPin)
            arduino.analogWrite((PWMPin)outPin,onVal);
        else if (outPin instanceof DigitalPin)
            arduino.digitalWrite((DigitalPin)outPin,onVal);
        System.out.println("Turning on LED with byte value: " + onVal);
        Thread.sleep(1000);

        arduino.stopArduinoProcess();
    } catch (Exception e) { e.printStackTrace(); }
}
}

Switching PWMPin to the DigitalPin line reduces output to half. I am puzzled where the problem sits ... suggestions?

lobsteroh commented 8 years ago

you got it. Yes, setting pinMode makes all the difference, but it has to happen at a point when the arduino is already listening. This version now works like a charm. Any byte value other than 0 will turn on the DigitalPin full steam...

import org.sintef.jarduino.DigitalPin;
import org.sintef.jarduino.JArduino;
import org.sintef.jarduino.PWMPin;
import org.sintef.jarduino.PinMode;

public class ArduinoTestWrite extends JArduino {

//static Object outPin = PWMPin.PWM_PIN_11;
static Object outPin = DigitalPin.PIN_11;

static byte onVal = (byte)127;

public ArduinoTestWrite(String port) { 
    super(port); 
}

@Override
protected void setup() {}

@Override
protected void loop() {}

public static void main(String[] args) {
    try {
        String serialPort = "/dev/tty.usbmodem1421";
        ArduinoTestWrite arduino = new ArduinoTestWrite(serialPort);
        arduino.runArduinoProcess();
        Thread.sleep(2000);

        if (outPin instanceof PWMPin)
            arduino.analogWrite((PWMPin)outPin,onVal);
        else if (outPin instanceof DigitalPin) {
            arduino.pinMode((DigitalPin)outPin, PinMode.OUTPUT);
            arduino.digitalWrite((DigitalPin)outPin,onVal);
        }
        System.out.println("Turning on LED with byte value: " + onVal);
        Thread.sleep(1000);

        arduino.stopArduinoProcess();
    } catch (Exception e) { e.printStackTrace(); }
}
}