Well here's a weird one. I'm monitoring a mic on an analog pin of an Arduino Nano and using the variation in voltage to control and LED.
When plugged in to my Laptop (USB 3, 500ma available, the nano only pulls 90) the detection is flawless. When plugged into an external power source, I get no variation at all or a random, non-responsive pattern of variation. Could be I'm getting voltage values that are out of range, but of course I can't monitor them on the serial port when I'm not plugged into the laptop.
I've got a potentiometer on the circuit to increase or decrease sensitivity by changing the trigger value. I can play poke-and-try at changing the trigger values to higher and lower ranges to try and find the sweet spot, but does anybody have an explanation of why I'd see a difference in the first place? I've tried a variety of external supplies, 500ma 1a, 2a - but if it's only pulling 90 (as reported by my Macbook System Report) this shouldn't matter, right? What else could account for differing behaviour?
Here's the code:
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 5.0) / 10; // convert to volts
val = analogRead(potPin);
if (val > 0) triggervoltage = 10;
if (val > 500) triggervoltage = 11;
if (val > 800) triggervoltage = 12;
if (val > 1000) triggervoltage = 15;
Serial.println(val);
Serial.println(triggervoltage);
Serial.println(volts);
//triggervoltage =
//Serial.println(volts);
//Serial.println(val);
//THIS IS the trigger mechanism for the effect. Vary the volts value to adjust sensitivity.
if (volts > triggervoltage) [LED RESPONSE HERE]