Go Down

Topic: Need help with some logic in my sketch. (Read 3248 times) previous topic - next topic

Dustin

I have wired up a garage door remote to my Arduino and made an app in Android to talk to the AsyncLabs WiShield 2.0. Everything works fine however in fixing my Android app I need to add some checks to my Arduino sketch. This is where I'm stuck.

The Android app works like this:

Door   | Circuit
closed | off -- default: door closed circuit off
open   | on -- button pushed 1st time: door open circuit on
open   | off -- after a set delay the app automatically closes circuit
closed | on -- button pushed 2nd time: door closed circuit on
closed | off -- after a set delay the app automatically closes circuit

Like I said, the Android side of things works great.

The Arduino code throws up a website with 3 buttons correlating to 3 pins. And they show on/off when they're pushed (as expected) what I need is some kind of counter to show when the door is open and when the door is off not just when the circuit is open or closed.

If that's too hard to understand I'd like the Arduino to work like this.

Door   | Circuit
closed | off -- default: door closed circuit off
open   | on -- button pushed 1st time: door open circuit on
open   | off -- after a set delay the app automatically closes circuit (and the door remains open)
closed | on -- button pushed 2nd time: door closed circuit on
closed | off -- after a set delay the app automatically closes circuit (and leaves the door closed)

Code: [Select]

#include <WiServer.h>
#include <string.h>

#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

#define ledPin1 5
#define ledPin2 6
#define ledPin3 9

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]    = {192,168,1,225};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,1,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[]                 = {"The Tardis"};   // max 32 bytes
unsigned char security_type = 3;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"biggerontheinside"}; // max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------

boolean states[3]; //holds led states
char stateCounter; //used as a temporary variable
char tmpStrCat[100]; //used in processing the web page
char stateBuff[4]; //used in text processing around boolToString()
char numAsCharBuff[2];
char ledChange;

void boolToString (boolean test, char returnBuffer[4])
{
  returnBuffer[0] = '\0';
  if (test)
  {
    strcat(returnBuffer, "Open");
  }
  else
  {
    strcat(returnBuffer, "Closed");
  }
}

void printStates()
{
        for (stateCounter = 0 ; stateCounter < 3; stateCounter++)
        {
            boolToString(states[stateCounter], stateBuff);
           
            Serial.print("State of ");
            Serial.print(stateCounter);
            Serial.print(": ");
            Serial.println(stateBuff);
        }
}

void writeStates()
{
        //set led states
        digitalWrite(ledPin1, states[0]);
        digitalWrite(ledPin2, states[1]);
        digitalWrite(ledPin3, states[2]);
}

// This is our page serving function that generates web pages
boolean sendPage(char* URL) {
 
  Serial.println("Page printing begun");
 
    printStates();
    writeStates();
   
  //check whether we need to change the led state
  if (URL[1] == '?' && URL[2] == 'L' && URL[3] == 'E' && URL[4] == 'D') //url has a leading /
  {
    ledChange = (int)(URL[5] - 48); //get the led to change.
   
    for (stateCounter = 0 ; stateCounter < 3; stateCounter++)
    {
      if (ledChange == stateCounter)
      {
        states[stateCounter] = !states[stateCounter];
            Serial.print("Have changed ");
            Serial.println(ledChange);
      }
    }
   
    //after having change state, return the user to the index page.
    WiServer.print("<HTML><HEAD><meta http-equiv='REFRESH' content='0;url=/'></HEAD></HTML>");
    return true;
  }
 
  if (strcmp(URL, "/") == false) //why is this not true?
   {
      WiServer.print("<html><head><title>Door Opener</title></head>");
   
      WiServer.print("<body><center>GARAGE DOOR OPENER<center>\n<center><BR><BR>");
      for (stateCounter = 0; stateCounter < 3; stateCounter++) //for each led
      {
        numAsCharBuff[0] = (char)(stateCounter + 49); //as this is displayed use 1 - 3 rather than 0 - 2
        numAsCharBuff[1] = '\0'; //strcat expects a string (array of chars) rather than a single character.
                                 //This string is a character plus string terminator.
       
        tmpStrCat[0] = '\0'; //initialise string
            strcat(tmpStrCat, "<input type=\"submit\" onclick=\"location=\'/?LED \'\" value=\"Opener  \""); //start the string
            tmpStrCat[45] = (char)(stateCounter + 48); //add the led number
            tmpStrCat[63] = (char)(stateCounter + 49);
            tmpStrCat[66] = '\0'; //terminate the string properly for later.
            strcat(tmpStrCat, ">");
            /*strcat(tmpStrCat, numAsCharBuff);
            strcat(tmpStrCat, ": ");*/
           
        boolToString(states[stateCounter], stateBuff);
        strcat(tmpStrCat, "Door ");
        strcat(tmpStrCat, stateBuff);
        strcat(tmpStrCat, "<br>");
        //strcat(tmpStrCat, "</a> "); //we now have something in the range of <a href=?LED0>Led 0: Off</a>
   
        WiServer.print(tmpStrCat);
      }

        WiServer.print("</html> ");
        return true;
   }
}

