millis and analogRead compatibility

I have built a few simple projects using the analogRead function to test the temperature through a thermistor, or weight through a load cell, which use the millis function to keep track of time. I used the blinkWithoutDelay example sketch, and just placed my analogRead function inside the "if(currentMillis - previousMillis > interval)". I use generally 60000 for the interval, to read the sensor every minute. Everything works fine at first, but then anywhere from a couple hours to a few days in, the arduino freezes in a random state, and will not do anything untill it is restarted. I don't have this problem unless I am using both the analogRead and millis in the same sketch. Is there anything to this?

Is there anything to this?

No, there is no fundemental incompatablities or restrictions with using millis() and analog reads functions in the same sketch. Your problem lies elsewhere. Posting you sketch is most likely the best path that will help.

Lefty

thanks for the reply, I couldn’t find anything on the forums, so I figured as much. This is the latest sketch I have been having this issue with…

// A 12 hour clock based on a DC Geared motor, ~4rpm, and a potentiometer for measuring the
// position of the clock hand. The clock must be powered on at 12:00, or the targetPosition
// variable must be initialised to the time in miliseconds it will be powered on.

const int positionSignalPin = 0; //Potentiometer to pin A0 for rotation measurement
const int positionSamples = 8; //number of samples to be averaged for each reading

const int high1Pin = 2; //H-Bridge high side to pin D2
const int high2Pin = 3; //other H-Bridge high side to pin D3
const int low1Pin = 4; //H-Bridge low side to pin D4
const int low2Pin = 5; //other H-Bridge low side to pin D5
const int greenLedPin = 11; //LED indicator, lit when at correct position, to pin D11
const int ledPin = 13; //LED indicator, lit for one second, off for one second, to pin D13

unsigned int targetPosition = 0; //ranges between 0 for 12:00, and 43199 for 11:59

int ledState = LOW; //toggles on and off to indicate seconds ticking
long previousMillis = 0;

long interval = 1000; //1000 miliseconds = 1 second

void setup()
{
pinMode(high1Pin, OUTPUT);
digitalWrite(high1Pin, HIGH);
pinMode(low1Pin, OUTPUT);
digitalWrite(low1Pin, LOW);
pinMode(high2Pin, OUTPUT);
digitalWrite(high2Pin, HIGH);
pinMode(low2Pin, OUTPUT);
digitalWrite(low2Pin, LOW);

pinMode(greenLedPin, OUTPUT);
digitalWrite(greenLedPin, LOW);
pinMode(ledPin, OUTPUT);
}

void loop()
{
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);

targetPosition++;
int goToPosition = map(targetPosition, 0, 43199, 1, 600); //convert the time in miliseconds to the
//desired range of analog values
if (targetPosition > 43199) // sweeps the clock hand back to the start position when 12hrs has elapsed
{
targetPosition = 0;
goToPosition = map(targetPosition, 0, 43199, 1, 600);
int currentPosition = positionRead();
while (currentPosition > goToPosition)
{
hBridge(3);
currentPosition = positionRead();
}
hBridge(0);
}
int positionNow = positionRead();
while (positionNow < goToPosition)
{
hBridge(2);
positionNow = positionRead();
}
hBridge(0);
}
blinkGreenLed(2);
}

void blinkGreenLed(int cycles)
{
for(int i = 0; i < cycles; i++)
{
digitalWrite(greenLedPin, HIGH);
delay(10);
digitalWrite(greenLedPin, LOW);
delay(10);
}
}

void hBridge(int x)
{
if(x == 0) // COAST
{
digitalWrite(high1Pin, HIGH);
digitalWrite(low1Pin, LOW);
digitalWrite(high2Pin, HIGH);
digitalWrite(low2Pin, LOW);
}
if(x == 1) // STOP
{
digitalWrite(high1Pin, HIGH); //puts the H-Bridge into the coast state for safety between each state change
digitalWrite(low1Pin, LOW); // ----
digitalWrite(high2Pin, HIGH); // ----
digitalWrite(low2Pin, LOW); // ----
digitalWrite(high1Pin, HIGH); //now puts the H-Bridge into the “locked” state
digitalWrite(low1Pin, HIGH);
digitalWrite(high2Pin, HIGH);
digitalWrite(low2Pin, HIGH);
}
if(x == 2) // FORWARD
{
digitalWrite(high1Pin, HIGH);
digitalWrite(low1Pin, LOW);
digitalWrite(high2Pin, HIGH);
digitalWrite(low2Pin, LOW);
digitalWrite(high1Pin, LOW);
digitalWrite(low1Pin, HIGH);
digitalWrite(high2Pin, HIGH);
digitalWrite(low2Pin, LOW);
}
if(x == 3) // BACKWARD
{
digitalWrite(high1Pin, HIGH);
digitalWrite(low1Pin, LOW);
digitalWrite(high2Pin, HIGH);
digitalWrite(low2Pin, LOW);
digitalWrite(high1Pin, HIGH);
digitalWrite(low1Pin, LOW);
digitalWrite(high2Pin, LOW);
digitalWrite(low2Pin, HIGH);
}
}

int positionRead(void)
{
long positionMeasurement = 0;
for(int i = 0; i<positionSamples; i++)
{
positionMeasurement += analogRead(positionSignalPin);
delay(1);
}
positionMeasurement = positionMeasurement / positionSamples;
return(positionMeasurement);
}

just to clarify, I know that this wont keep accurate time because of the way the targetPosition variable is updated, I just wanted to test a home made servo for use in later projects.

The way you have it written it can only run backward when (targetPosition > 43199).

I thin what you intended was:

    targetPosition++;
     if (targetPosition > 43199)  // sweeps the clock hand back to the start position when 12hrs has elapsed
      targetPosition = 0;

   int goToPosition = map(targetPosition, 0, 43199, 1, 600);  //convert the time in miliseconds to the 
                                                               //desired range of analog values

      int currentPosition = positionRead();
      while (currentPosition > goToPosition)
      {
        hBridge(3);
        currentPosition = positionRead();
      }
      hBridge(0);

yes, I found a couple problems with signed/unsigned variables so that was probably the problem. I am retrying now with the code fixes...

I FOUND THE PROBLEM. I have had a similar problems of the arduino randomly freezing / iradic behaviour, it was my power supply. I was using a wall-wart style switch mode. Once I switched over to a transformer style no more problems. :-)