Arduino:If/Else 个问题

Arduino: If/Else issues

情况:我有一个网页,上面有两个按钮。一个发送到 PubNub 频道一个 JSON 字符串 {"lightRight":"1"} 一个发送 {"lightRight":"0"},不管我在 Arduino Sketch I 中做了什么无法进入虚空光功能的 "on" 部分。

我从 arduino 的串行输出知道我正在从网页接收通信。如果我将我的布尔值翻转为关闭,那么我可以获得 "LED HIGH" 输出,但我不再可以获得 "led low"。

可以在此处找到我尝试改编的代码。

https://gist.github.com/ianjennings/ada8fb1a91a486a0c73e

很可能我从示例中破解了很多代码,这就是我将其包括在内的原因,但是 运行 我似乎无法使用股票代码。

我不是开发人员,所以如果我不称其为正确的术语,我感到非常抱歉。我会从指正中学习。

#include <PubNub.h>
#include <SPI.h>
#include <EthernetV2_0.h>
#include <string.h>
#include <Servo.h>

byte mac[] = { MAC was here };
byte gateway[] = { Gate was here };
byte subnet[] = { Sub was here };

IPAddress ip(IP was here);

char pubkey[] = "Key was here";
char subkey[] = "Key was here";
char channel[] = "Channel was here";

int lightRight = 5;
int lightRoom = 6;
int lightGarage = 7;

int i;

EthernetClient *client;

#define W5200_CS  3
#define SDCARD_CS 4

void setup()
{

  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);

  Serial.begin(9600);
  Serial.println("Serial set up");

  while (!Ethernet.begin(mac)) {
    Serial.println("Ethernet setup error");
                blink(1000, 999999);
    delay(1000);
  }
  Serial.println("Ethernet set up");

  PubNub.begin(pubkey, subkey);
  Serial.println("PubNub set up");

  pinMode(lightRight, OUTPUT);
  pinMode(lightRoom, OUTPUT);
  pinMode(lightGarage, OUTPUT);

//  blink(100, 5);
  reset();

}

//void flash(int ledPin)
//{
//  for (int i = 0; i < 3; i++) {
//      Serial.println("flash");
//    digitalWrite(ledPin, HIGH);
//    delay(100);
//    digitalWrite(ledPin, LOW);
//    delay(100);
//  }
//}

void loop()
{

  Ethernet.maintain();

    PubSubClient *client;

  Serial.println("waiting for a message (subscribe)");
  client = PubNub.subscribe(channel);

  if (!client) {
    Serial.println("subscription error");
    return;
  }

  String messages[10];

  boolean inside_command = false; 
  int num_commands = 0;

  String message = "";
  char c;

  while (client->wait_for_data()) {

    c = client->read();

    if(inside_command && c != '"') {
      messages[num_commands] += c;
    }

    if(c == '"') {

      if(inside_command) {         

        num_commands = num_commands + 1;
        inside_command = false;

      } else {     
        inside_command = true;
      }

    }

    message += c;

  }
  client->stop();

  for (i = 0; i < num_commands; i++){

    int colonIndex = messages[i].indexOf(':');

    String subject = messages[i].substring(0, colonIndex);
    String valueString = messages[i].substring(colonIndex + 1, messages[i].length());
    boolean value = false;

    if(valueString == "1") {
      value = true;
    }

    if(subject == "lightRight") {
      light(lightRight, value);
    }

    if(subject == "lightRoom") {
      light(lightRoom, value);
    }

    if(subject == "lightGarage") {
      light(lightGarage, value);
    }

    if(subject == "blink") {
      blink(100, valueString.toInt());
    }

    Serial.println(subject);
    Serial.println(value);

  }

  Serial.print(message);

  Serial.println();

  delay(2000);

}

void light(int ledPin, boolean on) {
  if(on) {
    digitalWrite(ledPin, HIGH);
      Serial.println("LED HIGH");
  } else {
    digitalWrite(ledPin, LOW);
      Serial.println("led low");
  }

}

void reset() {
        Serial.println("Void reset");
  light(lightRight, false);
  light(lightRoom, false);
  light(lightGarage, false);
}

void on() {
        Serial.println("Void on");
  light(lightRight, true);
  light(lightRoom, true);
  light(lightGarage, true);
}

void off() {
        Serial.println("Void off");
  light(lightRight, false);
  light(lightRoom, false);
  light(lightGarage, false);
}

void blink(int delayn, int count) {

  for (int j=0; j <= count; j++){
    on();
    delay(delayn);
    off();
    delay(delayn);
  } 

}

该示例期望 PubNub 发布为 "lightRight:1" 而不是 {"lightRight": "1"}。