Hallo,
ich baue mir grad einen Controller, mit dem ich 2 Schrittmotoren ansteuern kann. Beide Schrittmotoren sind über Pololu-Controller mit dem Arduino verbunden. Zusätzlich habe ich ein 16x2-LCD-Display und einen Joystick und Taster zur Steuerung.
Als Library für die Schrittmotoren nutze ich den AccelStepper.
Mein Problem ist nun, dass die Schrittmotoren extrem langsam werden, sobald zusätzliche Code dazugefügt wird. Ohne das analogRead und das Switch/Case laufen die Motoren normal/schnell. Mit dem analogRead langsamer und sobald das Switch/Case dazu kommt laufen die in Zeitlupe.
Wie müsste ich das Programm aufbauen, damit die Schrittmotoren nicht vom restlichen Code beeinflusst werden?
Zum Einsatz kommt ein Arduino MEGA 2560.
CODE IST NOCH NICHT VOLLSTÄNDIG:
#include <LiquidCrystal.h>
#include <AH_Pololu.h>
#include <AccelStepper.h>
LiquidCrystal lcd(29, 27, 22, 24, 26, 28);
//-------------Defintion for AH_Pololu-----------------
AH_Pololu stepperY(200,30,31,51,50,49,48); // Kippen
AH_Pololu stepperX(200,34,35,45,44,43,42); // Drehen
//-------------Defintion for AccelStepper-----------------
AccelStepper AccelStepperY(1, 31, 30); // Kippen
AccelStepper AccelStepperX(1, 35, 34); // Drehen
//-------------Defintion Analoge Eingänge-----------------
const int button0 = A0;
const int joyX = A1; // Nullwert = 525
const int joyY = A2; // Nullwert = 537
//-------------Werte Analoge Eingänge-----------------
int button0_value = 0;
int joyX_value = 0;
int joyY_value = 0;
int progStep = 0; // 0 = Auswahl Modus -- 1 = Auswahl Motion -- 2 = Programmablauf
int modus = 0; // 0=VIDEO -- 1=TIMELAPSE -- 2=PANO
int motionModus = 0; // 0=SLIDER -- 1=PANOHEAD -- 2=BOTH
int caseVIDEO = 0;
int caseTIMELAPSE = 0;
int casePANO = 0;
int hzX;
int schrittX =0;
int hzY;
int schrittY =0;
void setup()
{
pinMode (button0, INPUT);
pinMode (joyX, INPUT);
pinMode (joyY, INPUT);
//Serial.begin(115200);
lcd.begin(16, 2); // start the library
AccelStepperX.setMaxSpeed(2000);
AccelStepperX.setAcceleration(1000);
AccelStepperX.moveTo(-8000);
}
void loop()
{
AccelStepperX.run();
//-------------Einlesen der Eingaben-----------------
joyX_value = analogRead(joyX);
joyY_value = analogRead(joyY);
button0_value = analogRead(button0);
switch(progStep){
//------Auswahl Modus-------
case 0:
AuswahlModus();
break;
//------Auswahl Motion-------
case 1:
AuswahlMotion();
break;
//------Auswahl Programmablauf-------
case 2:
switch (modus){
case 0:
video();
break;
case 1:
timelapse();
break;
case 2:
pano();
break;
}
break;
} // switch (progStep)
} // void loop()
//---------------------------------------------------------------
//-----------------------Auswahl Modus---------------------------
//---------------------------------------------------------------
void AuswahlModus(){
//-------Auswahl Modus - Anzeige---------
lcd.setCursor(1,0);
lcd.print("VIDEO");
lcd.setCursor(1,1);
lcd.print("TIMELAPSE");
lcd.setCursor(12,0);
lcd.print("PANO");
//------Auswahl Modus - Control-Display-----------
if (modus==0){
lcd.setCursor(0,0);
lcd.print(">");
}
else if(modus==1){
lcd.setCursor(0,1);
lcd.print(">");
}
else{
lcd.setCursor(11,0);
lcd.print(">");
}
//-------Auswahl Modus - Control-Funktion---------------------------------
if(joyX_value>1000){
delay(400);
if(modus==0){
modus=1;
}
else if(modus==1){
modus=2;
}
else{
modus=0;
}
lcd.clear();
}
if(button0_value>1000){
delay(400);
progStep+=1;
lcd.clear();
}
}
//---------------------------------------------------------------
//----------------------Auswahl Motion---------------------------
//---------------------------------------------------------------
void AuswahlMotion(){
//-------Auswahl Motion - Anzeige---------------------------
lcd.setCursor(1,0);
lcd.print("PAN/TILT");
lcd.setCursor(1,1);
lcd.print("SLIDER");
lcd.setCursor(12,0);
lcd.print("BOTH");
//------Auswahl Motion - Control-Display--------------------------------
if (motionModus==0){
lcd.setCursor(0,0);
lcd.print(">");
}
else if(motionModus==1){
lcd.setCursor(0,1);
lcd.print(">");
}
else{
lcd.setCursor(11,0);
lcd.print(">");
}
//------Auswahl Motion - Control-Funktion--------------
if(joyX_value==1023){
delay(400);
if(motionModus==0){
motionModus=1;
}
else if(motionModus==1){
motionModus=2;
}
else{
motionModus=0;
}
lcd.clear();
}
if(button0_value>1000){
delay(400);
progStep+=1;
lcd.clear();
}
}
//---------------------------------------------------------------
//---------------------------VIDEO-------------------------------
//---------------------------------------------------------------
void video(){
//-----------Caseweiterschaltung durch Button----------
if(button0_value>1000){
delay(400);
caseVIDEO+=1;
lcd.clear();
}
int HzX =4000;
switch (caseVIDEO){
case 0:
lcd.setCursor(6,0);
lcd.print("MOVE");
lcd.setCursor(0,1);
lcd.print("TO STARTPOSITION");
stepperX.setSpeedHz(1000);
stepperX.move(-10);
delay(100);
// stepperX.setSpeedHz(HzX);
// stepperX.move(16);
/* //-----------X----------------------
if (joyX_value >550 && joyX_value<700)
{
schrittX-=8;
hzX =300;
}
if (joyX_value >700 && joyX_value <1020)
{
schrittX-=8;
hzX =1000;
}
if (joyX_value >1020)
{
schrittX-=8;
hzX =5000;
}
if (joyX_value <520 && joyX_value>300)
{
schrittX+=8;
hzX =300;
}
if (joyX_value <300 && joyX_value >20)
{
schrittX+=8;
hzX =1000;
}
if (joyX_value <20)
{
schrittX+=8;
hzX =5000;
}
//-----------------Y----------------------
if (joyY_value >540 && joyY_value<700)
{
schrittY+=8;
hzY =300;
}
if (joyY_value >700 && joyY_value <1020)
{
schrittY+=8;
hzY =1000;
}
if (joyY_value >=1020)
{
schrittY+=8;
hzY =5000;
}
if (joyY_value <510 && joyY_value>300)
{
schrittY-=8;
hzY =300;
}
if (joyY_value <300 && joyY_value >5)
{
schrittY-=8;
hzY =1000;
}
if (joyY_value <=5)
{
schrittY-=8;
hzY =5000;
}
stepperX.setSpeedHz(hzX);
stepperX.move(schrittX);
stepperY.setSpeedHz(hzY);
stepperY.move(schrittY);*/
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
//---------------------------------------------------------------
//-------------------------TIMELAPSE-----------------------------
//---------------------------------------------------------------
void timelapse(){
lcd.setCursor(0,1);
lcd.print("TIMELAPSE");
}
//---------------------------------------------------------------
//----------------------------PANO-------------------------------
//---------------------------------------------------------------
void pano(){
lcd.setCursor(0,1);
lcd.print("PANO");
}