Arduino的PIN码不equaly行为 [英] Arduino PINs not behaving equaly

查看:239
本文介绍了Arduino的PIN码不equaly行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经烧了code对我的Arduino的:

I have burned this code on my arduino:

#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>

using namespace std;

#define BUFFSIZE        16
#define PIN0            0
#define PIN1            1
#define PIN2            2
#define PIN3            3
#define PIN4            4
#define PIN5            5
#define PIN6            6
#define PIN7            7
#define PIN8            8
#define PIN9            9
#define PIN10           10
#define PIN11           11
#define PIN12           12
#define PIN13           13

// For mac address please view the Ethernet shield.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD5};

IPAddress server(192, 168, 0, 61); // IP address of RAAS server

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 62);


// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 8 is selected for RAAS):
EthernetClient client;

String GetNextCommand()
{
    int count,i;
    int temp;
    String command;
        Serial.println("Waiting for next Command.");
    while((count = client.available()) < 1 );
    for(i = 0; i < count ; i++)
        command += (char) client.read();
        command.replace("\r","");
        command.replace("\n","");
        Serial.println(command);
        return command;
}

bool ConnectServer() {
    int resCount,i;
    String response;
    Serial.println("Trying to connect...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 8)) {
        Serial.println("connected");
        // Make a Handshake request:
        client.println("EHLO");
        response = GetNextCommand();
        if (response == "EHLO" ) {
            Serial.println("Server Response OKAY");
            return true;
        }
    } else {
        // kf you didn't get a connection to the server:
        Serial.println("connection failed");
    }
    return false;
}

void InitializeBoard() {
    pinMode(PIN1, OUTPUT);
    pinMode(PIN2, OUTPUT);
    pinMode(PIN3, OUTPUT);
    pinMode(PIN4, OUTPUT);
    pinMode(PIN5, OUTPUT);
    pinMode(PIN6, OUTPUT);
    pinMode(PIN7, OUTPUT);
    pinMode(PIN8, OUTPUT);
    pinMode(PIN9, OUTPUT);
    pinMode(PIN10, OUTPUT);
    pinMode(PIN11, OUTPUT);
    pinMode(PIN12, OUTPUT);
    pinMode(PIN13, OUTPUT);
    digitalWrite(PIN1, LOW);
    digitalWrite(PIN2, LOW);
    digitalWrite(PIN3, LOW);
    digitalWrite(PIN4, LOW);
    digitalWrite(PIN5, LOW);
    digitalWrite(PIN6, LOW);
    digitalWrite(PIN7, LOW);
    digitalWrite(PIN8, LOW);
    digitalWrite(PIN9, LOW);
    digitalWrite(PIN10, LOW);
    digitalWrite(PIN11, LOW);
    digitalWrite(PIN12, LOW);
    digitalWrite(PIN13, LOW);
}

void ParseCommand(String str) {

    int pinNum;
    int pinState = LOW;

    String switchNo,operation;
    int temp;
        temp=0;
    temp = str.indexOf(':',temp);
    switchNo = str.substring(0,temp);
    operation = str.substring(temp+1);//,str.length()-temp-2);
    Serial.println("Port: " + switchNo);
    Serial.println("Operation: " + operation);  
    if(operation == "OFF;")
        pinState = LOW;
    else if(operation == "ON;")
        pinState = HIGH;
    else Serial.println("Invalid Command from server!");

    pinNum = str.toInt();

    Serial.print("Setting ");
    Serial.print( pinNum);
    Serial.print(  " to ");
    Serial.println( pinState);

    switch (pinNum){
        case PIN1:
                digitalWrite(PIN1, pinState);
                break;
        case PIN2:
                digitalWrite(PIN2, pinState);
                break;
        case PIN3:
                digitalWrite(PIN3, pinState);
                break;
        case PIN4:
                digitalWrite(PIN4, pinState);
                break;
        case PIN5:
                digitalWrite(PIN5, pinState);
                break;
        case PIN6:
                digitalWrite(PIN6, pinState);
                break;
        case PIN7:
                digitalWrite(PIN7, pinState);
                break;
        case PIN8:
                digitalWrite(PIN8, pinState);
                break;
        case PIN9:
                digitalWrite(PIN9, pinState);
                break;
        case PIN10:
                digitalWrite(PIN10, pinState);
                break;
        case PIN11:
                digitalWrite(PIN11, pinState);
                break;
        case PIN12:
                digitalWrite(PIN12, pinState);
                break;
        case PIN13:
                digitalWrite(PIN13, pinState);
                break;
        default:
            Serial.println("Invalid Pin Address!");
    }
}

void setup() {

    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, ip);
    } else {
      Serial.println("Bound on given Mac");
    }
    // give the Ethernet shield a second to initialize:
    delay(1000);

}

void loop() {
    //Keep trying to connect to the server until the server response okay!
    while (!ConnectServer());

    //While Server is connected, keep listening for incoming commands from server.
    while (client.connected()) {
        String command;
        command = GetNextCommand();
        ParseCommand(command);
    }

    // if the server's disconnected, stop the client:
    if (!client.connected()) {
        Serial.println();
        Serial.println("Server Disconnected.");
        client.stop();
    }
}

现在我要对继电器所有的引脚从1-13连接。但销不正确行为。当我将命令发送 13:ON; 13:OFF;在我的以太网盾销不改变其状态每一种情况下。但是,当我发 03:ON; 03:OFF; 它究竟是改变状态w.r.t命令。同样一些引脚像响应引脚3和其他引脚都没有(第13,14一样)。这是一些事情与code?

Now I have to connect Relays on all the pins from 1-13. But pins are not behaving properly. When I send command 13:ON; or 13:OFF; pins on my Ethernet shield does not change its state in each case. But when I send 03:ON; or 03:OFF; it changes its state exactly w.r.t the command. Similarly some pins are responding like pin 3 and other pins are not (like pin13). Is this some thing to do with the code?

推荐答案

这个问题是以太网盾采用13引脚,12,11,如SPI。同时10和4,选择SD或以太网。

The issue is the Ethernet shield uses pins 13, 12, 11, as SPI . Also 10 and 4, to select SD or ethernet.

<击>
    pinNum = str.toInt();

pinNum = str.toInt();

和那么你有一个开关语句的正确操作马赫pinNum。

And then you have a switch statement to mach pinNum with the correct operation.

我希望看到海峡前值ParseCommand()函数的外观。你能告诉我什么版画在GetNextCommand()

I would like to see how the value of str looks before "ParseCommand()" function. Can you show me what prints in the "GetNextCommand()"

这篇关于Arduino的PIN码不equaly行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