Stumped here, actually clueless. LOL

My code is listed in the next post under this subject. I was limited with space.

I am trying to make a controller for my pool. I want it to work as follows:

Turn pump on or off,
Turn light on or off
Turn heat on (only if pump is running)
Turn heat off either manually or automatically when pump is shut off.

Ambitious as this seems for a newbie, get a load of this:

I want to be able to control this from 3 momentary switches as well as via my home network.

I have figured out how to do each (either a manual control, OR network control. I just can seem to put them both together.

I was looking around on the internet for some code and found the sketch found below. With this also came the index.htm for the web server.

I set the whole thing up and found that with a relay shield, i can use LED1 for my pump, LED2 for my light and LED3 for my heater.

I made changes to index.htm so that all 3 are controlled with check boxes. ( i have had some experience with HTML)

I was able to modify the sketch (yellow highlights) to switch the heat off and to not allow the heat to start if there is no pump running.

Now what i am trying to pull off here is to make switch1 control the pump directly, switch 2 to control the light direct and switch3 to control the heat directly. I also want this to respond to the last command received. In other words if the pump is turned on with the switch, i want to be able to shut if off via the network.

I tried to go simple by adding the following to simply turn the pump on with a switch:

I added a switch int pumpSw = 31;

I set pin 31 to INPUT,

I then tried this code in the both loop and the setLEDS function.

if (pumpSw = HIGH)
{
LED_state[0] = 1; // save LED state
digitalWrite(6, HIGH);
}

This seems to ignore the "if" and just goes ahead and runs the code. I tried adding an "else" and nothing changes.
I am thinking this may have something to do with one of the includes disabling something but I'm not sure.

Im really quite stumped to say the least with this.

Any help?
I know this is going to be a long difficult thing to figure out and I am prepared to do what i have to. I just retired and need something to keep me busy.

Hi needahobby

if (pumpSw = HIGH)

C and C++ are very picky about the difference between "=" and "==", which are different operators :slight_smile:

EDIT Look here:

Regards

Ray

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60

// MAC address from Ethernet shield sticker under board
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 24); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[4] = {0}; // stores the states of the LEDs

//boolean pumpState;

int pumpSw = 31;

void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

Serial.begin(9600); // for debugging

// 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.htm file!”);
return; // can’t find index file
}
Serial.println(“SUCCESS - Found index.htm file.”);
// switches on pins 2, 3 and 5
pinMode(2, INPUT);
pinMode(3, INPUT);
//pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(31,INPUT);

// LEDs
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients

Serial.print(“IP Address is”);
Serial.println(Ethernet.localIP ());
Serial.println(digitalRead(2));
}

void loop()
{
EthernetClient client = server.available(); // try to get client

if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// limit the size of the stored received HTTP request
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
// remainder of header follows below, depending on if
// web page or XML page is requested
// Ajax request - send XML file
if (StrContains(HTTP_req, “ajax_inputs”)) {
// send rest of HTTP header
client.println(“Content-Type: text/xml”);
client.println(“Connection: keep-alive”);
client.println();
SetLEDs();
// send XML file containing input states
XML_response(client);
}
else { // web page request
// send rest of HTTP header
client.println(“Content-Type: text/html”);
client.println(“Connection: keep-alive”);
client.println();
// send web page
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();
}
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
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)

}

// checks if received HTTP request is switching on/off LEDs
// also saves the state of the LEDs

void SetLEDs(void)
{
// LED 1 (pin 6)
if (StrContains(HTTP_req, “LED1=1”)) {
LED_state[0] = 1; // save LED state
digitalWrite(6, HIGH);
}
else if (StrContains(HTTP_req, “LED1=0”)) {
LED_state[0] = 0; // save LED state
digitalWrite(6, LOW);
}
// LED 2 (pin 7)
if (StrContains(HTTP_req, “LED2=1”)) {
LED_state[1] = 1; // save LED state
digitalWrite(7, HIGH);
}
else if (StrContains(HTTP_req, “LED2=0”)) {
LED_state[1] = 0; // save LED state
digitalWrite(7, LOW);
}
// LED 3 (pin 8)
if (StrContains(HTTP_req, “LED3=1”) && (LED_state[0] == 1)) {
LED_state[2] = 1; // save LED state
digitalWrite(8, HIGH);
}
else if (StrContains(HTTP_req, “LED3=0”) || (LED_state[0]==0)) {
LED_state[2] = 0; // save LED state
digitalWrite(8, LOW);
}
// LED 4 (pin 9)
if (StrContains(HTTP_req, “LED4=1”)) {
LED_state[3] = 1; // save LED state
digitalWrite(9, HIGH);
}
else if (StrContains(HTTP_req, “LED4=0”)) {
LED_state[3] = 0; // save LED state
digitalWrite(9, LOW);
}
}

