Serial.Event()只运行一次(使用单个char输入)

时间:2016-06-21 12:15:45

标签: serialization arduino

在我的Arduino Mega 2560上,我试图运行一个电机,转动一个20瓶容器(接受输入1-20),同时通过一个单独的冷却器的PID调节温度。我对这个技术领域一般都很陌生,所以请耐心等待。我还为编码器设置了中断,以跟踪样品瓶的位置。

void serialEvent()void loop()是最重要的部分,但我决定将其余的代码放在那里以防万一你需要看到它。

    #include <PID_v1.h>
    #include <SPI.h>
    #include <TMC26XStepper.h>

    #define COOL_INPUT 0
    #define PIN_OUTPUT 9

    TMC26XStepper tmc26XStepper = TMC26XStepper(200,5,7,6,500);

    int step = 6;
    int value;
    int i;
    char junk = ' ';
    volatile long enc_count = 0;
    const byte interruptPinA = 2;
    const byte interruptPinB = 3;

    //Define Variables we'll be connecting to
    int outMax = 255;
    int outMin = -145;

    double Setpoint, Input, Output;
    double heatInput, heatOutput, originalInput;

    //Specify the links and initial tuning parameters
    // AGGRESSIVE VALUES (to get to 4 deg C)
    double aggKp=8.0, aggKi=3.0, aggKd=0.15;

    // CONSERVATIVE VALUES (to hover around 4 deg C)
    double consKp=2.5, consKi = 0.0, consKd = 1.0;

    PID myPID(&Input, &Output, &Setpoint, aggKp, aggKi, aggKd, REVERSE);

    void setup()
    {
       pinMode(step, OUTPUT);
       pinMode(interruptPinA, INPUT_PULLUP);
       pinMode(interruptPinB, INPUT_PULLUP);
       attachInterrupt(digitalPinToInterrupt(interruptPinA), encoder_isr, CHANGE);
       attachInterrupt(digitalPinToInterrupt(interruptPinB), encoder_isr, CHANGE);
       //initialize the variables we're linked to
       Input = (5.0*analogRead(COOL_INPUT)*100.0) / 1024;
       Setpoint = 10.75;
       myPID.SetOutputLimits(outMin, outMax);

       //turn the PID on
       myPID.SetMode(AUTOMATIC);
       Serial.begin(115200);

       tmc26XStepper.setSpreadCycleChopper(2,24,8,6,0);
       tmc26XStepper.setMicrosteps(32);
       tmc26XStepper.setStallGuardThreshold(4,0);
       Serial.println("...started...");
       tmc26XStepper.start();

       Serial.flush();
       Serial.println("Enter vial numbers 1-20");
    }

    void loop() {
        Input = (5.0*analogRead(COOL_INPUT)*100.0) / 1024;
        // A BUNCH OF CODE FOR TEMP REGULATION
        Serial.println(Input);
        delay(150);
    }

    void serialEvent() {
       while (Serial.available() == 0) {}
       i = Serial.parseInt();
       Serial.print("position: ");
       Serial.print(i);
       Serial.print(" ");
       while (Serial.available() > 0) {
           junk = Serial.read();
       }
       if (i == 1) {
           value = 0;
       } else {
           int num = i - 1;
           value = num * 72;
       }
       while (enc_count != value) {
           digitalWrite(6, HIGH);
           delayMicroseconds(100);
           digitalWrite(6, LOW);
           delayMicroseconds(100);
           if (enc_count == 1440) {
               enc_count = 0;
           }
       }
       Serial.println(enc_count);
    }


    // INFO FOR ENCODER
    void encoder_isr() {
        static int8_t lookup_table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
        static uint8_t enc_val = 0;

        enc_val = enc_val << 2;
        enc_val = enc_val | ((PIND & 0b1100) >> 2);

        enc_count = enc_count + lookup_table[enc_val & 0b1111];
     }

所以,最初我分别测试了两个过程(样品瓶位置+编码器,然后温度调节),一切都完全按照预期进行。现在,我将代码融合在一起,并将样品瓶位置条目存储在serialEvent()方法中,以保持温度读数连续,并且每当我决定提供输入时,样品瓶位置条目都可用。但是,当我输入一个值时,程序会一起停止。我能够看到我输入的号码(位置:5),但Serial.println(enc_count)永远不会打印出来。最重要的是,温度读数停止显示读数。

有什么想法?需要更多信息?

0 个答案:

没有答案