m2snd
October 2, 2011, 7:05pm
1
I am trying to create a scketch to be able to remotely control a window blind.
I wanted that, after making the pin 7 HIGH , change itself to LOW after 15 seconds.
Sorry my english
Thanks
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 66 };
byte gateway[] = { 192, 168, 1, 254 };
byte subnet[] = { 255, 255, 255, 0 };
byte state1 = 0;
long end1;
int val = 0;
Server server(80);
byte sampledata=50;
//luzes da casa
int luzCozinha = 7;
String readString = String(30);
boolean luzCozinhaON = false;
boolean estSalaUpON = false;
boolean estSalaDownON = false;
void setup(){
Ethernet.begin(mac, ip, subnet);
pinMode (luzCozinha,OUTPUT);
;
Serial.begin(9600);
}
//--------------------------inicio codigo
void loop(){
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 30)
{
readString.concat(c);
}
Serial.print(c);
if (c == '\n') {
//luz da cozinha
switch (state1) {
case 0:
if (readString.substring(6,11) == "L7=UP"){
digitalWrite (luzCozinha, HIGH);
end1 = millis () + 15000;
state1=1;
}
break;
case 1:
if (end1 <= millis ()) {
digitalWrite (luzCozinha, LOW);
state1=0;
}
break;
}
//-----------------------------------------------------------------------------------------------
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<h1><center>CASA</center></h1>");
client.println("<font color='black'>");
client.println("
");
//Output luz da cozinha
client.println("<form method=get name=LED><input type=submit name=L7 value=UP> Persiana<form>");
client.println("<form method=get name= LED><input type=submit name=L7 value=DOWN ><form>");
if (luzCozinhaON)
{
client.print("<font color='green' size='3'> UP");
}
else
{
client.print("<font color='red' size = '3'> DOWN");
}
client.println("<font color='black'
");
//procedimento final
client.println("</body></html>");
readString="";
client.stop();
}
}
}
}
}
system
October 2, 2011, 7:24pm
2
Switch statements are appropriate when there are more than 2 states. They are less appropriate when there are only two states.
end1 = millis () + 15000;
Adding to an unsigned int is not a good idea. The start time should be subtracted from the current time, and compared to the desired interval, instead.
if(millis() - start1 > 15000)
You should separate your decision making from your action based on that decision. When the client says to open the blinds, make note of that fact.
Regardless of whether the client is still connected 15 seconds later, or not, you have to stop the action, if it was started.
m2snd
October 2, 2011, 9:18pm
3
Thanks for you replay!
I tried with if and else, but can not get there, Can you give me an example of code please ...
I tried with if and else, but can not get there,
So post what you tried, what happened, what you want to happen and we will tell you where you went wrong.
m2snd
October 3, 2011, 10:49am
5
I tried this:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 66 };
byte gateway[] = { 192, 168, 1, 254 };
byte subnet[] = { 255, 255, 255, 0 };
Server server(80);
byte sampledata=50;
//luzes da casa
int luzCozinha = 7;
long previousMillis = 0;
long interval = 15000;
String readString = String(30);
boolean luzCozinhaON = false;
void setup(){
Ethernet.begin(mac, ip, subnet);
pinMode (luzCozinha,OUTPUT);
Serial.begin(9600);
}
//--------------------------inicio codigo
void loop(){
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 30)
{
readString.concat(c);
}
Serial.print(c);
if (c == '\n') {
//luz da cozinha
unsigned long currentMillis = millis();
if (readString.substring(6,11) == "L7=UP"){
if (currentMillis - previousMillis > 15000)
previousMillis = currentMillis;
digitalWrite (luzCozinha, HIGH);
luzCozinhaON = true;
}
else
digitalWrite(luzCozinha,LOW);
luzCozinhaON = false;
}
//-----------------------------------------------------------------------------------------------
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<h1><center>CASA</center></h1>");
client.println("<font color='black'>");
client.println("
");
//Output luz da cozinha
client.println("<form method=get name=LED><input type=submit name=L7 value=UP> Persiana<form>");
client.println("<form method=get name= LED><input type=submit name=L7 value=DW ><form>");
if (luzCozinhaON)
{
client.print("<font color='green' size='3'> UP");
}
else
{
client.print("<font color='red' size = '3'> DW");
}
client.println("<font color='black'
");
//procedimento final
client.println("</body></html>");
readString="";
client.stop();
}
}
}
}
system
October 3, 2011, 11:07am
6
Put each { and } on its own line. Use Tools + Autoformat to format your code, then post it again. It is nearly impossible to read that poorly indented (and possibly incorrectly nested) code.
m2snd
October 3, 2011, 11:23am
7
I´m sorry,
I did not know that tool
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 1, 66 };
byte gateway[] = {
192, 168, 1, 254 };
byte subnet[] = {
255, 255, 255, 0 };
Server server(80);
byte sampledata=50;
//luzes da casa
int luzCozinha = 7;
long previousMillis = 0;
long interval = 15000;
String readString = String(30);
boolean luzCozinhaON = false;
void setup()
{
Ethernet.begin(mac, ip, subnet);
pinMode (luzCozinha,OUTPUT);
Serial.begin(9600);
}
//--------------------------inicio codigo
void loop(){
Client client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 30)
{
readString.concat(c);
}
Serial.print(c);
if (c == '\n')
{
//luz da cozinha
unsigned long currentMillis = millis();
if (readString.substring(6,11) == "L7=UP")
{
if (currentMillis - previousMillis > 15000)
previousMillis = currentMillis;
digitalWrite (luzCozinha, HIGH);
luzCozinhaON = true;
}
else
digitalWrite(luzCozinha,LOW);
luzCozinhaON = false;
}
//-----------------------------------------------------------------------------------------------
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<h1><center>CASA</center></h1>");
client.println("<font color='black'>");
client.println("
");
//Output luz da cozinha
client.println("<form method=get name=LED><input type=submit name=L7 value=UP> Persiana<form>");
client.println("<form method=get name= LED><input type=submit name=L7 value=DW ><form>");
if (luzCozinhaON)
{
client.print("<font color='green' size='3'> UP");
}
else
{
client.print("<font color='red' size = '3'> DW");
}
client.println("<font color='black'
");
//procedimento final
client.println("</body></html>");
readString="";
client.stop();
}
}
}
}
system
October 3, 2011, 11:53am
8
Whenever you receive "L7=UP" in the request, you turn the luzCozinha pin on, set luzCozinhaON to true, and if it has been more than 15 seconds since you last set previousMillis, you set it again.
There is nothing magic about setting previousMillis. At some point, you need to write the code that compares previousMillis to "now" and turn the luzCozinha pin off.
Where is that code?
m2snd
October 3, 2011, 12:25pm
9
Sorry to be so boring, but I'm very newbie to arduino ....
like that?:
..... else
if (previousMillis > currentMillis)
{
digitalWrite(luzCozinha,LOW);
luzCozinhaON = false
};
.....
system
October 3, 2011, 12:33pm
10
What is the purpose of testing how long it has been since previousMillis was set?
You want to set some variable (previousMillis is marginal; the name should be something like pinOnTime) when the pin is turned on.
Then, after the block that turns the pin on (the if(client) {} block), have an if block that turns the pin off.
if(luzCozinhaON && millis() - previousMillis > 15000)
{
// Turn the pin off and set luzCozinhaON to false
}
m2snd
October 3, 2011, 1:16pm
11
the purpose is for, when I load up or down on the client, the blind window needs 15 seconds (HIGH) to totally open or close. After 15 seconds, the relay must turn off (LOW) , to disconnect the power.
system
October 3, 2011, 1:48pm
12
the purpose is for, when I load up or down on the client, the blind window needs 15 seconds (HIGH) to totally open or close.
You need to record when the pin goes HIGH, every time the pin goes HIGH, not just if the pin has not been HIGH for 15 seconds.
m2snd
October 5, 2011, 2:45pm
13
Ok !!!
Finally found and solve de problem!!!
The problem was that the browser did not do the refresh to change de LED status...Solved with:
client.println("<META HTTP-EQUIV=REFRESH CONTENT=1;url=http://192.168.1.66/>");
Thanks guys!!!