Hello all,
I am working on my home automatic system, Arduino Mega 2560 with a Arduino Net Shield and I need some assistance on getting a function to loop without the need to use the dreaded "delay()". I can't get the function to loop and just work correctly inside the if(readString.indexOf("toggle60min") > -1). Been at this for several hours, tried for loops, return, continue, while etc. Remember, this should only function when I click on the timer (60 minutes). Any suggestions?
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0x90 };
byte ip[] = { 192, 168, 1, 134 }; // MY IP change to your needs
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
EthernetClient client1;
String readString = String(100);
unsigned long currentTime;
unsigned long loopTime;
void setup(){
Serial.begin(9600);
pinMode(2, OUTPUT); //AirCon relay
Ethernet.begin(mac, ip);
} //setup
void loop(){
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 100)
{
readString = readString + c;
}
if (c == '\n')
{
//code for pin 2
if(readString.indexOf("toggle2") > -1)
{
toggle(2);
delay(100);
//emailtxt();
}
if(readString.indexOf("get2") > -1){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
if(digitalRead(2) == 0){
client.print("<IMG SRC=images/aircon_off.png>");}
else if(digitalRead(2) == 1){
client.print("<IMG SRC=images/aircon_on.png>");}
}
if(readString.indexOf("toggle60min") > -1)
{
currentTime = millis();
loopTime = currentTime + 5000;
if(millis()%1000 != 0)
{
if(currentTime == loopTime){
toggle(2);
return;
} //if
}// if
} // read
if(readString.indexOf("get60min") > -1){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
if(digitalRead(2) == LOW){ // not important yet
client.print("<IMG SRC=images/timer_off.png>");}
else if(digitalRead(2) == HIGH){
client.print("<IMG SRC=images/timer_on.png>");}
}
readString="";
client.stop();
} // if c== '\n'
} // if client
} // while
} // if connected
// int toggletimer(){
// } //toggletimer
} // void loop
int toggle(int pinnumber){
if(digitalRead(pinnumber) == HIGH){
Serial.print("toggle functions says ");
Serial.print(pinnumber);
Serial.print(" is on");
Serial.println();
digitalWrite(pinnumber, LOW);
return(0);
} //if
if(digitalRead(pinnumber) == LOW){
Serial.print("toggle functions says ");
Serial.print(pinnumber);
Serial.print(" is off");
Serial.println();
digitalWrite(pinnumber, HIGH);
return(0);
} //if
} //toggle
This code works perfect for my Garage's Arduino Uno, but I can't do anything when it's running the "While loop"
if(readString.indexOf("toggle6") > -1)
{
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
Starttimer = millis();
Currenttimer = Starttimer + 15000;
while ( millis() <= Currenttimer) {
if (millis() == Currenttimer){
Serial.println("email sending.");
emailtxt();
Serial.println("email sent.");
} //if
} //while
} // toggle6