Temperature and stepper motion

Hello, i’m just getting my feet wet with arduno and could use a little help. I’m using an arduino mega 2560 with an adafruit motor shield. i have 2 steppers i operate with one joystick. ive also got a got a ds18b20 temp sensor attached as well.

my goal is to take temp measurements in the background leaving the joystick free to operate the steppers. what i have working now the steppers move, but after T seconds (whatever time i’ve set the temp command to) it interrupts the motion and runs the temp loop.

what i need to have it do is when the joystick operates suspend that loop and start it again when the joystick is finished. i’ve tried what i can think of and cant get it to work. any help would be great. thanks

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 44


// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 24 steps per revolution (15 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myXMotor = AFMS.getStepper(24, 2);
Adafruit_StepperMotor *myYMotor = AFMS.getStepper(24, 1);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const int X_pin = A9;
const int Y_pin = A10;
const int FOV_Switch = 48;
const int Focus_Switch = 52;


int motorspeed = 1000;
int FOV_Steps;
int Focus_Steps;

int lasttemp = 0;
int newtemp = 0;
int deltat = 0;



void setup() {
//===================================================================
//*******************************************************************
//===================================================================

  Serial.begin(9600);           // set up Serial library at 9600 bps
 sensors.begin();
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myXMotor->setSpeed(10);  // 10 rpm   
  myYMotor->setSpeed(10);  // 10rpm   

//Home Sequence
//===================================================================
pinMode(FOV_Switch,INPUT);
pinMode(Focus_Switch,INPUT);

//FOV Home Sequence
//===================================================================
while (digitalRead(FOV_Switch)==HIGH) 
{myYMotor->onestep(FORWARD, SINGLE);
   delay(5);}

while(digitalRead(FOV_Switch)==LOW) 
{myYMotor->onestep(BACKWARD, SINGLE); 
 delay(5);}

//FOCUS Home Sequence
//===================================================================
 while (digitalRead(Focus_Switch)==LOW) 
{myXMotor->onestep(BACKWARD, SINGLE);
   delay(5);}

while(digitalRead(Focus_Switch)==HIGH) 
{myXMotor->onestep(FORWARD, SINGLE); 
 delay(5);}

FOV_Steps = 0;
Focus_Steps = 0;
}