void setup() {
  // Initialize WiServer and have it use the sendMyPage function to serve pages
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

  Serial.begin(9600);
  WiServer.init(sendPage);
  states[0] = false;
  states[1] = false;
  states[2] = false;
}

void loop(){
  // Run WiServer

  WiServer.server_task();

  delay(10);
}

PaulS

Quote
what I need is some kind of counter to show when the door is open and when the door is off

What you need to do is explain things clearly. A counter counts events. You might count the number of times the door is opened. You might count the number of times the door falls off.

You might increment the counter when the door is opened, and decrement the counter when the door is closed.

I have no idea what you mean by "when the door is off". The door does not have an on/off state, normally. Switches have on/off states. In particular, the open/closed limit switches have states.

What is it you want the Arduino to detect and report? Using what external hardware?

Dustin

#2
Apr 18, 2012, 01:00 pm Last Edit: Apr 18, 2012, 01:02 pm by Dustin Reason: 1
You're right. Let me try again.

If you close the circuit on the Arduino it sends power to the garage door opener that has been wired to it. This opens or closes the garage door depending on if it's already open or closed. The circuit will remain closed and the garage door opener will remain on until you push the button again to open the circuit turning off the garage door opener. This will have no affect on whether or not the actual door is open until you close the circuit again. When you push the button again the process is repeated.

What I meant by counter was something like this:

Default state.
Doorstate = 0;  (door closed)
Circuitstate = 0; (circuit off)

push the button.
Doorstate = 1;    (door open)
Circuitstate = 1; (circuit on)

push it again.
Doorstate = 1;  (door remains open)
Circuitstate = 0;(circuit off)

push it again.
Doorstate = 0;  (door closes)
Circuitstate = 1; (circuit on)

push it once more.
Doorstate = 0; (door remains closed)
Circuitstate = 0; (circuit off)

Hopefully that clears everything up.

PaulS

So, you want to count button presses, and set some booleans to true or false as the count changes. Easy enough. How is the "button" connected to the Arduino?

Dustin

It's an html submit type button. I'm using an AsyncLabs WiShield 2.0. The code looks confusing. I just don't know where to implement the checks / counts.

Code is above in the first post. Html from the WiShield:

Code: [Select]

<html>
<head><title>Door Opener</title></head>
<body>
<center>GARAGE DOOR OPENER<center>
<center>
<BR>
<BR>
<input type="submit" onclick="location='/?LED0'" value="Opener 1">Door Closed<br>
<input type="submit" onclick="location='/?LED1'" value="Opener 2">Door Closed<br>
<input type="submit" onclick="location='/?LED2'" value="Opener 3">Door Closed<br>
</html>


And when you click one of the buttons the page refreshes and the status gets updated.

Dustin

Does that answer your question? Think you could help me out? A friend help me modify the sketch I'm using but neither of us can figure out how or where to put the boolean statements.

PaulS

The GET request that gets sent to the Arduino is different, depending on whether it is an initial request or a submit request. An automatic refresh is just like an initial request.

You should print out the full request, to see the difference(s). One will have ?name=value&name=value type stuff. The other won't.

