Pages: [1]   Go Down
Author Topic: LED not acting like it should  (Read 517 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Guys,

This is a newbie problem, but I am trying to turn on pin 13 led on and off via the web server onboard. The web server is working and ethernet but the led does weird behavior by staying on and looping to on. It will never stay off. Can anyone try this code and let me know where I have gone wrong?

This code is for ethershield but I have the Etherten board from freetronics.


Code:
//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://10.20.0.199:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 10, 20, 0, 199 }; // ip in lan
byte gateway[] = { 10, 20, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 252, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

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

void setup(){

  pinMode(13, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>LED off and on web based</H1>");
         
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(13, HIGH);    // set pin 13 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(13, LOW);    // set pin 13 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50843
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Ethernet shield is an SPI device. The SPI pins are 11, 12, and 13. You can NOT use pin 13 for any other purpose while using the Ethernet shield. You actually have to find an LED and current limiting resistor and wire one up to an unused pin.
Logged

0
Offline Offline
Shannon Member
****
Karma: 220
Posts: 12700
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I note the use of String append - you may run out of RAM.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed the code to below and still have same issue. Funny thing is as soon as I browse to the URL the LED goes high. then when pressing on or off it will blink and go high again never turning off. I have a current limiting resistor picture below.

Is there another way rather than append to resolve the memory issue?



Code:
//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://10.20.0.199:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 10, 20, 0, 199 }; // ip in lan
byte gateway[] = { 10, 20, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 252, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

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

void setup(){

  pinMode(7, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>LED off and on web based</H1>");
         
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(7, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(7, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}


Logged

0
Offline Offline
Shannon Member
****
Karma: 220
Posts: 12700
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the string "on" present in the body of the URL perhaps?  Can we see the serial output taces?
Logged

[ I won't respond to messages, use the forum please ]

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50843
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have a current limiting resistor picture below.
Connected to an SPI pin. You STILL can not use the SPI pins for anything other than the Ethernet connection. Get over it.
Logged

Liberal, Kansas
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have a current limiting resistor picture below.
Connected to an SPI pin. You STILL can not use the SPI pins for anything other than the Ethernet connection. Get over it.

It looks to me like he has the resistor in pin 7 and the LED grounded to the ground pin next to pin 13 to me.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50843
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It looks to me like he has the resistor in pin 7 and the LED grounded to the ground pin next to pin 13 to me.
My bad.

I still have some issues with the code:
Code:
          Serial.println(readString); //print to serial monitor for debuging
The debug output is not shown.

Code:
            digitalWrite(7, HIGH);    // set pin 4 high
            digitalWrite(7, LOW);    // set pin 4 low
The code does not do what the comments say.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect the problem is that your browser is automatically trying to download "favicon.ico" for your web site, which happens to contain your "on" string. Your Serial.println(readString); should have shown you that. You could avoid the problem by making the strings unique, for example by converting them to uppercase or by testing for "?on" etc.

As a general principle I recommend using c-strings rather than the String class. The String class does not give you any significant benefits, exposes a memory leak that can cause instability problems on older IDE versions, and exposes you to possible memory fragmentation issues on all versions. Using dynamic memory to manage dynamic data is fine on a PC with virtual memory support, loads of RAM and the resources to do proper garbage collection and compaction - it's a fundamentally bad strategy on a memory-starved microcontroller.

To eliminate the String class you would replace String readString with a char array, and have a variable to record how many characters are stored in the array.
Code:
const int BUFFER_SIZE = 80; // or however big it needs to be in your application
char buffer[BUFFER_SIZE+1]; // +1 allows space for the null terminator
int length = 0;
Then you append new characters like this:

Code:
if(length < BUFFER_SIZE)
{
  buffer[length++] = c;
  buffer[length] = 0; // append null terminator;
}

Clear the buffer by setting length=0.

Use strstr() to test whether the buffer contains a specified string.

Code:
if(strstr(buffer, "?on") != 0)
{
    // match found ...
}

If you want to print the content of the buffer, you can pass the buffer to any of the usual print functions:
Code:
Serial.print(buffer);
« Last Edit: April 21, 2013, 09:30:28 am by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: