Why is this analog adjust timer buggy?

I have watched the AnalogInOutSerial and I get good readings. However this program is unpredictable. If I turn the pot to an analog value of 10 I get what I'd expect, however if I turn it to around 2 minutes (should be half of my 1023 or 12oc on the pot), it goes less sometimes and other times more. Maybe it's not reading the A0 sensor pin at the beginning of the program and just picking a random value?

//variable definitions
int sensorPin = A0;     // select the input pin for the potentiometer (input zero)
int ledPin = 13; // select the pin for the LED
int sensorValue = 0;    // variable to store the value coming from the sensorPin (pot)


//main program
void setup() {
pinMode(ledPin, OUTPUT);   // declare the ledPin as an OUTPUT:
 
sensorValue = analogRead(sensorPin);   // read the value from the sensor:
{
  digitalWrite(ledPin, HIGH);   // turn the LED/Output to HIGH
  delay(210000+(235*sensorValue));                // wait 210 seconds (3.5 minutes) + 235*sensorValue (analog 0-1023) for a max of 4 minutes.
  digitalWrite(ledPin, LOW);    // turn the LED/Output to LOW
}

As a test I changed the default (non adjustable minimum) time to just 1s and added the 2 minutes (half of the 1023) and it's no where near 2 minutes.

Like this:

//main program
void setup() {
pinMode(ledPin, OUTPUT);   // declare the ledPin as an OUTPUT:

sensorValue = analogRead(sensorPin);   // read the value from the sensor:
{
 digitalWrite(ledPin, HIGH);   // turn the LED/Output to HIGH
 delay(1000+(235*sensorValue));                // wait 210 seconds (3.5 minutes) + 235*sensorValue (analog 0-1023) for a max of 4 minutes.
 digitalWrite(ledPin, LOW);    // turn the LED/Output to LOW
}

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

It seems the issue is the 235 multiplier (235*sensorValue)... If I insert 100 as the multiplier, it works just as expected. EDIT: I was wrong. It doesn't work as expected with 100 as the multiplier. It does however work as expected near the lower end of the pot. It is a linear taper by the way.

No idea why this could be.

I'm using an Uno btw. Circuit wise, I have a pot with 5v to pin 3 and gnd to pin 0 with the wiper going to analog 0. The serial monitor shows all is well.

ROLLOVER!

Mark

grasping at straws here, but I think maybe it has to do with a float value. I changed the variable to a float and it worked upon the first test.

int sensorValue = 0

is now float sensorValue = 0

As I said ROLLOVER!

Mark

holmes4:
As I said ROLLOVER!

Mark

Can you translate that for me? Not familiar with that. Looked it up and it says something about 49 days. This timer is in minutes.

Looked it up and it says something about 49 days

Look again!

Mark

Where?

holmes4:
Look again!

Mark

Thank you, but where?

One thing I do know is that float has solved the issue. What I don't know is how it's related to rollover. Please expound (or at least point me in a direction) if you don't mind.

What Mark means is integer overflow.

An integer on Arduino has a maximum value of 32767, minimum -32768. Analog input values are 0 to 1023.

What do you get when when you multiply 1023 by 100?

Ahh… thank you. So much to learn. I was going nuts trying to figure out what was going wrong.