Git Product home page Git Product logo

Comments (3)

MattGlobal avatar MattGlobal commented on August 15, 2024

Hello @dhalbert , i modified your text and it seems to be working :

void setup() 
{
Serial.begin(9600);
pinMode(A0, INPUT);
}

void loop() {
  // Attach A0 to 3.3V through a 220K or other high-value resistor,
  // so it's not stronger than the driven output.

 delay(100);
  Serial.println(analogRead(A0));



delay(100);
  Serial.println("Done");

}

the problem is still the same with touchsceen... but i'm wondering if it's not just about a pins definition problem ?

from arduinocore-mbed.

dhalbert avatar dhalbert commented on August 15, 2024

@MattGlobal In the example you gave, there is no pinMode() between analogRead() calls. In my example, the pin usage alternates between pinMode() and analogRead(). My hypothesis is that the second call to analogRead() is assuming the pin is already set up where as in fact its settings where changed "behind the back" of analogRead().

from arduinocore-mbed.

dhalbert avatar dhalbert commented on August 15, 2024

Here is the impl:

int analogRead(pin_size_t pin)
{
if (pin >= PINS_COUNT) {
return -1;
}
PinName name = analogPinToPinName(pin);
if (name == NC) {
return -1;
}
mbed::AnalogIn* adc = analogPinToAdcObj(pin);
if (adc == NULL) {
adc = new mbed::AnalogIn(name);
analogPinToAdcObj(pin) = adc;
#ifdef ANALOG_CONFIG
if (isAdcConfigChanged) {
adc->configure(adcCurrentConfig);
}
#endif
}
return (adc->read_u16() >> (16 - read_resolution));
}

The first time through, a new mbed::AnalogIn object is created. That's reused on subsequent calls.

A workaround might be to call analogUpdate() before every call to analogRead(). I'll try that. (EDIT: ANALOG_CONFIG isn't defined, so analogUpdate() is not included, so it requires some surgery in the sources, which is not a clean workaround for the average user.) But there's an assumption being made in the code in this file that the pin state is not changing out from under analogRead().

from arduinocore-mbed.

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.