Moving servo from website on SD card

Hello everybody

i am working on a project for my model railway. i want to use a servo motor for moving the turnouts instead of the more expensive turnout motors.

First i was able to make i work by having the HTML code embedded in the arduino code.

next step was to put the HTML code on the sd card. I the tried to make this code:

#include <Servo.h>


#include <Ethernet.h>


#include <SPI.h>


#include <SD.h>








byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA6, 0x2B };


byte ip[] = { 192, 168, 1, 178 }; 
("192.168.1.178")


byte gateway[] = { 192, 168, 1, 1 }; 


byte subnet[] = { 255, 255, 255, 0 }; 


EthernetServer server(80);


String readString;





File webFile;





// constant variables used to set servo angles, in degrees


const int straight = 80; 


const int divergent = 130;





// constant variables holding the ids of the pins we are using


const int buttonpin = 8;


const int servopin = 9;





// servo movement step delay, in milliseconds


const int step_delay = 70;





// create a servo object


Servo myservo;  





// global variables to store servo position


int pos = straight; // current


int old_pos = pos; // previous





void setup() 


{ 


 


 Ethernet.begin(mac, ip);  // initialize Ethernet device


 server.begin();           // start to listen for clients


 // Open serial communications and wait for port to open:


 Serial.begin(9600);


 


 // set the mode for the digital pins in use


 pinMode(buttonpin, INPUT);


  


 // setup the servo


 myservo.attach(servopin);  // attach to the servo on pin 9


 myservo.write(pos); // set the initial servo position


 


 Ethernet.begin(mac, ip, gateway, subnet);


 server.begin();


 Serial.print("server is at ");


 Serial.println(Ethernet.localIP());


 


  // initialize SD card


   Serial.println("Initializing SD card...");


   if (!SD.begin(4)) {


       Serial.println("ERROR - SD card initialization failed!");


       return;    // init failed


   }


   Serial.println("SUCCESS - SD card initialized.");


   // check for index.htm file


   if (!SD.exists("index.htm")) {


       Serial.println("ERROR - Can't find index.html file!");


       return;  // can't find index file


   }


   Serial.println("SUCCESS - Found index.html file.");





 


}





void loop() 


{ 


 


 EthernetClient client = server.available();


 if (client) 


 { 


   boolean currentLineIsBlank = true;


   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


    


          client.println("HTTP/1.1 200 OK"); //send new page


          client.println("Content-Type: text/html");


          client.println();     


          


          


           webFile = SD.open("index.htm");        // open web page file


                   if (webFile) {


                       while(webFile.available()) {


                           client.write(webFile.read()); // send web page to client


                       }


                       webFile.close();


                   }


                   break;


               }


               // every line of text received from the client ends with \r\n


               if (c == '\n') {


                   // last character on line of received text


                   // starting new line with next character read


                   currentLineIsBlank = true;


               } 


               else if (c != '\r') {


                   // a text character was received from client


                   currentLineIsBlank = false;


               }


           } // end if (client.available())


       } // end while (client.connected())


       delay(1);      // give the web browser time to receive the data


       client.stop(); // close the connection


   } // end if (client)


  
   old_pos = pos;   // save the current position


   


   // Toggle the position to the opposite value


   pos = pos == straight ? divergent: straight;


   


   


   if (readString.indexOf("?button2on") >0)


   {   


     // Move the servo to its new position


     if(old_pos < pos)


     {   // if the new angle is higher


       // increment the servo position from oldpos to pos


       for(int i = old_pos + 1; i <= pos; i++)


       {  


         myservo.write(i); // write the next position to the servo


         delay(step_delay); // wait


       }


     } 


     else 


     {  // otherwise the new angle is equal or lower


       // decrement the servo position from oldpos to pos


       for(int i = old_pos - 1; i >= pos; i--)


       { 


         myservo.write(i); // write the next position to the servo


         delay(step_delay); // wait


       }


     }


   }


}

Now the servo just moves from side to side and doesn’t care if i press the button or not.

i think that i has something to do with this part:

if (readString.indexOf("?button2on") >0)

But i´m not sure what to write in the if statement…

Pleace help me.

("192.168.1.178")

What is this supposed to do?

What's with all the stupid white space? Clean up your code before wasting our time.

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

So, you know what the client requested. That makes one of us.

i think that i has something to do with this part:

Unless you share what the GET request looked like, we'll have to go with your guessing.

Add some Serial.print() statements after that if statement, to see what the Arduino decided.

PaulS:

("192.168.1.178")

What is this supposed to do?

What's with all the stupid white space? Clean up your code before wasting our time.

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

So, you know what the client requested. That makes one of us.
Unless you share what the GET request looked like, we'll have to go with your guessing.

Add some Serial.print() statements after that if statement, to see what the Arduino decided.

first of all thank you for your kind reply.. i´m sorry if i wasted your time with "all of the stupid white space"

if you care to take a look at the left of my post it says that i´m a newbie at this. So once again i am sorry to waste your time with my question. but then again i did not ask for you to comment on my problem if it was to be any kind of a way to ruin your day.

Thank you for that one advise at the very end of your reply...i will try that.

have at nice day and a HAPPY new year..

i´m sorry if i wasted your time with “all of the stupid white space”

Only be sorry that somebody was probably potty trained on a electric chair. I’ve attached some files that make a web page slider setup for servos which is served up from an SD card. Not exactly what you are looking for, but you can see some of the basic concepts.

slider files.zip (6.11 KB)

What's with all the stupid white space?

...

Only be sorry that somebody was probably potty trained on a electric chair.

Come on, everyone. Try a little harder to be nice to each other. It's New Year's Eve for goodness sake. Try to relax and enjoy each others' companies. Or if that isn't possible, write an insulting reply, and then delete it rather than posting it. It is quite therapeutic doing that. :slight_smile:

What's with all the stupid white space?

@PaulS: Let me rephrase that for you:

Your excessive use of white space makes the code difficult to read. If you bought a book with a lot of excessive gaps between lines you would find it hard to read.

Hello Nick and Zoomcat

and thank you for replying

i haven´t had the time to look further into the slider files but i definitely will as soon as possible as far as i can tell it is possible to build it further into what i am trying to achieve

@Nick Gammon

I will try to minimize my use of white space. i hope that you had a great new year :slight_smile: