Shift register controled through webpage question

Hello!

I have the code below that turns on LEDs depending on what dataArray is being read. I found the code here http://www.arduino.cc/en/tutorial/ShiftOut Code Sample 1.3 Using an array.

I would like to be able to have a webpage that has 8 radio on/off buttons that would be read either on or off then the values made into an array that is sent as the dataArray when a submit button is clicked.

For example, radio buttons 1,3,5, and 7 are turned ON and 2,4,6,8 are turned OFF. So the dataArray would read 0B10101010 and be sent to turn on the LEDs when the button is pushed.

Does anyone have an idea of how to go about this? Any advice is greatly appreciated!!

/*
   Shift Register Example
   Turning on the outputs of a 74HC595 using an array

  Hardware:
  * 74HC595 shift register 
  * LEDs attached to each of the outputs of the shift register

  */
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//holders for infromation you're going to pass to shifting function
byte data;
byte dataArray[10];

void setup() {
   //set pins to output because they are addressed in the main loop
   pinMode(latchPin, OUTPUT);
   Serial.begin(9600);

   //Binary notation as comment
   dataArray[0] = 0xFF; //0b11111111
   dataArray[1] = 0xFE; //0b11111110
   dataArray[2] = 0xFC; //0b11111100
   dataArray[3] = 0xF8; //0b11111000
   dataArray[4] = 0xF0; //0b11110000
   dataArray[5] = 0xE0; //0b11100000
   dataArray[6] = 0xC0; //0b11000000
   dataArray[7] = 0x80; //0b10000000
   dataArray[8] = 0x00; //0b00000000

}

void loop() {

   for (int j = 0; j < 9; j++) {
     //load the light sequence you want from array
     data = dataArray[j];
     //ground latchPin and hold low for as long as you are transmitting
     digitalWrite(latchPin, 0);
     //move 'em out
     shiftOut(dataPin, clockPin, data);
     //return the latch pin high to signal chip that it 
     //no longer needs to listen for information
     digitalWrite(latchPin, 1);
     delay(300);
   }
}



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
   // This shifts 8 bits out MSB first, 
   //on the rising edge of the clock,
   //clock idles low

   //internal function setup
   int i=0;
   int pinState;
   pinMode(myClockPin, OUTPUT);
   pinMode(myDataPin, OUTPUT);

   //clear everything out just in case to
   //prepare shift register for bit shifting
   digitalWrite(myDataPin, 0);
   digitalWrite(myClockPin, 0);

   //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
   //This means that %00000001 or "1" will go through such
   //that it will be pin Q0 that lights. 
   for (i=7; i>=0; i--)  {
     digitalWrite(myClockPin, 0);

     //if the value passed to myDataOut and a bitmask result 
     // true then... so if we are at i=6 and our value is
     // %11010100 it would the code compares it to %01000000 
     // and proceeds to set pinState to 1.
     if ( myDataOut & (1<<i) ) {
       pinState= 1;
     }
     else {  
       pinState= 0;
     }

     //Sets the pin to HIGH or LOW depending on pinState
     digitalWrite(myDataPin, pinState);
     //register shifts bits on upstroke of clock pin  
     digitalWrite(myClockPin, 1);
     //zero the data pin after shift to prevent bleed through
     digitalWrite(myDataPin, 0);
   }
//stop shifting
   digitalWrite(myClockPin, 0);
}

An 8 bit data array should be a byte. Just one byte, one variable of type byte is 8 bits.

A complete lesson all typed up is at this link:
http://playground.arduino.cc/Code/BitMath

Learn this and you will have fewer questions in the future.

Really, bits in variables are all true/false. If I have a pin watching a button, I keep the last checked pin state in bit 1 and read the new pin state into bit 0 (digitalRead returns a 0 or 1, that is bit 0) so with a single check of the whole variable I can know past and present combined. But you gotta know how bits work to understand.

So the dataArray would read 0B10101010 and be sent to turn on the LEDs when the button is pushed.

No, the dataArray would not read that. Octal constants, which start with 0 can not contain a B.

It your webpage being sent directly to the arduino? The arduino is working as a web server?

In that case, you'd definitely want to use a library to read the http request from the web browser, so "how do I do this?" comes down to reading the documentation for the library you are using.

If you are not using a library, then you'll need to understand:

  • how HTML forms convert selections on a form into URL parameters
  • what a HTTP request looks like when it is read off an input stream.

On an HTTP form, checkboxes each have a name. If the checkbox is checked, then the url request includes a parameter named that with a value of "on", i think.

So if your form looks like this:

<form method="GET">
  <input type="checkbox" name="foo"/>
  <input type="checkbox" name="bar"/>
  <input type="submit"/>
</form>

And the user checks 'foo', then your arduino will get a HTTP request that looks a bit like this:

GET /?foo=on HTTP/1.1
Content-Length: 0
other-header: some-value
other-header2: some-value
-- BLANK LINE --

To do this correctly, you need to

  • read that first line
  • parse it into request method, uri, and http version
  • if the http version is not 'HTTP/1.1', then just barf and say "I have no idea what this is"
  • read all of the headers
    ** parse each header into name and value
    ** as you read the headers, if any of them is Content-Length then make a record of the value
  • stop reading headers when you hit a blank line
  • If the request method was not HEAD, then read the request body:
    ** if there was a Content-Length, then read that many votes band discard them
    ** if there was no Content-Length, read bytes until no more bytes are available, then close the connection