void loop() {
//===================================================================
//*******************************************************************
//===================================================================

  int speed = 0;
 boolean active = false;

  //Temp Check
 //===================================================================
const unsigned long checkTime = .25 * 60 * 1000;
static unsigned long lastSampleTime = 0 - checkTime;
unsigned long now = millis();

if (now - lastSampleTime >= checkTime)
  {
  lastSampleTime += checkTime;
  sensors.requestTemperatures();
  lasttemp = newtemp;
  newtemp = sensors.getTempCByIndex(0);
  deltat=newtemp-lasttemp;
  Serial.println("The last temp was :");  
  Serial.println(lasttemp);
  Serial.println("The new temp is :");
  Serial.println(newtemp);
  Serial.println("The change in temp is :");
  Serial.println(deltat);}



 //FOCUS +Motion (X)
 //===================================================================
if (Focus_Steps < 1200) {
  if ((analogRead(X_pin) > 510) && (analogRead(X_pin) <= 900)){ 
    speed = map(analogRead(X_pin),510,900,0,(motorspeed/4));
    myXMotor->setSpeed(speed);
    myXMotor->step(1, FORWARD, DOUBLE); 
    active = true;
    Focus_Steps ++;
 lastSampleTime += checkTime;  //This is what i was using to try to "reset" the time loop
    //Serial.println(Focus_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(X_pin) > 900) && (analogRead(X_pin) <= 1000)){ 
    speed = map(analogRead(X_pin),900,1000,0,(motorspeed));
    myXMotor->setSpeed(speed);
    myXMotor->step(1, FORWARD, DOUBLE); 
    active = true;
    Focus_Steps ++;
 lastSampleTime += checkTime;
    //Serial.println(Focus_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(X_pin) > 1000)){ 
    speed = map(analogRead(X_pin),1000,1023,0,(motorspeed*2));
    myXMotor->setSpeed(speed);
    myXMotor->step(1, FORWARD, DOUBLE); 
    active = true;
    Focus_Steps ++;
 lastSampleTime += checkTime;
    //Serial.println(Focus_Steps);
    //Serial.print("\n");
}}

//FOCUS - Motion (X)
//===================================================================
if (Focus_Steps > -1050) {
  if ((analogRead(X_pin) < 490) && (analogRead(X_pin) >= 100)){
    speed = map(analogRead(X_pin),490,100,0,(motorspeed/4));
    myXMotor->setSpeed(speed);
    myXMotor->step(1, BACKWARD, DOUBLE); 
    active = true;
    Focus_Steps --;
 lastSampleTime += checkTime;
    // Serial.println(Focus_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(X_pin) < 100) && (analogRead(X_pin) >= 20)){ 
    speed = map(analogRead(X_pin),100,20,0,(motorspeed));
    myXMotor->setSpeed(speed);
    myXMotor->step(1, BACKWARD, DOUBLE); 
    active = true;
    Focus_Steps --;
 lastSampleTime += checkTime;
    //Serial.println(Focus_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(X_pin) < 20)){ 
    speed = map(analogRead(X_pin),20,0,0,(motorspeed*2));
    myXMotor->setSpeed(speed);
    myXMotor->step(1, BACKWARD, DOUBLE); 
    active = true;
    Focus_Steps --;
 lastSampleTime += checkTime;
    //Serial.println(Focus_Steps);
    //Serial.print("\n");
}}

 //FOV +Motion (Y)
 //===================================================================
if (FOV_Steps < 670) {
  if ((analogRead(Y_pin) > 501) && (analogRead(Y_pin) <= 900)){ 
    speed = map(analogRead(Y_pin),501,900,0,(motorspeed/4));
    myYMotor->setSpeed(speed);
    myYMotor->step(1, FORWARD, DOUBLE); 
    active = true;
    FOV_Steps ++;
 lastSampleTime += checkTime;
    //Serial.println(FOV_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(Y_pin) > 900) && (analogRead(Y_pin) <= 1000)){ 
    speed = map(analogRead(Y_pin),900,1000,0,(motorspeed));
    myYMotor->setSpeed(speed);
    myYMotor->step(1, FORWARD, DOUBLE); 
    active = true;
    FOV_Steps ++;
 lastSampleTime += checkTime;
    //Serial.println(FOV_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(Y_pin) > 1000)){ 
    speed = map(analogRead(Y_pin),1000,1023,0,(motorspeed*2));
    myYMotor->setSpeed(speed);
    myYMotor->step(1, FORWARD, DOUBLE); 
    active = true;
    FOV_Steps ++;
 lastSampleTime += checkTime;
    //Serial.println(FOV_Steps);
    //Serial.print("\n");
}}

//FOV - Motion (Y)
//===================================================================
if (FOV_Steps > -620) {
  if ((analogRead(Y_pin) < 490) && (analogRead(Y_pin) >= 100)){ 
    speed = map(analogRead(Y_pin),490,100,0,(motorspeed/4));
    myYMotor->setSpeed(speed);
    myYMotor->step(1, BACKWARD, DOUBLE); 
    active = true;
    FOV_Steps --;
 lastSampleTime += checkTime;
    //Serial.println(FOV_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(Y_pin) < 100) && (analogRead(Y_pin) >= 20)){ 
    speed = map(analogRead(Y_pin),100,20,0,(motorspeed));
    myYMotor->setSpeed(speed);
    myYMotor->step(1, BACKWARD, DOUBLE); 
    active = true;
    FOV_Steps --;
 lastSampleTime += checkTime;
    //Serial.println(FOV_Steps);
    //Serial.print("\n");
  }
  if ((analogRead(Y_pin) < 20)){ // Step only if Limit_X1 is open (HIGH)
    speed = map(analogRead(Y_pin),20,0,0,(motorspeed*2));
    myYMotor->setSpeed(speed);
    myYMotor->step(1, BACKWARD, DOUBLE); 
    active = true;
    FOV_Steps --;
 lastSampleTime += checkTime;
    //Serial.println(FOV_Steps);
    //Serial.print("\n");
}}


//===================================================================
if (!active)
   {myXMotor->release();
    myYMotor->release();
   }

//===================================================================

}

The DallasTemperature library defaults to synchronous read. You can change that using setWaitForConversion, which means you can just manage the delay between requesting and reading yourself. Then there's no need to suspend operation of the joystick.

On the first pass through loop(), this:

static unsigned long lastSampleTime = 0 - checkTime;

Will equal 4294952296, is that what you expected?

Have a look at how the code is organized in any short single purpose functions in Planning and Implementing a Program

By breaking your code into several short single-purpose functions it will be much easier to develop, test, debug and maintain.

When using millis() for managing time ALWAYS use subtraction as in if (millis() - startTime >= interval). This is also illustrated in the tutorial.

One thing you may need to deal with is the possibility that the Dallas library is very slow and calls to it block other activities. I believe that is just because of the way it was written rather than any intrinsic problem with the sensor.

...R