Pages: 1 [2]   Go Down
Author Topic: Need help with some logic in my sketch.  (Read 2861 times)
0 Members and 1 Guest are viewing this topic.
Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I notice that 'ledChange' is modified only if 'url' + 1 is '?LED' this works for the first 'if' 'ledChange' search chain but will not be correct for the second further down.

EDIT:

Notice you're looping 6 times without any change to 'ledChange' so the very same 'digitalWrite' happens 6 time producing no visible change.

Code:
for ( stateCounter = 0; stateCounter <6; stateCounter++ )
{
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); }
}
« Last Edit: April 19, 2012, 11:10:47 pm by lloyddean » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought because I had 6 states (3 on 3 off) i needed to change it to 6 (default was 3). I changed it to loop once but then the website died after pushing a few buttons. It's set at 3 now and it's doing pretty well. Here's the updated code:

Code:
#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 Is Open</h1></center>"};
const prog_char wbDoor1Closed[] PROGMEM = {"<center><h1>Door 1 Is Closed</h1></center>"};
const prog_char wbDoor2Open[] PROGMEM = {"<center><h1>Door 2 Is Open</h1></center>"};
const prog_char  wbDoor2Closed[] PROGMEM = {"<center><h1>Door 2 Is Closed</h1></center>"};
const prog_char wbDoor3Open[] PROGMEM = {"<center><h1>Door 3 Is Open</h1></center>"};
const prog_char wbDoor3Closed[] PROGMEM = {"<center><h1>Door 3 Is 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>"};

char ledChange;
char stateCounter;
boolean door1closed;
boolean door2closed;
boolean door3closed;
// Methods ------------------------------------------------------------------

boolean sendPage(char* URL) {

if(strncmp(URL, "/?LED", 5) == 0)
{
  ledChange = (int)(URL[5]) - 48;
  for (stateCounter = 0; stateCounter <3; stateCounter++)
  {
    if      ( ledChange == 1 )  { digitalWrite(ledPin1, HIGH); door1closed = false; }
    else if ( ledChange == 2 )  { digitalWrite(ledPin2, HIGH); door2closed = false; }
    else if ( ledChange == 3 )  { digitalWrite(ledPin3, HIGH); door3closed = false; }
    else if ( ledChange == 4 )  { digitalWrite(ledPin1,  LOW); door1closed = true;  }
    else if ( ledChange == 5 )  { digitalWrite(ledPin2,  LOW); door2closed = true;  }
    else if ( ledChange == 6 )  { digitalWrite(ledPin3,  LOW); door3closed = true;  }
 }
    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 ( door1closed == false ) { WiServer.println_P(wbDoor1Open);  }
  if ( door1closed == true  ) { WiServer.println_P(wbDoor1Closed);}
  if ( door2closed == false ) { WiServer.println_P(wbDoor2Open);  }
  if ( door2closed == true  ) { WiServer.println_P(wbDoor2Closed);}
  if ( door3closed == false ) { WiServer.println_P(wbDoor3Open);  }
  if ( door3closed == true  ) { 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);
}

Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I'm going to add some ping sensors at the top and bottom of the garage door. If the door is close to the sensor at the top then door = open. If the door is close to the sensor at the bottom then door = closed. Seems like a decent way to detect the door's state rather than simply an on/off switch.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same thing.
See my edit in message #15.

Why loop 6 time when nothing changes as result?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I read that about the loops 6 times. I don't know if the website directly died quicker because of this but the code I copied had 3 so I changed it back to 3. It works so I'm not too worried about it.

I thought because I had 6 states (3 on 3 off) i needed to change it to 6 (default was 3). I changed it to loop once but then the website died after pushing a few buttons. It's set at 3 now and it's doing pretty well. Here's the updated code:

Code:
#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 Is Open</h1></center>"};
const prog_char wbDoor1Closed[] PROGMEM = {"<center><h1>Door 1 Is Closed</h1></center>"};
const prog_char wbDoor2Open[] PROGMEM = {"<center><h1>Door 2 Is Open</h1></center>"};
const prog_char  wbDoor2Closed[] PROGMEM = {"<center><h1>Door 2 Is Closed</h1></center>"};
const prog_char wbDoor3Open[] PROGMEM = {"<center><h1>Door 3 Is Open</h1></center>"};
const prog_char wbDoor3Closed[] PROGMEM = {"<center><h1>Door 3 Is 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>"};

char ledChange;
char stateCounter;
boolean door1closed;
boolean door2closed;
boolean door3closed;
// Methods ------------------------------------------------------------------

boolean sendPage(char* URL) {

if(strncmp(URL, "/?LED", 5) == 0)
{
  ledChange = (int)(URL[5]) - 48;
  for (stateCounter = 0; stateCounter <3; stateCounter++)
  {
    if      ( ledChange == 1 )  { digitalWrite(ledPin1, HIGH); door1closed = false; }
    else if ( ledChange == 2 )  { digitalWrite(ledPin2, HIGH); door2closed = false; }
    else if ( ledChange == 3 )  { digitalWrite(ledPin3, HIGH); door3closed = false; }
    else if ( ledChange == 4 )  { digitalWrite(ledPin1,  LOW); door1closed = true;  }
    else if ( ledChange == 5 )  { digitalWrite(ledPin2,  LOW); door2closed = true;  }
    else if ( ledChange == 6 )  { digitalWrite(ledPin3,  LOW); door3closed = true;  }
 }
    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 ( door1closed == false ) { WiServer.println_P(wbDoor1Open);  }
  if ( door1closed == true  ) { WiServer.println_P(wbDoor1Closed);}
  if ( door2closed == false ) { WiServer.println_P(wbDoor2Open);  }
  if ( door2closed == true  ) { WiServer.println_P(wbDoor2Closed);}
  if ( door3closed == false ) { WiServer.println_P(wbDoor3Open);  }
  if ( door3closed == true  ) { 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);
}

Logged

Pages: 1 [2]   Go Up
Jump to: