Шаговый мотор работает с регулировкой скорости потенциометром скетч
Basic example code for controlling a stepper without library
by Dejan, https://howtomechatronics.com
*/
// defines pins
#define stepPin 2
#define dirPin 5
int customDelay, customDelayMapped;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
speedControl();
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Custom function for reading the potentiometer and mapping its value from 300 to 3000, suitable for the custom delay value in microseconds
void speedControl() {
customDelay = analogRead(A0); // Read the potentiometer value
customDelayMapped = map(customDelay, 0, 1023, 300, 3000); // Convert the analog input from 0 to 1024, to 300 to 3000
}
Я попытался добавить код для измерения напряжения на том же входе А0 и индикации его
на OLED.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MsTimer2.h>
#define OLED_RESET 4
#define MEASURE_PERIOD 1000 // время периода измерения
int timeCount; // счетчик времени
uint32_t sum; // переменные для суммирования кодов АЦП
uint32_t value; // сумма кодов АЦП (среднее значение * 500)
boolean flagReady; // признак готовности данных измерения
uint16_t buttonState = 0;
Adafruit_SSD1306 display(OLED_RESET);
uint32_t out;
void timerInterupt() {
timeCount++; // +1 счетчик выборок усреднения
sum += analogRead(A0); // суммирование кодов АЦП
// проверка числа выборок усреднения
if (timeCount >= MEASURE_PERIOD) {
timeCount = 0;
value = sum; // перегрузка среднего значения
sum = 0;
flagReady = true; // признак результат измерений готов
}
}
void setup() {
Serial.begin(9600);
MsTimer2::set(1, timerInterupt); // прерывания по таймеру, период 1 мс
MsTimer2::start(); // разрешение прерывания
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
display.setCursor(75,10);
display.setTextSize(2);
display.clearDisplay();
display.setTextColor(WHITE);
out= value/1000;
display.println(out);
display.setCursor(10,10);
display.setTextSize(2);
display.println("RPM: ");
display.display();
}
Оба скетча работают по отдельности нормально,но когда я их объединил скорость мотора резко упала
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MsTimer2.h>
#include <AccelStepper.h>
AccelStepper stepper1(1, 2, 5);
#define OLED_RESET 4
#define MEASURE_PERIOD 1000 // время периода измерения
int timeCount; // счетчик времени
uint32_t sum; // переменные для суммирования кодов АЦП
uint32_t value; // сумма кодов АЦП (среднее значение * 500)
boolean flagReady; // признак готовности данных измерения
uint16_t buttonState = 0;
Adafruit_SSD1306 display(OLED_RESET);
uint32_t out;
void timerInterupt() {
timeCount++; // +1 счетчик выборок усреднения
sum += analogRead(A0); // суммирование кодов АЦП
// проверка числа выборок усреднения
if (timeCount >= MEASURE_PERIOD) {
timeCount = 0;
value = sum; // перегрузка среднего значения
sum = 0;
flagReady = true; // признак результат измерений готов
}
}
void setup() {
Serial.begin(9600);
stepper1.setMaxSpeed(1000);
MsTimer2::set(1, timerInterupt); // прерывания по таймеру, период 1 мс
MsTimer2::start(); // разрешение прерывания
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
stepper1.setSpeed(analogRead(A0));
// Step the motor with a constant speed previously set by setSpeed();
stepper1.runSpeed();
display.setCursor(75,10);
display.setTextSize(2);
display.clearDisplay();
display.setTextColor(WHITE);
out= value/1000;
display.println(out);
display.setCursor(10,10);
display.setTextSize(2);
display.println("RPM: ");
display.display();
}
мотор медленно вращается короткими шажками. Я понимаю что или нельзя использовать в
коде дважды вход А0,или прерывание для АЦП влияет на код шагового мотора. Но как разрешить эту проблему не знаю.
Надеюсь получить помощь.
Спасибо.
Юрий