Then you need to parse the uri into request path, '?', list of parameters separated by ampersands
Then you need to separate each parameter into parameter name and parameter value
Then - optional step - you need to decode the parameter value, which may contain percent escapes.

So you can see why people just plug in a library. In your case, if you choose the name of the checkboxes carefully, you can just use strstr() to search that first line for those names, because if the check box is not checked then 'checkbox_3' won't be in the request uri at all. And you can handle the entire rest of the message by reading it until nothing is available, and closing the connection (which is a legit thing to do).

One of probably many check box tutorials.

Thank you everyone for your input. I have been able to get this code below to work some but it has problems and I think there is a better way to do it. It creates a webpage with two radio button sets to turn two LEDs on and off.

Problems with this code:
If I turn both LEDs ON the first will light for a second(because I added the delay) then go off and the second will come On and stay on. When you click the submit button the radio buttons both have the off radio inputs selected even if you selected one or both to be on.

Does anyone have an idea of how to make this better? I need it to be able to handle 8 LED's total so I am not sure if this would be the best way to go.

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


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0E, 0xCC, 0xC8 };
IPAddress ip(192,168,1, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

String HTTP_req; //Stores the HTTP request
boolean LED_status =0;

//Pin connected to ST_CP 
const int latchPin= 8;

//Pin connected to SH_CP
const int clockPin= 12;

//Pin connected to DS 
const int dataPin= 11;

void setup(){
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
 // pinMode(2,OUTPUT);
  //The bits you want to send
byte bitsToSend= 0;

//turn off the output so the pins don't light up while your shifting bits
digitalWrite(latchPin, LOW);

//turn on the next highest bit in bitsToSend
bitWrite(bitsToSend, B00000000, HIGH);

//shift the bits out
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

//turn on the output so the LEDs can light up
digitalWrite(latchPin, HIGH);
}

void loop(){
  EthernetClient client = server.available();
  
  if (client){
    boolean currentLineIsBlank = true;
    while (client.connected()){
      if (client.available()){
        char c = client.read();
        HTTP_req += c;
        if (c=='\n' && currentLineIsBlank){
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          //TITLE OF THE WEBPAGE
          client.println("<title>Arduino LED control</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<h1>LED</h1>");
          client.println("<p> Click to switch LED on and off.</p>");
          client.println("<form method =\"get\">");
          client.println("<input type=\"radio\" name= \"LED2\" value= \"1\">LED2 On<input type=\"radio\" name= \"LED2\" value= \"0\" checked>LED2 Off");
          client.println("<input type=\"radio\" name= \"LED1\" value= \"2\">LED1 On<input type=\"radio\" name= \"LED1\" value= \"0\" checked>LED1 Off");
          client.println("<input type=\"submit\" value=\"submit\">");
          ProcessCheckbox(client);
          client.println("</form>");
          client.println("</body>");
          client.println("</html>");
          Serial.print(HTTP_req);
          HTTP_req = "";
          break;
        } //end  if (c=='\n' && currentLineIsBlank)
        if (c== '\n'){
          currentLineIsBlank = true;
        }
        else if (c !='\r'){
          currentLineIsBlank = false;
        }
      } // end if (client.available())
    } // end while (client.connected())
    delay(1);
    client.stop();
  } //end if (client)
} //end void

//switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl){
  
  if (HTTP_req.indexOf("LED2=1") >-1){
       //The bits you want to send
      byte bitsToSend= 0;
      
      //turn off the output so the pins don't light up while your shifting bits
      digitalWrite(latchPin, LOW);
      
      //turn on the next highest bit in bitsToSend
      bitWrite(bitsToSend, 0, HIGH);
      
      //shift the bits out
      shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
      //turn on the output so the LEDs can light up
      digitalWrite(latchPin, HIGH);
      delay(1000);
    }
    
  if (HTTP_req.indexOf("LED2=0") >-1){
      //The bits you want to send
      byte bitsToSend= 0;
      
      //turn off the output so the pins don't light up while your shifting bits
      digitalWrite(latchPin, LOW);
      
      //turn on the next highest bit in bitsToSend
      bitWrite(bitsToSend, 0, LOW);
      
      //shift the bits out
      shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
      //turn on the output so the LEDs can light up
      digitalWrite(latchPin, HIGH);
    } // end if (HTTP_req.indexOf("LED2=2") >-1)
    
  if (HTTP_req.indexOf("LED1=0") >-1){
      //The bits you want to send
      byte bitsToSend= 0;
      
      //turn off the output so the pins don't light up while your shifting bits
      digitalWrite(latchPin, LOW);
      
      //turn on the next highest bit in bitsToSend
      bitWrite(bitsToSend, 1, LOW);
      
      //shift the bits out
      shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
      //turn on the output so the LEDs can light up
      digitalWrite(latchPin, HIGH);
  }
  if (HTTP_req.indexOf("LED1=2") >-1){
       //The bits you want to send
      byte bitsToSend= 0;
      
      //turn off the output so the pins don't light up while your shifting bits
      digitalWrite(latchPin, LOW);
      
      //turn on the next highest bit in bitsToSend
      bitWrite(bitsToSend, 1, HIGH);
      
      //shift the bits out
      shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
      //turn on the output so the LEDs can light up
      digitalWrite(latchPin, HIGH);
      delay(1000);
    }  
}