Arduino:如果/ Else问题

时间:2015-07-02 13:25:47

标签: arduino arduino-uno arduino-ide

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

我从arduino的串行输出中知道我正在从网页接收通信。如果我将布尔值翻转为关闭,那么我可以将" LED置为高电平"输出,但我不再能得到" led低#34;

我想在这里找到适合的代码。

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);
  } 

}

1 个答案:

答案 0 :(得分:4)

该示例要求PubNub发布为“lightRight:1”而不是{“lightRight”:“1”}。