So, you can distinguish between a status request and a change request. It is only the change requests that you want to count (although I don't think that counting them is the right approach). If you properly act on each change request, opening or closing the door, and you properly read the open and close switches, it is not necessary to count how many times you've been told to open or close the door. You'll know, based on the limit switches, whether the door is open or closed (and based on the last request ) or opening or closing.

Dustin

When I push the submit button on the Arduino's webpage the only thing that shows is the 192.168.1.225/?LED0, 1 or 2

Do you have an example of what you're talking about? Like I said the original code works but I only partially understand it. I've tried googling other examples and found some that seem to work but no source code to reference.

You said counting isn't the best way to go about doing this. What would you suggest?

PaulS

Quote
When I push the submit button on the Arduino's webpage the only thing that shows is the 192.168.1.225/?LED0, 1 or 2

Yes, but when the page is initially requested, there is not ?LEDn string as part of the request. That is how you distinguish am initial/refresh request from an action request.

Dustin

Agreed. But my App for my phone doesn't call the page and then add the ?LEDn it just sends the get request xxx.xxx.xxx.xxx/?LEDn. If I knew how to do what it is you're talking about I probably wouldn't be asking in the forums. Do you have any example code I can look at?

Dustin

After working on this all day I think I'm making progress.

I've now got this:

Code: [Select]

#include <WiServer.h>
#include <string.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

#define ledPin1 5
#define ledPin2 6
#define ledPin3 9

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]    = {192,168,1,225};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,1,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM     = {"The Tardis"};   // max 32 bytes
unsigned char security_type = 3;  // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"biggerontheinside"}; // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;

// Webserver static content ------------------------------------------------
const prog_char wbHtmlOpen[] PROGMEM = {"<html>"};
const prog_char wbHeadOpen[] PROGMEM = {"<head>"};
const prog_char wbTitle[] PROGMEM = {"<title>Door Opener</title>"};
const prog_char wbMetaRefresh[] PROGMEM = {"<HTML><HEAD><meta http-equiv='REFRESH' content='10;url=/'></HEAD></HTML>"};
const prog_char wbDoor1Open[] PROGMEM = {"<h1>Door 1 Open</h1>"};
const prog_char wbDoor1Closed[] PROGMEM = {"<h1>Door 1 Closed</h1>"};
const prog_char wbHeadClose[] PROGMEM = {"</head>"};
const prog_char wbBodyOpen[] PROGMEM = {"<body>"};
const prog_char wbOPEN[] PROGMEM = {"<h1>TEST</h1>"};
const prog_char wbDivPatterns[] PROGMEM = {"<center>GARAGE DOOR OPENER<center>\n<center><BR><BR>"};
const prog_char wbDivPatterns2[] PROGMEM = {"<input type=\"submit\" onclick=\"location=\'/?LED0 \'\" value=\"Button 1\"><br><br><input type=\"submit\" onclick=\"location=\'/?LED1 \'\" value=\"Button 2\"><br><br><input type=\"submit\" onclick=\"location=\'/?LED2 \'\" value=\"Button 3\"><br>"};
const prog_char wbBodyClose[] PROGMEM = {"</body>"};
const prog_char wbHtmlClose[] PROGMEM = {"</html>"};


int incomingByte = 0;
int webnum = -1;
int num = -1;
int option1State = 0;
int circuit1State = 0;
int test = 0;

// Methods ------------------------------------------------------------------

boolean sendPage(char* URL) {
 
if (URL[1] == '?' && URL[2] == 'L' && URL[3] == 'E' && URL[4] == 'D') //url has a leading /
{incomingByte = URL[5];
if(incomingByte >= 48 && incomingByte <=57){
   webnum = incomingByte - 48;
}

  WiServer.println_P(wbHtmlOpen);
  WiServer.println_P(wbHeadOpen);
  WiServer.println_P(wbTitle);
  WiServer.println_P(wbMetaRefresh);
  WiServer.println_P(wbHeadClose);
  WiServer.println_P(wbBodyOpen);
  WiServer.println_P(wbDivPatterns);
  WiServer.println_P(wbDivPatterns2);
  WiServer.println_P(wbBodyClose);
  WiServer.println_P(wbHtmlClose);
  return true;
}
}

void numDisplay(int num){
  if(num == 0){
    test++;
     if(test == 1){
     if ((option1State == 0) && (circuit1State == 0)){
       WiServer.print(wbDoor1Open);
       option1State = 1;
       circuit1State = 1;
       digitalWrite(ledPin3, LOW);
       }
    } 
  if(test == 2){
     if ((option1State == 1) && (circuit1State == 1)){
       WiServer.print(wbDoor1Closed);
       option1State = 0;
       circuit1State = 0;
       test = 0;
       digitalWrite(ledPin3, HIGH);
      }
    }
}
  if(num == 1){
 
  }
  if(num == 2){

  }
}

void setup() {
//setup leds
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
WiServer.init(sendPage);
}

void loop() {
  num = webnum;
  numDisplay(num);
   WiServer.server_task();
   delay(100);
}


However the check

