Go Down

Topic: LED not acting like it should (Read 550 times) previous topic - next topic

sheeds

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: [Select]

//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="";

        }
      }
    }
  }
}

PaulS

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.

MarkT


I note the use of String append - you may run out of RAM.
[ I won't respond to messages, use the forum please ]

sheeds

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: [Select]

//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="";

        }
      }
    }
  }
}




MarkT

Is the string "on" present in the body of the URL perhaps?  Can we see the serial output taces?
[ I won't respond to messages, use the forum please ]

PaulS

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.

daveydav27


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.

PaulS

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: [Select]
          Serial.println(readString); //print to serial monitor for debuging
The debug output is not shown.

Code: [Select]
            digitalWrite(7, HIGH);    // set pin 4 high
            digitalWrite(7, LOW);    // set pin 4 low

The code does not do what the comments say.

PeterH

#8
Apr 21, 2013, 04:22 pm Last Edit: Apr 21, 2013, 04:30 pm by PeterH Reason: 1
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: [Select]

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: [Select]

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: [Select]

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: [Select]
Serial.print(buffer);
I only provide help via the forum - please do not contact me for private consultancy.

Go Up