Arduino用于圣诞灯控制 - 反转代码

时间:2016-12-10 00:26:09

标签: arduino arduino-uno

我有一个arduino uno连接到8通道继电器板。我想将它与Vixen 3一起使用。当我上传代码时,所有继电器都会打开。因此,当我在Vixen发送信号时,继电器关闭。我需要一种方法来反转它,所以当我从Vixen发送信号时它会打开继电器。编码方面并不是我的强项,所以请放轻松我。

用爱xx

int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];

void setup()
{
  Serial.begin(9600);
  pinMode(C1, OUTPUT);
  pinMode(C2, OUTPUT);
  pinMode(C3, OUTPUT);
  pinMode(C4, OUTPUT);
  pinMode(C5, OUTPUT);
  pinMode(C6, OUTPUT);
  pinMode(C7, OUTPUT);
  pinMode(C8, OUTPUT);
}

void loop()
{
  if (Serial.available() >= 8) {
    for (int i=0; i<=8; i++)
    {
      incomingByte[i] = Serial.read();
    }
    analogWrite(C1, incomingByte[0]);
    analogWrite(C2, incomingByte[1]);
    analogWrite(C3, incomingByte[2]);
    analogWrite(C4, incomingByte[3]);
    analogWrite(C5, incomingByte[4]);
    analogWrite(C6, incomingByte[5]);
    analogWrite(C7, incomingByte[6]);
    analogWrite(C8, incomingByte[7]);
  }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码:

    int C1 = 2;
    int C2 = 3;
    int C3 = 4;
    int C4 = 5;
    int C5 = 6;
    int C6 = 7;
    int C7 = 8;
    int C8 = 9;
    int i = 0;
    int incomingByte[8];

    void setup()
    {
      Serial.begin(9600);
      pinMode(C1, OUTPUT);
      pinMode(C2, OUTPUT);
      pinMode(C3, OUTPUT);
      pinMode(C4, OUTPUT);
      pinMode(C5, OUTPUT);
      pinMode(C6, OUTPUT);
      pinMode(C7, OUTPUT);
      pinMode(C8, OUTPUT);
      digitalWrite(C1, LOW);
      digitalWrite(C2, LOW);
      digitalWrite(C3, LOW);
      digitalWrite(C4, LOW);
      digitalWrite(C5, LOW);
      digitalWrite(C6, LOW);
      digitalWrite(C7, LOW);
      digitalWrite(C8, LOW);
    }

    void loop()
    {
      if (Serial.available() >= 8) {
        for (int i = 0; i <= 8; i++)
        {
          incomingByte[i] = Serial.read();
        }
        analogWrite(C1, incomingByte[0]);
        analogWrite(C2, incomingByte[1]);
        analogWrite(C3, incomingByte[2]);
        analogWrite(C4, incomingByte[3]);
        analogWrite(C5, incomingByte[4]);
        analogWrite(C6, incomingByte[5]);
        analogWrite(C7, incomingByte[6]);
        analogWrite(C8, incomingByte[7]);
      }
    }