Arduino PIN 的行为不一样 [英] Arduino PINs not behaving equally

查看:53
本文介绍了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天全站免登陆