I have a simple Arduino sketch which I have been playing with on my Uno.
The idea is simple, the user turns a potentiate (pin A2), when they pass a certain threshold a LED is turned on (pin 7).
Here is the code:
// Digital pins
const byte ledPin = 7;
// Analog pins
const byte userInputPotPin = A2;
// Potentiometer constants
const int userInputPotMinimumValue = 0;
const int userInputPotMaximumValue = 1023;
const int userInputMinimumValue = 1;
const int userInputMaximumValue = 100;
// Variables
const int threshold = 90;
void setup()
{
Serial.begin(9600);
// Digital pins
pinMode(ledPin, OUTPUT);
// Analog pins
pinMode(userInputPotPin, INPUT);
}
void loop()
{
int rawValue = analogRead(userInputPotPin);
int mappedValue = map(rawValue, userInputPotMinimumValue, userInputPotMaximumValue, userInputMinimumValue, userInputMaximumValue);
String messageText;
messageText += "Raw: " + String(rawValue) + "\t";
messageText += "Mapped:" + String(mappedValue);
if(mappedValue > threshold)
{
messageText += "\tThreshold exceeded!";
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
Serial.println(messageText);
delay(100);
}
The problem is that when it is run the hardware is very unresponsive - I can turn the pot, but I don't see the light change for several minutes. The log messages in the serial monitor also seem to be lagging behind.
I have tried:
* Removing the Serial statements (so there is no serial communication when the sketch is running), and just observing the LED. This makes no difference.
* Increasing the value in the delay up to 1000. This makes no difference in the responsiveness.
However, when I run the sketch on my Mega2560 the problem goes away and the circuit is as responsive as I expect.
Is there something wrong with my Uno board?
void loop(){Serial.println(analogRead(A2));delay(100);}?