Whole program with working smoothing on one analog pin.
//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
#include <AccelStepper.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(53, 50, 45, 41, 33, 31);
//_____________________________________________________________
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;
enum motorStateENUM {
WAITING,
FIRSTRUN,
BETWEENRUNS,
SECONDRUN,
SECONDRUNDONE,
THIRDRUN,
FINISHED,
};
motorStateENUM motorState = WAITING;
//_______________________________________________________________
//LDR
const int ledPinLDR = 52;
const int crashPin1 = A1;
int crashPin1State;
boolean crashButtonPressed = false;
//ACTUATOR
AccelStepper stepperA(AccelStepper::DRIVER, 4, 3); // (DRIVER, Step, Directon)
int enableA = 2;
const int homePinA = 26;
int homePin2StateA;
int potPinA = A4;
int motorSpeedA;
int mapMinSpeedA = 2000;
int mapMaxSpeedA = 3500;
//STARTBUTTON
const int startButton = 39;
int startButtonState = 0; //1 = open->not pressed
//STRATBUTTON TOGGLE
int startButtonReading; // the current reading from the input pin
int previouStartButtonReading = 0; // the previous reading from the input pin
//NEMA 23 (LINEAR MOTION MOTOR)
AccelStepper stepperL(AccelStepper::DRIVER, 7, 8); // (DRIVER, Step, Directon)
int enableL = 9;
const int homePinL = 34;
int homePinStateL;
int potPinL = A0;
int motorSpeedL;
int mapMinSpeed = 1500;
int mapMaxSpeed = 3000;
boolean directionL;
boolean atStartPosition;
//SERVO
#include <known_16bit_timers.h>
#include <Adafruit_TiCoServo.h>
Adafruit_TiCoServo myservo;
int servo = 11;
int pos = 0;
//STABELIZING ANALOG READ
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int total = 0; // the running total
//STEPPERL
int readIndexL = 0; // the index of the current reading
int averageL = 0; // the average
//STEPPERA
int readIndexA = 0; // the index of the current reading
int averageA = 0; // the average
//SPEEDCONTROL
int EnterLED = 42;
int EnterButton = 51;
void setup() {//================================================================
lcd.begin(16, 2);
lcd.print("STYLEMATHOT");
lcd.setCursor(0, 1);
lcd.print("WRAPPER 2000!");
pixels.begin();
myservo.attach(servo);
myservo.write(2);
delay(500);
pinMode(EnterLED,OUTPUT);
pinMode(EnterButton,INPUT);
pinMode(potPinL, INPUT);
pinMode(potPinA, INPUT);
pinMode(ledPinLDR, OUTPUT);
pinMode(crashPin1, INPUT);
pinMode(startButton, INPUT_PULLUP);
pinMode(homePinA, INPUT_PULLUP);
pinMode(homePinL, INPUT_PULLUP);
pinMode(enableA, OUTPUT);
pinMode(enableL, OUTPUT);
digitalWrite(enableA, HIGH);
digitalWrite(enableL, HIGH);
Serial.begin(115200);
delay(2000);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
homePin2StateA = digitalRead(homePinA);
homePinStateL = digitalRead(homePinL);
if (homePin2StateA == 0 or homePinStateL == 1) {
atStartPosition = false;
}
else {
atStartPosition = true;
for (int i = 0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0, 150, 0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
lcd.clear();
}
void loop() {//========================================================
stepperA.setMaxSpeed(motorSpeedA);
stepperA.setAcceleration(30000);
if (digitalRead(EnterButton) == 1 or millis() < 6000){
total = total - readings[readIndexL];
readings[readIndexL] = map(analogRead(potPinL), 0, 1023, mapMinSpeed, mapMaxSpeed);
total = total + readings[readIndexL];
readIndexL = readIndexL + 1;
if (readIndexL >= numReadings) {
readIndexL = 0;
}
averageL = total / numReadings;
// Serial.print(averageL);
//Serial.print("-");
delay(1); // delay in between reads for stability
motorSpeedL = averageL;//map(analogRead(averageL), 0, 1023, mapMinSpeed, mapMaxSpeed);
motorSpeedA = map(analogRead(potPinA), 0, 1023, mapMinSpeedA, mapMaxSpeedA);
//Serial.print(map(motorSpeedL,mapMinSpeed,mapMaxSpeed,0,100));
int motorSpeedLDisplay = map(motorSpeedL,mapMinSpeed,mapMaxSpeed,20,99);
//Serial.print("-");
//Serial.println(motorSpeedLDisplay);
lcd.setCursor(0, 0);
lcd.print("Links:");
lcd.print(motorSpeedLDisplay);
lcd.setCursor(0, 1);
lcd.print("Rechts:");
lcd.print(motorSpeedA);
digitalWrite(EnterLED,HIGH);
}
else
digitalWrite(EnterLED,LOW);
startbutton();
crashPin1State = analogRead(crashPin1);
homePin2StateA = digitalRead(homePinA);
homePinStateL = digitalRead(homePinL);
//////////////////////////READ THE LDR/////////////////////////////////////////
if (crashPin1State <= 25 and homePin2StateA == 1 )
{
digitalWrite(ledPinLDR, HIGH);
crashButtonPressed = true;
}
else {
digitalWrite(ledPinLDR, LOW);
}
////////////////////////ENABLE OR DISABLE STAPPER-L///////////////
if (homePinStateL == 0 && startButtonState == 1)
{
digitalWrite(enableL, HIGH);
}
else {
digitalWrite(enableL, LOW);
}
//////////////////////////STEPPER L DIRECTION/////////////////////////////////////////
if (homePinStateL == 0 and stepperL.currentPosition() == 0)
{
directionL = 1;
}
if (stepperL.currentPosition() == 4920)
{
directionL = 0;
}
//////////////////////////FORWARD MOVE STEPPER L/////////////////////////////////////////
if (directionL == 1)
{
if (startButtonState == 0 && homePin2StateA == 1 && crashButtonPressed == false
&& stepperL.currentPosition() == 0)
{
stepperL.setMaxSpeed(motorSpeedL);
stepperL.setAcceleration(motorSpeedL * 2);
stepperL.move(2600);
}
if (startButtonState == 0 && crashButtonPressed == false
&& stepperL.currentPosition() == 2600 && stepperL.targetPosition() == 2600)
{
stepperL.setMaxSpeed(motorSpeedL - 1050);
stepperL.setAcceleration(motorSpeedL * 2);
stepperL.setSpeed(motorSpeedL/2);
stepperL.moveTo(4920);
}
}
if (directionL == 0)
{
if (homePinStateL == 1) {
//homePinStateL = digitalRead(homePinL);
if (stepperL.currentPosition() == 4300) {
for (pos = 0; pos <= 80; pos += 1) {
myservo.write(pos);
}
}
if (stepperL.currentPosition() == 200) {
for (pos = 80; pos >= 50; pos -= 1) {
myservo.write(pos);
}
}
if (stepperL.currentPosition() < 500) {
stepperL.setMaxSpeed(500);
stepperL.setSpeed(-500);
stepperL.runSpeed();
}
if (stepperL.currentPosition() > 201) {
stepperL.setMaxSpeed(1600);
stepperL.setSpeed(-1600);
stepperL.runSpeed();
}
}
if (homePinStateL == 0)
{
stepperL.setCurrentPosition(0);
startButtonState = 1;
for (pos = 50; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
}
stepperL.run();
stepperL.runSpeed();
///////////////////////////////ACTUATOR MOVE//////////////////////////
///// Here one stepper does a move forward and then backword.
}