Arduino PIN行为不均 [英] Arduino PINs not behaving equally

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

问题描述

我已经在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;时,它会完全更改该状态,而无需执行该命令.类似地,某些引脚在响应,例如引脚3,而其他引脚没有响应(例如,引脚13).这与代码有关吗?

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

然后您将具有正确的操作的switch语句到pinNum.

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

我想看看str的值在"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行为不均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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