Arduino PINs not behaving equaly

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

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?
Also when I connect relay pins to ground and pin3 of my ethernet shield the pin3 it self goes to ground rather than triggering the relay!

How are you connecting the relays to the Arduino I/O pins? If you are connecting them directly then it is pretty unlikely that the relay will operate, and also pretty likely that the attempt will burn out the Arduino output circuit for that pin.

The Ethernet Shield uses pins 4, 10, 11, 12, and 13. You can't use those for relays if you are already using them for Ethernet.

You can use Serial In/Parallel Out shift registers to add output pins. Google for a shiftOut() tutorial.

@PeterH

Yes, I am connecting it directly. Actually I am merely a computer programmer. It is my first exposure to hardware ever. Should I use a resistor or capacitor etc in between?

@johnwasser

What about pin 0 and 1. Are these also reserved. Because they also exhibiting same property as pin13 rather pin 11 is behaving normally as pin3....!

You mean the serial pins?

No. Pins lined up on right side when facing SD card slot. I dont know if they are called serial pins or so..!

I dont know if they are called serial pins or so..!

Then, it's time you looked at the board. Pins 0 and 1 are labeled RX and TX. For a reason.

Pins 1 & 2 (right before digital pin 3) are (optionally) used for serial/USB communication.

From the [u]Arduino Uno Product page[/u]:

  • Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.

mtariq61:
Yes, I am connecting it directly

You can’t drive a relay directly from an I/O pin and in attempting to do so there is a good chance that you have blown the pin. You need to use a transistor circuit to amplify the output current from the Arduino so that it can power the relay, and you would probably also need a flyback diode in parallel with the relay coil to protect the transistor.

PeterH: You can't drive a relay directly from an I/O pin...

... Unless the relay coil runs in 5V and less than about 30 mA. ... Or you are using a relay board that has transistor drivers.

Got a new problem. As discussed here I changed my Relay circuit. I added the transistor and made it exactly according to the tutorials provided on forums. But the problem is not with that.

I tested it using LEDs. Using the above program. I tried to set pin3 to HIGH but led did not let. Then I modified the blink program, changing pin13 to pin3. Then I ran it and the LED is working properly as it should.

mtariq61:
I tried to set pin3 to HIGH but led did not let. Then I modified the blink program, changing pin13 to pin3. Then I ran it and the LED is working properly as it should.

If the hardware works as expected with one sketch but not with the other then it suggests that there is something wrong with the sketch that doesn’t work.

But I have gone through my code more than 10 times. And wasn’t able to got a weak point. Sketch is in first post of this thread. I am re-posting it here.

////////////////////////////////////////////////////////////

///////////////        RAAS Client      ///////////////////////
// The Client application to be ran at Arduino Client device.//

////////////////////////////////////////////////////////////

#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();
    }
}
switch (pinNum){
        case PIN1:
                digitalWrite(PIN1, pinState);
                break;

An array would be simpler, and much shorter.

AWOL:

switch (pinNum){

case PIN1:
                digitalWrite(PIN1, pinState);
                break;



An array would be simpler, and much shorter.

An array would be a waste of space.

if (pinNum>=0 && pinNum <=13) {
     digitalWrite(pinNum, pinState);
}
else {
      Serial.println("Invalid Pin Address!");
}

An array would be a waste of space.

No, it wouldn't. With an array, OP could quit f**king around with the SPI pins, for one thing.

All the input comes from the client instance, which means that SPI is most definitely in use, so the SPI pins, and the various SS pins, should not be diddled with.

using namespace std;

Why would anyone ever include this on the Arduino?