1

我有一个步进电机的基本代码,如下所示,但由于某种原因,每当我尝试为我的 2 个 LCD 添加代码时,它都会搞砸一切。电机的输出只是不停地反复点火,没有延迟,液晶显示器甚至不打印任何东西。我做错了什么?

简单的步进代码(这是工作代码)

#define IN1  52
#define IN2  50
#define IN3  48
#define IN4  46
int Steps = 4096; //4096 or 768
int cstep = 0;

void setup()
{
    Serial.begin(9600);
    pinMode(IN1, OUTPUT); 
    pinMode(IN2, OUTPUT); 
    pinMode(IN3, OUTPUT); 
    pinMode(IN4, OUTPUT); 
}

void loop()
{
    for(int x=0; x<Steps; x++)
    {
        step1();
        delayMicroseconds(2500);
    }
    Serial.println("Boom!!");
    delay(1000);
}

void step1()
{
    //stepp
    switch(cstep)
    {
        case 0:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, HIGH);
             break; 

        case 1:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, HIGH);
             digitalWrite(IN4, HIGH);
             break; 

        case 2:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, HIGH);
             digitalWrite(IN4, LOW);
             break; 

        case 3:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, HIGH);
             digitalWrite(IN3, HIGH);
             digitalWrite(IN4, LOW);
             break; 

        case 4:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, HIGH);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 

        case 5:
             digitalWrite(IN1, HIGH); 
             digitalWrite(IN2, HIGH);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 

        case 6:
             digitalWrite(IN1, HIGH); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 

        case 7:
             digitalWrite(IN1, HIGH); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, HIGH);
             break; 

        default:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 
    }
    cstep=cstep+1;
    if (cstep==8)
    {
       cstep=0;
    }
}

带 LCD 代码的步进器(破碎的)

#include <LiquidCrystal.h>

#define IN1  52
#define IN2  50
#define IN3  48
#define IN4  46
int Steps = 4096; //4096 or 768
int cstep = 0;

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
LiquidCrystal lcd2(14, 9, 10, 11, 12, 13);

void setup(){
  lcd.begin(16,2);
  lcd2.begin(16,2);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
  Serial.begin(9600);
}

void loop()
{
    for (int x=0;x< Steps; x++)
    {
        step1();
        //delay(1);
        delayMicroseconds(2500);
    }
    Serial.println("Boom!!");
    delay(1000);
}

    void step1()
    {
        switch(cstep)
        {
            case 0:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 1");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print("."); 
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, HIGH);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 1:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 2");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print("."); 
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN4, HIGH);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 2:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 3");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print(".");
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 3:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 4");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print("."); 
                 digitalWrite(IN2, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN3, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 4:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 5");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print(".") ;
                 digitalWrite(IN2, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 5:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 6");
                 lcd2.print("Working");
                 digitalWrite(IN1, HIGH);
                 lcd2.print(".") ;
                 digitalWrite(IN2, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 6:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 7");
                 lcd2.print("Working");
                 digitalWrite(IN1, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 7:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 8");
                 lcd2.print("Working");
                 digitalWrite(IN1, HIGH);
                 lcd2.print("."); 
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, HIGH);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

           default:
                 digitalWrite(IN1, LOW); 
                 digitalWrite(IN2, LOW);
                 digitalWrite(IN3, LOW);
                 digitalWrite(IN4, LOW);
                 break; 
       }
       cstep = cstep+1;
       if (cstep==8)
       {
           cstep=0;
       }
   }

   void lcdClear(){
       lcd.setCursor(0,0);
       lcd.clear();
   }

   void lcd2Clear(){
       lcd2.setCursor(0,0);
       lcd2.clear();
   }

不知道这是否有帮助,但我的主板是 Mega 2560-R3

4

1 回答 1

1

PIN 1 是通讯引脚,不是数字。是TX0。我认为你不应该使用它。第二个显示器的 PIN 14 是 TX1。它应该又是数字的。尝试更改它们:)

于 2019-12-10T07:49:13.977 回答