Hello everyone,
I have a problem when running a stepper motor (Nema 17-04) with a lcd display and a light sensor. When running the Motor alone, it runs smoothly without any troubles. But as soon as i add other parts that i need for the project, it starts to rotate slower.
I have made a wiring diagramm and will include the code i used.
#include <Wire.h>
#include "Adafruit_VL6180X.h"
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <TimerOne.h>
const int SetPotPin = A2;
// Define pin connections
const int dirPin = 8;
const int stepPin = 9;
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
Adafruit_VL6180X vl = Adafruit_VL6180X();
LiquidCrystal_I2C lcd(0x27, 16, 2);
const unsigned long UpdateInterval = 50;
unsigned long LastUpdate = 0;
const unsigned long UpdateInterval_2 = 1000;
volatile bool timerFlag = false;
const int numReadings = 10;
int readings[numReadings]; // the readings from the sensor
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
lcd.begin();
if (!vl.begin()) {
lcd.println("Failed to find sensor");
while (1);
}
lcd.println("Sensor found!");
delay (1000);
lcd.setCursor(0, 0);
lcd.print("Set: ");
lcd.setCursor(0, 1);
lcd.print("Sens: ");
// set the maximum speed
myStepper.setMaxSpeed(1000);
// configure timer interval
Timer1.initialize(UpdateInterval * 1000);
Timer1.attachInterrupt(timerISR);
}
void loop() {
int posDiff;
if (timerFlag) { //Sensor Interrupt------------------------------------------------------
// take multiple readings and average them
total = total - readings[readIndex];
readings[readIndex] = vl.readRange();
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
if (average > 10) {
average = average + 9;
}
timerFlag = false;
}
if (millis() - LastUpdate >= UpdateInterval_2) {//Pot und Display----------------------------------------------
//Read the value from the potentiometer
int SetPinValue = analogRead(SetPotPin);
// Convert the value to a distance value from 0mm to 100mm
int set = map(SetPinValue, 0, 1023, 0, 100);
lcd.setCursor(5, 0);
lcd.print(set, DEC);
lcd.print("mm ");
uint8_t status = vl.readRangeStatus();
if (status == VL6180X_ERROR_NONE) {
lcd.setCursor(5, 1);
lcd.print(average, DEC);
lcd.print("mm ");
}
else if ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
lcd.println("System error");
}
LastUpdate = millis();
posDiff = set - average;
}
if (posDiff > 3){
myStepper.setAcceleration(100);
myStepper.setSpeed(1000);
myStepper.run();
}
else if (posDiff < -3) {
myStepper.setAcceleration(100);
myStepper.setSpeed(-1000);
myStepper.run();
}
else if (posDiff >= -3 && posDiff <= 3) {
myStepper.setSpeed(0);
myStepper.run();
}
}
void timerISR() {
timerFlag = true;
}
do you have any suggestions or improvments for the code?
Regards
Ali