// send the XML file with analog values, switch status
// and LED status
void XML_response(EthernetClient cl)
{
int analog_val; // stores value read from analog inputs
int count; // used by ‘for’ loops
int sw_arr = {2, 3, 5}; // pins interfaced to switches

cl.print("<?xml version = \"1.0\" ?>");
cl.print("");
// read analog inputs
for (count = 2; count <= 5; count++) { // A2 to A5
analog_val = analogRead(count);
cl.print("");
cl.print(analog_val);
cl.println("");
}
// read switches
for (count = 0; count < 3; count++) {
cl.print("");
if (digitalRead(sw_arr[count])) {
cl.print(“ON”);
}
else {
cl.print(“OFF”);
}
cl.println("");
}
// checkbox LED states
// LED1
cl.print("");
if (LED_state[0]) {
cl.print(“checked”);
}
else {
cl.print(“unchecked”);
}
cl.println("");
// LED2
cl.print("");
if (LED_state[1]) {
cl.print(“checked”);
}
else {
cl.print(“unchecked”);
}
cl.println("");
// button LED states
// LED3
cl.print("");
if (LED_state[2]) {
cl.print(“checked”);
}
else {
cl.print(“unchecked”);
}
cl.println("");
// LED4
cl.print("");
if (LED_state[3]) {
cl.print(“on”);
}
else {
cl.print(“off”);
}
cl.println("");

cl.print("");
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str = 0;

  • }*
    }
    // searches for the string sfind in the string str
    // returns 1 if string found
    // returns 0 if string not found
    char StrContains(char *str, char *sfind)
    {

  • char found = 0;*

  • char index = 0;*

  • char len;*

  • len = strlen(str);*

  • if (strlen(sfind) > len) {*

  • return 0;*

  • }*

  • while (index < len) {*

  • if (str[index] == sfind[found]) {*

  • found++;*

  • if (strlen(sfind) == found) {*

  • return 1;*

  • }*

  • }*

  • else {*

  • found = 0;*

  • }*

  • index++;*

  • }*

  • return 0;*
    }

Read How to post code properly

Pete

If you post code, please use the code tags (the "#" button above the row of smileys).

This makes it much easier for people to read the code.

I didn't use the code tags because it would not allow me to highlight.

CORRECTION TO MY ORIGINAL POST

I ADDED THE FOLLOWING CODE

  if (pumpSw == HIGH) 
        {  
        LED_state[0] = 1;  // save LED state
        digitalWrite(6, HIGH);
        }

NOT

   if (pumpSw = HIGH) 
        {  
        LED_state[0] = 1;  // save LED state
        digitalWrite(6, HIGH);
        }

needahobby:
I didn't use the code tags because it would not allow me to highlight.

So it's better to force people to try and decipher forum mangled code instead of just putting a comment next to the line?

needahobby:
I didn’t use the code tags because it would not allow me to highlight.

CORRECTION TO MY ORIGINAL POST

I ADDED THE FOLLOWING CODE

  if (pumpSw == HIGH) 

{  
       LED_state[0] = 1;  // save LED state
       digitalWrite(6, HIGH);
       }




NOT



if (pumpSw = HIGH)
       {  
       LED_state[0] = 1;  // save LED state
       digitalWrite(6, HIGH);
       }

You are testing if the pin number (31) equals high, not the state of the input. Try digitalRead(pumpSw).

Which of these is easier to read?

Hey look here!

Hey look here!

thanks for the replay David,

I have tried and that does not seem to work either. It seems as if any sort of if, or if else that i try to add does not work.
As i said, I'm thinking it my have to do with something early in the sketch but i can't seem to find it.

if digitalRead(pumpSw == HIGH) 
        {  
        LED_state[0] = 1;  // save LED state
        digitalWrite(6, HIGH);
        }

Try ...

if (digitalRead(pumpSw) == HIGH)

I have tried adding (see code below) to the end of loop () as well as the the beginning of setLEDs (). The is no change. It just seems to ignore the if condition and executes the code.

I added Serial.print(pumpSw) to see if perhaps the issue was the wire or the bread board and that seems to be working fine.

if (digitalRead(pumpSw)==HIGH) {
LED_state[0] = 1;  // save LED state
digitalWrite(6, HIGH);}

it my have to do with something early in the sketch but i can't seem to find it.

Post the current version of the sketch and we'll take a look.

needahobby:
I added Serial.print(pumpSw) to see if perhaps the issue was the wire or the bread board and that seems to be working fine.

That should print the pin number of the pin you are using. In this case I think it is 13. If you want to print the state of the pin you have to call digitalRead.

Serial.print(digitalRead(pumpSw));

You are correct DeltaG, i did not type it correctly when i posted it.

Thanks though. Any help is appreciated.