Coder Social home page Coder Social logo

analogRead(string) fails about remote-wiring HOT 7 CLOSED

ManniAT avatar ManniAT commented on August 16, 2024
analogRead(string) fails

from remote-wiring.

Comments (7)

turkycat avatar turkycat commented on August 16, 2024

The analog pins on Arduino and similar devices are numbered after the
digital pins, so "A0" should become 14 on an Arduino uno because there are
14 digital pins numbered 0-13, hence the analog offset. What type of device
are you using?

Jesse

On Sat, Aug 15, 2015, 12:01 PM ManniAT [email protected] wrote:

When I call remDevice.analogRead("A0"); I get an error result.

Debugging the source I found that in Line 132 _analog_offset is added.
For my case this means "A0" - becomes 18
analogRead(18) is called.
And there at line 99 the _analog_offset is added again...

90 uint16_t
91 RemoteDevice::analogRead(
92 uint8_t pin_
93 )
94 {
95 //critical section equivalent to function scope
96 std::lock_guardstd::recursive_mutex lock( _device_mutex );
97

98 uint16_t val = -1;
99 uint8_t analog_pin_num = pin_ + _analog_offset;
100

101 if( _pin_mode[ analog_pin_num ] != static_cast( PinMode::ANALOG ) )
102 {
103 if( _pin_mode[ analog_pin_num ] == static_cast( PinMode::INPUT ) )
104 {
105 pinMode( analog_pin_num, PinMode::ANALOG );
106 }
107 else
108 {
109 return static_cast( val );
110 }
111 }
112

113 if( pin_ <

_num_analog_pins ) 114 { 115 val = analog_pins[ pin ];
116 }
117

118 return val;
119 }
120

121 uint16_t
122 RemoteDevice::analogRead(
123 Platform::String^ analog_pin_
124 )
125 {
126 uint8_t parsed_pin = parsePinFromAnalogString( analog_pin_ );
127 if( parsed_pin == static_cast( -1 ) )
128 {
129 return static_cast( -1 );
130 }
131

132 return analogRead( static_cast( parsed_pin ) + _analog_offset );
133 }
134


Reply to this email directly or view it on GitHub
#46.

from remote-wiring.

ManniAT avatar ManniAT commented on August 16, 2024

Hi Jesse,

I know what you mean - an my Yun A0 is pin 18.
Try to examine the code...

I call analogRead("A0").
In this method parsed_pin becomes correctly 0 - then (on line 132) analogRead is called with parsed_pin + _analog_offset (which is correctly 18 on my yun).
So this results in the call analogRead(18)

At line 99 the _analog_offset is again added.
So _analog_pin_num becomes 36 - FAIL.

from remote-wiring.

ManniAT avatar ManniAT commented on August 16, 2024

Just to be more precise - it doesn't matter if the offset is 14 or 18 or whatever.
The offset is added twice this is the problem

from remote-wiring.

zfields avatar zfields commented on August 16, 2024

HI ManniAT,

As a work around, you can use pin 18 directly on your call to pinMode(18, PinMode::ANALOG), then read it using analogRead(0).
It is confusing, but it reflects the underlying implementation of Firmata, and it will work for you until we get a hot fix out the door. We are glad you're using the library, and thanks for reporting the issue!

from remote-wiring.

ManniAT avatar ManniAT commented on August 16, 2024

Hi zfields,

I simply changed line 132 from
return analogRead( static_cast( parsed_pin ) + _analog_offset );
To
return analogRead( static_cast( parsed_pin ) );

And everything works as expected.

from remote-wiring.

zfields avatar zfields commented on August 16, 2024

Awesome news! Would you like to submit a pull-request? We would love to have your contribution. Either way we should have this fixed by the end of the week, but we would love to give you the opportunity to become a contributor.

from remote-wiring.

ManniAT avatar ManniAT commented on August 16, 2024

Ok, I did it.. Request is submitted

from remote-wiring.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.