Code: [Select]

  if(num == 0){
    test++;
     if(test == 1){
     if ((option1State == 0) && (circuit1State == 0)){
       WiServer.print(wbDoor1Open);
       option1State = 1;
       circuit1State = 1;
       digitalWrite(ledPin3, LOW);
       }
    } 
  if(test == 2){
     if ((option1State == 1) && (circuit1State == 1)){
       WiServer.print(wbDoor1Closed);
       option1State = 0;
       circuit1State = 0;
       test = 0;
       digitalWrite(ledPin3, HIGH);
      }
    }
}


Just makes the LED blink. Any suggestions?

PaulS

Quote
Any suggestions?

Yes. Try to make your code make sense.

Code: [Select]
{incomingByte = URL[5];
NOTHING else goes on the line with the {
Code: [Select]
{
   incomingByte = URL[5];

If you INSIST on putting them together, at least use some damned space between the { and the statement.

Code: [Select]
if(incomingByte >= 48 && incomingByte <=57){
Maybe you got your jollies memorizing the ASCII table, but the rest of the world finds this:
Code: [Select]
if(incomingByte >= '0' && incomingByte <= '9')
{

easier to understand.

Code: [Select]
if (URL[1] == '?' && URL[2] == 'L' && URL[3] == 'E' && URL[4] == 'D') //url has a leading /
So?
Code: [Select]
if(strncmp(URL, "/?LED", 5) == 0)
{
}

Much easier to understand and change.

Code: [Select]
incomingByte = URL[5];
if(incomingByte >= 48 && incomingByte <=57){
  webnum = incomingByte - 48;

should be
Code: [Select]
incomingByte = URL[5];
if(incomingByte >= '0' && incomingByte <= '9')
{
  webnum = incomingByte - '0';


Of course, why sendPage() returns a boolean, when the value that it returns is hardcoded to true is a mystery.


option1State? What is option1?

Dustin

#12
Apr 20, 2012, 02:05 am Last Edit: Apr 20, 2012, 02:08 am by Dustin Reason: 1
Everything in that post up there ^^ is true. But like I said I found that sketch on the AsyncLab's site. A friend of mine helped me get to where I was at when I started asking questions. None of it made sense to me, either. I think I've figured it out now, though. I just got this done tonight:

Code: [Select]

#include <WiServer.h>
#include <string.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

#define ledPin1 5
#define ledPin2 6
#define ledPin3 9

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]    = {192,168,1,225};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,1,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM     = {"The Tardis"};   // max 32 bytes
unsigned char security_type = 3;  // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"biggerontheinside"}; // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;

// Webserver static content ------------------------------------------------
const prog_char wbHtmlOpen[] PROGMEM = {"<html>"};
const prog_char wbHeadOpen[] PROGMEM = {"<head>"};
const prog_char wbTitle[] PROGMEM = {"<title>Door Opener</title>"};
const prog_char wbMetaRefresh[] PROGMEM = {"<HTML><HEAD><meta http-equiv='REFRESH' content='60;url=/'></HEAD></HTML>"};
const prog_char wbHeadClose[] PROGMEM = {"</head>"};
const prog_char wbBodyOpen[] PROGMEM = {"<body>"};
const prog_char wbDoor1Open[] PROGMEM = {"<center><h1>Door 1 Open</h1></center>"};
const prog_char wbDoor1Closed[] PROGMEM = {"<center><h1>Door 1 Closed</h1></center>"};
const prog_char wbDoor2Open[] PROGMEM = {"<center><h1>Door 2 Open</h1></center>"};
const prog_char  wbDoor2Closed[] PROGMEM = {"<center><h1>Door 2 Closed</h1></center>"};
const prog_char wbDoor3Open[] PROGMEM = {"<center><h1>Door 3 Open</h1></center>"};
const prog_char wbDoor3Closed[] PROGMEM = {"<center><h1>Door 3 Closed</h1></center>"};
const prog_char wbDivPatterns[] PROGMEM = {"<center>GARAGE DOOR OPENER<center>\n<center><BR><BR>"};
const prog_char wbDivPatterns2[] PROGMEM = {"<input type=\"submit\" onclick=\"location=\'/?LED1 \'\" value=\"Button 1 On\"><br><input type=\"submit\" onclick=\"location=\'/?LED2 \'\" value=\"Button 2 On\"><br><input type=\"submit\" onclick=\"location=\'/?LED3 \'\" value=\"Button 3 On\"><br><br>"};
const prog_char wbDivPatterns3[] PROGMEM = {"<input type=\"submit\" onclick=\"location=\'/?LED4 \'\" value=\"Button 1 Off\"><br><input type=\"submit\" onclick=\"location=\'/?LED5 \'\" value=\"Button 2 Off\"><br><input type=\"submit\" onclick=\"location=\'/?LED6 \'\" value=\"Button 3 Off\"><br>"};
const prog_char wbBodyClose[] PROGMEM = {"</body>"};
const prog_char wbHtmlClose[] PROGMEM = {"</html>"};


int incomingByte = 0;
int webnum = -1;
char ledChange;
char stateCounter;
char doorState;
// Methods ------------------------------------------------------------------

boolean sendPage(char* URL) {
 
if (URL[1] == '?' && URL[2] == 'L' && URL[3] == 'E' && URL[4] == 'D') //url has a leading /
{
 ledChange = (int)(URL[5]) - 48;
 for (stateCounter = 0; stateCounter <6; stateCounter++)
 {
   if(ledChange == 1){
     digitalWrite(ledPin1, HIGH);
   }
   if(ledChange == 2){
     digitalWrite(ledPin2, HIGH);
   }
   if(ledChange == 3){
     digitalWrite(ledPin3,HIGH);
   }
   if(ledChange == 4){
     digitalWrite(ledPin1, LOW);
   }
   if(ledChange == 5){
     digitalWrite(ledPin2, LOW);
   }
   if(ledChange == 6){
     digitalWrite(ledPin3,LOW);
   }
}
   WiServer.print("<HTML><HEAD><meta http-equiv='REFRESH' content='0;url=/'></HEAD></HTML>");
   return true;
}

 WiServer.println_P(wbHtmlOpen);
 WiServer.println_P(wbHeadOpen);
 WiServer.println_P(wbTitle);
 WiServer.println_P(wbMetaRefresh);
 WiServer.println_P(wbHeadClose);
 WiServer.println_P(wbBodyOpen);
 WiServer.println_P(wbDivPatterns);
 WiServer.println_P(wbDivPatterns2);
 WiServer.println_P(wbDivPatterns3);
   if(ledChange == 1){
   WiServer.println_P(wbDoor1Open);
 }
 if (ledChange == 2){
    WiServer.println_P(wbDoor2Open);
 }
 if (ledChange ==3) {
    WiServer.println_P(wbDoor3Open);
 }
 if(ledChange == 4){
   WiServer.println_P(wbDoor1Closed);
 }
 if(ledChange == 5){
   WiServer.println_P(wbDoor2Closed);
 }
 if (ledChange == 6){
   WiServer.println_P(wbDoor3Closed);
 }
 WiServer.println_P(wbBodyClose);
 WiServer.println_P(wbHtmlClose);
 return true;
}

void setup() {
//setup leds
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
WiServer.init(sendPage);
}

void loop() {
  WiServer.server_task();
  delay(100);
}

But it still uses that ASCII thing (because I copied it from the previous sketch.) I'll take your words and fix my newest sketch. Thanks for taking the time to help.

lloyddean

Nothing to add other than a simple comment (maybe even a reminder).

'ledChange' will be any ONE of a range of values and once identified you need search no longer.

Code: [Select]

if      ( ledChange == 1 )  { digitalWrite(ledPin1, HIGH); }
else if ( ledChange == 2 )  { digitalWrite(ledPin2, HIGH); }
else if ( ledChange == 3 )  { digitalWrite(ledPin3, HIGH); }
else if ( ledChange == 4 )  { digitalWrite(ledPin1,  LOW); }
else if ( ledChange == 5 )  { digitalWrite(ledPin2,  LOW); }
else if ( ledChange == 6 )  { digitalWrite(ledPin3,  LOW); }


Code: [Select]

if      ( ledChange == 1 )  { WiServer.println_P(  wbDoor1Open); }
else if ( ledChange == 2 )  { WiServer.println_P(  wbDoor2Open); }
else if ( ledChange == 3 )  { WiServer.println_P(  wbDoor3Open); }
else if ( ledChange == 4 )  { WiServer.println_P(wbDoor1Closed); }
else if ( ledChange == 5 )  { WiServer.println_P(wbDoor2Closed); }
else if ( ledChange == 6 )  { WiServer.println_P(wbDoor3Closed); }


Then there is the fact that both these search chains could be made shorter and more linear.  I won't say anything more about that unless you're interested.

Dustin

#14
Apr 20, 2012, 03:05 am Last Edit: Apr 20, 2012, 03:22 am by Dustin Reason: 1
I'd love to read more. Right now I'm working on displaying all three states and updating them when they change.

Go Up