I'm using RFduino to do analogRead at pin 2 and pin 0. Pin 2 works perfectly, and I got the result. However, for pin 0 it returns all zero.
#include <RFduinoBLE.h>
static const int topPin = 2;
static const int bottomPin = 1;
int top;
int bottom;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(topPin, INPUT);
pinMode(bottomPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
RFduino_ULPDelay( SECONDS(0.01) );
bottom = analogRead(bottomPin);
top = analogRead(topPin);
Serial.print(top);
Serial.print(" ");
Serial.print(bottom);
Serial.print("\n");
}
I double checked the wires and it's OK. I measured the voltage across pin 0 and it's 3.3V. How do I fix this problem? There's no shortage between pin 0 and 3.3V.
2 Answers 2
Without RFDuino specs handy, I'm guessing - but I think it's likely - that the A/D converter gets shut down in an ultra low power sleep and requires some amount of time to wake up. The mis-reading pin, pin 0, is the one your sketch reads immediately on waking. That read call probably takes long enough that the A/D wakes up by the time your read pin 2 so it gives a better reading.
Try switching the order in which you read the pins and see if pin 2 mis-reads if you try to read it immediately after waking. If it does, the cure may be as simple as a 10-100 uSec delay between waking and trying to read any analog data. I just pulled that number out of the air; you might start with a couple of milliseconds and see how low you can make it before you start to see errors, then double that amount.
-
Thanks for your confidence, but unless this actually worked, I think @edgar nailed this one.JRobert– JRobert2016年09月08日 11:43:02 +00:00Commented Sep 8, 2016 at 11:43
I couldn't find an usable datasheet for the RFduino, seems like a very poorly documented board. But I found an enlightening comment in the source code of the RFduino library:
Only pins 1-6 is avaliable [sic] for using as ADC inputs
If you read the source, it is clear that calling analogRead()
on an
inappropriate pin is supposed to return 0.
A0
,A1
... rather than the number.A0, A2
rather than0
and2
?