Hi, I have a wobble varnishing machine with the following code.
The code works so that when the varnishing carriage is up, pressing a button causes the carriage to go down. When reaching the intermediate sensor, the speed of movement can be adjusted with a potentiometer. When reaching the lower sensor, the carriage stops. The program has a programmable (delay2000) lower limit timer. I need a 10k potentiometer-operated timer 1-10s to replace this. I can implement this electrically, but I can't make Arduino code. Could someone help me with this? ![]()
google translate code
< CODE/ >
// define the stepper motor driver pins 9 stepping, 8 direction
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
// define the button pins for the arduino
#define ON_BUTTON_PIN 4
#define UPPER_LIMIT_PIN 3
#define LOWER_LIMIT_PIN 2
#define DECELERATION_PIN 5
// define the speed control potentiometer analog pin
#define SPEED_PIN 0
// define the maximum and minimum step speed per second (scale the potentiometer to this range)
#define MAX_SPEED 20000
#define MIN_SPEED 100
bool deceleration;
void setup() {
// adjust the maximum speed of the stepper motor
stepper1.setMaxSpeed(40000.0);
deceleration = false;
// Set the button type of the sensors and the push button. input_pullupit means that the operation implemented with pull-up resistors is used
pinMode(PAALLENAPPI_PIN, INPUT_PULLUP);
pinMode(YLARAJA_PIN, INPUT_PULLUP);
pinMode(ALARAJA_PIN, INPUT_PULLUP);
pinMode(PAALLENAPPI_PIN, INPUT_PULLUP);
}
void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// 1 carriage moves up
// -1 carriage moves down
// 0 carriage stops
// if the sensor or pushbutton is active, the next character is written to the program(sign)
if ((digitalRead(ON_BUTTON_PIN) == 1) && (digitalRead(UPPER_LIMIT_PIN) == 1)) {
sign = -1;
}
if ((digitalRead(ON_BUTTON_PIN) == 1) && (digitalRead(UPPER_LIMIT_PIN) == 0)) {
sign = 1;
}
if ((sign == 1) && (digitalRead(UPPER_LIMIT_PIN) == 1)) {
sign = 0;
}
if (((digitalRead(LOWER_LIMIT_PIN) == 1)) && (sign != 1)) {
sign = 1;
delay(4000);
}
if ((digitalRead(DELAY_PIN) == 1) && (sign == -1)) {
delay = true;
}
if ((digitalRead(DELAY_PIN) == 1) && (sign == 1)) {
delay = false;
}
if (slowdown) {
// the speed is read occasionally (because it takes a long time in the program
// and we don't want to do it all the time in the main loop)
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
}
// scale the potentiometer value from minimum to maximum
current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
}
else {
current_speed = sign * (((1) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
}
// update the stepper motor speed to the new value
stepper1.setSpeed(current_speed);
// this will run the stepper motor at a constant speed
stepper1.runSpeed();
}
< CODE/ >
original code
< CODE/ >
#include <AccelStepper.h>
// määritetään askelmoottorin ohjaimen pinnit 9 askellus, 8 suunta
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
// määritetään painikkeiden pinnit arduinolle
#define PAALLENAPPI_PIN 4
#define YLARAJA_PIN 3
#define ALARAJA_PIN 2
#define HIDASTUS_PIN 5
// määritetään nopeus säädön potikka analogia pinni
#define SPEED_PIN 0
// määritetään maksimi ja minimi askelnopeus sekuntia kohden (skaalaa potikan tähän väliin)
#define MAX_SPEED 20000
#define MIN_SPEED 100
bool hidastus;
void setup() {
// säädetään askelmoottorin maximi pyörimisnopeus
stepper1.setMaxSpeed(40000.0);
hidastus = false;
// Asetetaan antureiden ja painonapin nappityyppi. input_pullupit tarkoittaa, että käytössä ylösvetovastuksilla toteutettu toiminta
pinMode(PAALLENAPPI_PIN, INPUT_PULLUP);
pinMode(YLARAJA_PIN, INPUT_PULLUP);
pinMode(ALARAJA_PIN, INPUT_PULLUP);
pinMode(PAALLENAPPI_PIN, INPUT_PULLUP);
}
void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// 1 kelkka liikkuu ylöspäin
// -1 kelkka liikkuu alaspäin
// 0 kelkka pysähtyy
// jos anturi tai painonappi on aktiivinen, seuraava merkki kirjoitetaan ohjelmaan(sign)
if ((digitalRead(PAALLENAPPI_PIN) == 1) && (digitalRead(YLARAJA_PIN) == 1)) {
sign = -1;
}
if ((digitalRead(PAALLENAPPI_PIN) == 1) && (digitalRead(YLARAJA_PIN) == 0)) {
sign = 1;
}
if ((sign == 1) && (digitalRead(YLARAJA_PIN) == 1)) {
sign = 0;
}
if (((digitalRead(ALARAJA_PIN) == 1)) && (sign != 1)) {
sign = 1;
delay(4000);
}
if ((digitalRead(HIDASTUS_PIN) == 1) && (sign == -1)) {
hidastus = true;
}
if ((digitalRead(HIDASTUS_PIN) == 1) && (sign == 1)) {
hidastus = false;
}
if (hidastus) {
// potikkaa(nopeutta luetaan silloin tällöin (koska se ottaa ohjelmassa pitkän ajan
// eikä sitä haluta tehdä koko ajan pääsilmukassa)
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
}
// skaalataan potikan arvo minimistä maksiminopeuteen
current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
}
else {
current_speed = sign * (((1) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
}
// päivitetään askelmoottorin nopeus uuteen arvoon
stepper1.setSpeed(current_speed);
// tämä ajaa askelmoottoria vakionopeudella
stepper1.runSpeed();
}
< CODE/ >
