Изменение скорости шаговаго двигателя
- Войдите на сайт для отправки комментариев
Чт, 21/02/2013 - 16:45
Пытаюсь кнопкой изменять скорость шагового двигателя.
Проблема в том, что скорость при нажатии кнопок уменьшается. Но когда идёт следующий цыкл скорость остаётся самой низкой и уже не увеличивается. Не могу понять, это или библиотека Accelstepper так работает или в программе ошибка.
#include <Bounce.h>
#include <EEPROM.h>
#include <AccelStepper.h>
AccelStepper stepper(1, 9, 10);
int button1Pin = 17;
int button2Pin = 18;
int button3Pin = 19;
int button7Pin = 11;
Bounce bouncer1 = Bounce( button1Pin,5 );
Bounce bouncer2 = Bounce( button2Pin,5 );
Bounce bouncer3 = Bounce( button3Pin,5 );
int value1;
int value2;
int value3;
int inputMode = 1;
int potpin = 0;
int valPot;
int val1;
int SPEED;
int regim7=1;
int flag7=0;
void setup()
{
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button7Pin, INPUT);
}
void loop()
{
if(inputMode) {
valPot = analogRead(potpin);
valPot = map(valPot, 0, 1024, 0, 1224);
stepper.setMaxSpeed(3000);
stepper.moveTo(valPot);
stepper.setSpeed(2500);
stepper.runSpeedToPosition();
}
if(digitalRead(button7Pin)==HIGH&&flag7==0)
{
regim7++;
flag7=1;
if(regim7>3)
{
regim7=1;
}
}
if(digitalRead(button7Pin)==LOW&&flag7==1)
{
flag7=0;
}
if(regim7== 1) {
SPEED = 600;
}
if(regim7== 2) {
SPEED = 200;
}
if(regim7==3) {
SPEED = 50;
}
bouncer1.update ( );
value1 = bouncer1.read();
if(value1 == HIGH) {
byte *x1 = (byte *)&valPot;
EEPROM.write(10, x1[0]);
EEPROM.write(11, x1[1]);
delay(15);
}
bouncer2.update ( );
value2 = bouncer2.read();
if(value2 == HIGH ) {
inputMode=0;
byte xx1[] = {
EEPROM.read(10), EEPROM.read(11) };
int *y1 = (int *)&xx1;
val1 = y1[0];
stepper.moveTo(val1);
stepper.setMaxSpeed(SPEED);
stepper.runSpeedToPosition();
while (stepper.distanceToGo() != 0)
{
stepper.run();
}
}
bouncer3.update ( );
value3 = bouncer3.read();
if(value3 == HIGH) {
inputMode=0;
byte xx1[] = {
EEPROM.read(10), EEPROM.read(11) };
int *y1 = (int *)&xx1;
val1 = y1[0];
stepper.moveTo(valPot);
stepper.setMaxSpeed(SPEED);
stepper.runSpeedToPosition();
while (stepper.distanceToGo() != 0)
{
stepper.run();
}
inputMode=1;
}
}