Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading analog input value from Arduino #34

Open
larsterje opened this issue Jan 16, 2020 · 1 comment
Open

Reading analog input value from Arduino #34

larsterje opened this issue Jan 16, 2020 · 1 comment

Comments

@larsterje
Copy link

Hi. I have played around several hours with this library using PowerShell, and I'm able to program digital output to LED connected to differnt pins. But I'm trying to read the analog value from a potmeter connected to an analog pin, and I'm not able to get my head around how this works with the state monitors and the events.
Is there anyone that could help me with an example of running Powershell code for continous reading an analog value from an analog input pin on an Arduino? I just need some code to understand how it is all connected, and then I can work it from there.
It would be highly appriciated

@bradmartin333
Copy link

Hi @larsterje, one method is to add an event handler for Session.MessageReceived or Session.AnalogStateReceived. That works, but it is nice to implement the IObserver<AnalogState> interface along with its 3 methods. Here is a snippet of C# code that might help you. It shows how to setup the analog pin as well as subscribe to the state monitor. You can do what you need with the value in the OnNext method.

internal class IO_Sample : IController, IObserver<AnalogState>
{
    private ArduinoSession? Session { get; set; }
    private int AnalogTest;

    public void Initialize(ArduinoSession session)
    {
        Session = session;
        Session.SetAnalogReportMode(5, true); // A5
        Session.CreateAnalogStateMonitor().Subscribe(this);
    }

    public void OnCompleted() { }

    public void OnError(Exception error) { }

    public void OnNext(AnalogState state)
    {
        Console.WriteLine($"pin {state.Channel} reads {state.Level}");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants