Skip to content

Reading analog input value from Arduino #34

Open

Description

@larsterje

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

Activity

  1. bradmartin333 commented on Nov 30, 2022

    @bradmartin333

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions