SolidSoils / Arduino

C# .NET - Arduino library supporting simultaneous serial ASCII, Firmata and I2C communication
https://solidsoils.github.io/Arduino/index.html
BSD 2-Clause "Simplified" License
200 stars 61 forks source link

Unable to get 'GetPinState' from Analog input to return value #14

Closed virtq closed 6 years ago

virtq commented 8 years ago

I'm trying to get a value from an analog input. I have tested with a standard arduino sketch together with a potentiometer and I'm getting values between 0 - 1023 as expected. However when I try to do the same using SolidSoils library I get the value 0 every time. It can recognize values from digital ports though. Maybe I don't understand how to use the library correctly. Do I need to set AnalogReportMode to true for the channel?

Using Firmata version 2.5.2.

Code:

private void ReadButton_Click(object sender, EventArgs e)
{
            //initialize connection to Arduino
            var connection = new SerialConnection("COM5", SerialBaudRate.Bps_57600);
            var session = new ArduinoSession(connection, timeOut: 250);

            session.SetDigitalPinMode(16, PinMode.AnalogInput);
            PinState ps = session.GetPinState(16);
            MessageBox.Show(ps.Value.ToString() + " " + ps.Mode.ToString() + " " + ps.PinNumber.ToString());
            Thread.Sleep(500);
            connection.Close();
}
SolidSoils commented 6 years ago

Method SetAnalogReportMode is used to command an Arduino board to send a continuous stream of sampled values. GetPinState retrieves a pin's single value.

I cannot reproduce the issue.

Oluntridao commented 6 years ago

The same think happens to me. I always receve 0 value when i execute getPinState.

Angelusvetus commented 5 years ago

I also have similar problems. I see a tremendous number of posts with the same questions. I feel the solidsoils library is well written. However, to date, I have not been able to find anyone providing information on using the analog side of solidsoils. All responses are vague and without example. There are a multitude of videos and text articles on the digital side. However, the digital side is rarley in question.

It would be very helpful if someone could provide a short example in C#/C#.NET that reads an analog value from Analog input pin A0 on the Arduino UNO using Firmata. This would be most helpful. The internet and myself would be most grateful.

zoopyserg commented 3 years ago

ZEEEEEEEEEEROOOOOOOOOOOO, THIS CODE GIVES ZERO:

PinState result = session.GetPinState(14);
UpdateStatusListBox(result.Value.ToString());

Can anyone here do their job properly?

SolidSoils commented 3 years ago

Hi @zoopyserg, I am feeling your pain. ;-) Software development can be frustrating.

Your struggle may be caused by a misconception about what GetPinState actually does. This library is based on the standard firmata protocol that is described here. What GetPinState actually does is described here.

zoopyserg commented 3 years ago

This file from the SolidSolis repo contains a full working example of both Analog and Digital signal processing:

https://github.com/SolidSoils/Arduino/blob/master/Solid.Arduino.Run/Program.cs

Everything that starts with this:

static void SimpelTest

And until the end of the file.

But out of the box, it is also incomplete (sets up the callbacks, but starts listening for only digital pins). To get it to work for analog pins, people need to toss in this line somewhere inside static void SimpelTest:

firmata.SetAnalogReportMode(5, true);

Which then makes that code magically trigger the callback private void Session_OnAnalogStateReceived which contains the correct recording of an analog pin.

After that I need to solve the problems with parallel threads and busy open Arduino connections which is still work in progress.

This is the answer I was looking for. I'm a hero here.

SolidSoils commented 3 years ago

@zoopyserg, this SimpelTest is part of a basic and informal tool I only created for testing the software. I hope to fix the documentation soon and add some proper examples though.

Besides, IFirmata.SetAnalogReportMode does not trigger the callback. It sends a message to the Arduino board telling it to start or stop sending analog state change messages. The ArduinoSession object routes these messages to the OnAnalogStateReceived event.

Regarding the open connections: it is the Windows OS that is keeping serial port connections open for some time. There is no (easy) way to work around that behaviour as it is hidden somewhere in the system32 dll's. Serial port connections are closed asynchronously by the OS, so even when you close the connection in .NET, behind the scenes it still remains open for some time.