Hi, I am trying to write an Arduino program for a stepper motor and temperature sensors. I am using Arduino MKR1000, two DS18B20 temperature, two photoelectric sensors as switches, and NEMA 17-like stepper motor with an A4988 stepper motor driver.
I want my program to take temperature readings continuously. Also, when a user inputs, let's say 's', I want the stepper motor to go forward until the first switch is activated and then go back until the second switch is activated. Later, it will go further just a little bit until the second switch is deactivated then it will stop. Everything works almost as I want. However, I still have a problem. My problem is while the stepper motor moves I cannot get the temperature readings. I want to get temperature readings while the stepper motor moves. How can I achieve this? I also don't want to use AccelStepper if possible because I think it isn't fully compatible with my stepper motor. Thanks
Here is what I have tried so far:
#include <OneWire.h> // library for DS18B20 temperature sensors
#include <DallasTemperature.h>
// define variables for the temperature sensors, the stepper motor driver, and the photoelectric sensors
#define ONE_WIRE_BUS_1 6
#define ONE_WIRE_BUS_2 7
#define stepPin 8
#define dirPin 9
#define switchLMinus 13
#define switchLPlus 10
// define the speed of the stepper motor (in microseconds per step)
const int stepSpeed = 800;
// Define the state variables
unsigned long lastReading = 0;
const unsigned long interval = 100;
OneWire oneWire1(ONE_WIRE_BUS_1);
OneWire oneWire2(ONE_WIRE_BUS_2);
DallasTemperature sensor1(&oneWire1);
DallasTemperature sensor2(&oneWire2);
void setup() {
// set the direction and step pin as an output
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// set the limit switch pins as inputs
pinMode(switchLMinus, INPUT_PULLUP);
pinMode(switchLPlus, INPUT_PULLUP);
// setup home position
// move the stepper motor until limit switch minus is activated
digitalWrite(dirPin, LOW);
while (digitalRead(switchLMinus) == LOW) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepSpeed);
}
delay(50);
// move the stepper motor a little further until limit switch minus is deactivated
digitalWrite(dirPin, HIGH);
while (digitalRead(switchLMinus) == LOW) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepSpeed);
}
// start the serial communication
Serial.begin(115200);
// start up the temerature library
sensor1.begin();
sensor2.begin();
}
void loop() {
// read temp values
if (millis() - lastReading >= interval) {
temperatureReadings();
lastReading = millis();
}
// check if the user has entered any input
if (Serial.available()) {
char userInput = Serial.read();
if (userInput == 's') {
// Call the flipCheck function
moveMotor();
}
}
}
void moveMotor() {
// set the direction of the stepper motor to forward
digitalWrite(dirPin, HIGH);
// move the stepper motor until limit switch plus is activated
while (digitalRead(switchLPlus) == LOW) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepSpeed);
}
// reverse the direction of the stepper motor
digitalWrite(dirPin, LOW);
// move the stepper motor until limit switch minus is activated
while (digitalRead(switchLMinus) == LOW) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepSpeed);
}
delay(50);
// move the stepper motor a little further until limit switch minus is deactivated
digitalWrite(dirPin, HIGH);
while (digitalRead(switchLMinus) == HIGH) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepSpeed);
}
}
// get temperature values from the sensors
void temperatureReadings() {
sensor1.requestTemperatures();
sensor2.requestTemperatures();
Serial.print(sensor1.getTempCByIndex(0));
Serial.print(",");
Serial.println(sensor2.getTempCByIndex(0));
}