mercredi 10 avril 2019

Why is Arduino not going through the if/then/elseif as expected when receiving bytes over serial?

I'm working on a project where the Arduino should do different things based on different characters (a, b, etc.) being received over Serial. I have gotten it to respond, but I hit a logic error. When I enter a, it does as requested and does the soft-reset procedure (grounding the reset pin through a resistor to reset itself remotely). However, when I do b (or any other of the 30-some other characters, none of which are identical), it does the a command. It should, based on my logic, not do a's code since incomingByte != 'a', but it does = b. Why is it running a and not anything else? Thanks for any help.

P.S. I am aware there are other methods of doing this, which are likely more 'proper,' but I would like to stick with this if/then/elseif logic if possible.

I have attached some of the code below, there's a lot more so I know there might be a missing } there somewhere at the end, but it's there in the code.

void loop() {
    char incomingByte;
    //wait for commands
    if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    bool alarmState = false;
    //variable for storing status of the alarm system for use during unsilence controls, to ensure operator cannot
    //unsilence a non-alarmed system and cause audible signals to activate prematurely

    //use incomingByte as the command byte in ain if/then/elseif
    if (incomingByte = "a") {
    //code to soft-reset
      //output reset confirmation
      tone(buzzer, 600, 500);
      Serial.println("System reset in progress...");
      delay(100);
      digitalWrite(powerLED, LOW);
      delay(500);
      digitalWrite(powerLED, HIGH);
      delay(500);
      digitalWrite(powerLED, LOW);
      delay(500);
      digitalWrite(powerLED, HIGH);
      delay(500);
      digitalWrite(powerLED, LOW);
      delay(500);
      digitalWrite(powerLED, HIGH);
      delay(500);
      digitalWrite(powerLED, LOW);
      delay(250);
      digitalWrite(resetPin, LOW);
      digitalWrite(troubleLED, HIGH);
      delay(100);
      digitalWrite(resetPin, HIGH);  //this is needed to complete the reset

    }
    else if (incomingByte = "b") {
      //code to silence audible signals
      if (alarmState = true) {
      digitalWrite(NAC1, LOW);
      digitalWrite(silenceLED, HIGH);
      digitalWrite(silence1, HIGH);
      Serial.println("Audible signals silenced");
      }
      else if (alarmState = false) {
        Serial.println("The system is not in alarm and the NAC state could not be modified. If you need to manually override NAC states without system alarm, use the System menu.");
      }
    }
    else if (incomingByte = "c") {
      //code to unsilence audible signals
      if (alarmState = true) {
      digitalWrite(NAC1, HIGH);
      digitalWrite(silenceLED, LOW);
      digitalWrite(silence1, LOW);
      Serial.println("Audible signals unsilenced");
      }
      else if (alarmState = false) {
        Serial.println("The system is not in alarm and the NAC state could not be modified. If you need to manually override NAC states without system alarm, use the System menu.");
      }

Aucun commentaire:

Enregistrer un commentaire