Управление шаговым двигвтелем из меню
- Войдите на сайт для отправки комментариев
Вс, 12/06/2016 - 09:06
Здравствуйте!
Есть ардуино мега, ЛСД на шилде с 6-ю кнопками. подключен драйвер шагового двигателя и сам шаговый двигатель.
когда я запускаю скетч шагового двигателя. все работает о.к. но проблема что я немогу склеить скетч работы шагового с скетчем меню дисплея...
Вот склееный скетч:
#include <LiquidCrystal.h>
#include <Stepper.h>
//Подключение шагового двигателя
const int stepsPerRevolution = 12800; // кол-во импульсов на оборот
Stepper myStepper(stepsPerRevolution, 19, 21); // Подключение ШД
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
int motor=0;
void setup() {
//Set the characters and column numbers.
lcd.begin(16, 2);
//Print default title.
clearPrintTitle();
//Параметры шагового двигателя
// pinMode(pul, OUTPUT);
// pinMode(dir, OUTPUT);
}
void loop()
{
//Call the main menu.
mainMenu();
}
void mainMenu() {
//State = 0 every loop cycle.
int state = 0;
//Refresh the button pressed.
int x = analogRead (0);
//Set the Row 0, Col 0 position.
lcd.setCursor(0,0);
//Check analog values from LCD Keypad Shield
if (x < 100) {
//Right
state = 4;
} else if (x < 200) {
//Up
state = 1;
} else if (x < 400){
//Down
state = 2;
} else if (x < 600){
//Left
state = 5;
} else if (x < 800){
//Select
state = 3;
}
//If we are out of bounds on th menu then reset it.
if (currentMenuItem < 0 || currentMenuItem > 13) {
currentMenuItem = 0;
}
//If we have changed Index, saves re-draws.
if (state != lastState) {
if (state == 1) {
//If Up
currentMenuItem = currentMenuItem - 1;
displayMenu(currentMenuItem);
} else if (state == 2) {
//If Down
currentMenuItem = currentMenuItem + 1;
displayMenu(currentMenuItem);
} else if (state == 3) {
//If Selected
motor=0;
//selectMenu(currentMenuItem);
} else if (state == 4) {
//If RIGHT
rightMenu(currentMenuItem);
} else if (state == 5) {
//If LEFT
leftMenu(currentMenuItem);
}
//Save the last State to compare.
lastState = state;
}
//Small delay
delay(5);
}
//Display Menu Option based on Index.
void displayMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("0.01");
break;
case 2:
clearPrintTitle();
lcd.print ("0.015");
break;
case 3:
clearPrintTitle();
lcd.print ("0.02");
break;
case 4:
clearPrintTitle();
lcd.print ("0.03");
break;
case 5:
clearPrintTitle();
lcd.print ("0.04");
break;
case 6:
clearPrintTitle();
lcd.print ("0.06");
break;
case 7:
clearPrintTitle();
lcd.print ("0.08");
break;
case 8:
clearPrintTitle();
lcd.print ("0.1");
break;
case 9:
clearPrintTitle();
lcd.print ("0.13");
break;
case 10:
clearPrintTitle();
lcd.print ("0.15");
break;
case 11:
clearPrintTitle();
lcd.print ("0.18");
break;
case 12:
clearPrintTitle();
lcd.print ("0.20");
break;
case 13:
clearPrintTitle();
lcd.print ("0.22");
break;
}
}
//Print a basic header on Row 1.
void clearPrintTitle() {
lcd.setCursor(0,0);
lcd.print("Podacha mm/ob");
lcd.setCursor(0,1);
}
//Show the rightMenu on Screen.
void rightMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("0.01 >>");
// myStepper.setSpeed(400);
// step 1 of a revolution:
// myStepper.step(stepsPerRevolution );
motor=1;
void motormenu();
lcd.setCursor(10,1);
lcd.print (motor);
break;
//Call the function that belongs to Option 1
case 2:
clearPrintTitle();
lcd.print ("0.015 >>");
//Call the function that belongs to Option 2
break;
case 3:
clearPrintTitle();
lcd.print ("0.02 >>");
//Call the function that belongs to Option 3
break;
case 4:
clearPrintTitle();
lcd.print ("0.03 >>");
//Call the function that belongs to Option 4
break;
case 5:
clearPrintTitle();
lcd.print ("0.04 >>");
//Call the function that belongs to Option 5
break;
case 6:
clearPrintTitle();
lcd.print ("0.06 >>");
//Call the function that belongs to Option 6
break;
case 7:
clearPrintTitle();
lcd.print ("0.08 >>");
//Call the function that belongs to Option 7
break;
case 8:
clearPrintTitle();
lcd.print ("0.1 >>");
//Call the function that belongs to Option 8
break;
case 9:
clearPrintTitle();
lcd.print ("0.13 >>");
//Call the function that belongs to Option 9
break;
case 10:
clearPrintTitle();
lcd.print ("0.15 >>");
//Call the function that belongs to Option 10
break;
case 11:
clearPrintTitle();
lcd.print ("0.18 >>");
//Call the function that belongs to Option 11
break;
case 12:
clearPrintTitle();
lcd.print ("0.2 >>");
//Call the function that belongs to Option 12
break;
case 13:
clearPrintTitle();
lcd.print ("0.22 >>");
//Call the function that belongs to Option 13
break;
}
}
//Show the leftMenu on Screen.
void leftMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("0.01 <<");
motor=21;
void motormenu();
lcd.setCursor(10,1);
lcd.print (motor);
// myStepper.setSpeed(50);
// step 1 of a revolution:
//myStepper.step(-stepsPerRevolution );
break;
case 2:
clearPrintTitle();
lcd.print ("0.015 <<");
//Call the function that belongs to Option 2
break;
case 3:
clearPrintTitle();
lcd.print ("0.02 <<");
//Call the function that belongs to Option 3
break;
case 4:
clearPrintTitle();
lcd.print ("0.03 <<");
//Call the function that belongs to Option 4
break;
case 5:
clearPrintTitle();
lcd.print ("0.04 <<");
//Call the function that belongs to Option 5
break;
case 6:
clearPrintTitle();
lcd.print ("0.06 <<");
//Call the function that belongs to Option 6
break;
case 7:
clearPrintTitle();
lcd.print ("0.08 <<");
//Call the function that belongs to Option 7
break;
case 8:
clearPrintTitle();
lcd.print ("0.1 <<");
//Call the function that belongs to Option 8
break;
case 9:
clearPrintTitle();
lcd.print ("0.13 <<");
//Call the function that belongs to Option 9
break;
case 10:
clearPrintTitle();
lcd.print ("0.15 <<");
//Call the function that belongs to Option 10
break;
case 11:
clearPrintTitle();
lcd.print ("0.18 <<");
//Call the function that belongs to Option 11
break;
case 12:
clearPrintTitle();
lcd.print ("0.2 <<");
//Call the function that belongs to Option 12
break;
case 13:
clearPrintTitle();
lcd.print ("0.22 <<");
//Call the function that belongs to Option 13
break;
}
}
//********************Режим работы шагового двигателя
void motormenu()
{
if (motor==1) {
myStepper.setSpeed(400);
// step 1 of a revolution:
myStepper.step(stepsPerRevolution );
}
else if (motor==21) {
myStepper.setSpeed(200);
// step 1 of a revolution:
myStepper.step(-stepsPerRevolution );
}
}
При нажатии в leftMenu кнопки влево переменная motor меняет значение. и по идее дожно запускаться motormenu и крутить двигатель с определенными оборотами, а он не крутится.
Подскажите пожалуйста что я сделал не так?
В 176 и 253 строке неправильно написан вызов motormenu, нужно без void.
двигатель запустился,делает один оборот и останавливается. Но как сделать чтоб он крутился постоянно, до изменения переменной motor?
Вродибы цикличность комманд управления двигателем и так должна быть? Если нет то как ее сделать?