Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Re: Delayed check on digital input change - not working on: January 30, 2014, 01:26:07 pm
Ah! Perfect. I added some brackets (quite possibly more than required) and now it's working exactly as I expected:

Code:
if(DryerInChange == 1 && ((CurrentMillis - ChangeMillis) > DryerCheckDelay)) {

Thank you PaulS!   smiley-razz
2  Using Arduino / Programming Questions / Delayed check on digital input change - not working on: January 30, 2014, 12:07:27 pm
Hello  smiley-grin

I'm having trouble understanding how to program a delayed "double check" on a digital input.  I want to sense a digital input change and then check it again 5s later to make sure it is a permanent change.

I have pasted the relevant code from my script below.  The loop routine checks to see if a digital pin, DryerPin, has changed state.  If it has, a timer is started (DryerCheckDelay, currently set to 5000ms).

After that time expires the code should check that the DryerPin is STILL in the new state - if so, it calls a routine SendData(StrToSend).  The SendData routine works well.  If DryerPin is not in the new state DryerInChange is reset to 0 and we start again.

The code in my loop routine doesn't work properly.  It detects the change of state but doesn't wait the 5000ms; instead it reports the change immediately.

I am probably misunderstanding something.  I would be grateful if someone could point out what.   smiley

Code:
#include <SPI.h>
#include <Ethernet.h>


// Set up pins
int DryerPin = 30; // Digital in for dryer on/off digital sensor


// Set up dryer sensor
int DryerState = 0; // dryer off initially
int OldDryerState = 0; // Previous dryer state

int DryerCheckDelay = 5000; // millis to wait between checks to see if dryer is still at new state
unsigned long ChangeMillis = 0;
unsigned long CurrentMillis = 0;
int DryerInChange = 0; // Is the dryer in a changed state?

char StrToSend[20];

void setup()  {

pinMode(DryerPin, INPUT);           // set pin to input
digitalWrite(DryerPin, HIGH);       // turn on pullup resistors

}


void loop()
{

DryerState = digitalRead(DryerPin);
        CurrentMillis = millis();
// Serial.println(DryerState);
if (DryerState != OldDryerState && DryerInChange == 0) { // Start timer before 2nd check
ChangeMillis = CurrentMillis;
                DryerInChange = 1;
}
       
        if(DryerInChange == 1 && CurrentMillis - ChangeMillis > DryerCheckDelay) {
          // If dryer state changing and we have waited long enough
          if (DryerState != OldDryerState) { // Still changed state - send the change of state now
                DryerInChange = 0;
                sprintf(StrToSend, "d=%d", DryerState);
SendData(StrToSend);
OldDryerState = DryerState;
          }
          else
          {
            DryerInChange = 0; // Not permanent change, so stand down
          }
}
}



void SendData (char *ToSend) {


// This bit works fine  :-)


// Idea from http://arduino.cc/en/Tutorial/WebClient
if (client.connect()) {
Serial.println("Connected");
// Make a HTTP request:
client.print("GET /~maidhous/dev/input/in.php?");
client.print(ToSend);
Serial.println(ToSend);
client.print("&p=");
client.print(DataWritePassword); // Password
client.println(" HTTP/1.0");
client.println();
}

// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
//Serial.println("Server responded");
//char c = client.read();
//Serial.print(c);
}

// if the server's disconnected, stop the client:
//if (!client.connected()) {
//Serial.println();
//Serial.println("Disconnecting.");
delay(30);
client.stop();
}
3  Topics / Home Automation and Networked Objects / Re: attiny85 radiator booster / fan controller on: November 06, 2013, 07:49:32 am
Nice work  smiley

I was interested in a similar commercial product the other day:
http://www.radfan.com/
4  Topics / Home Automation and Networked Objects / Re: arduino life on: November 06, 2013, 07:17:46 am
Uno will not accept 24V input, at least not for long

As Nick_Pyner pointed out, the OP did not mention 24V, he said 24 hours. But an interesting discussion none the less.

My MEGA 2560 has been working continuously with a mains voltage adaptor without any problems for a couple of years (barring my own coding errors that is!)
5  Community / Website and Forum / Re: Double slash // on: November 05, 2013, 05:01:51 am
Fixed.

Lovely, thanks MadBob. All looking good to me, email notifications and search results looking good.   smiley-grin
6  Community / Website and Forum / Double slash // on: November 03, 2013, 05:06:25 pm
Somewhere in its code the forum is inserting a double slash in the URL.

Try a forum search (for "hello" - whatever really) and click on one of the search results. You'll get something like:
http://forum.arduino.cc//index.php?topic=197001.0

Now, Chrome (and I suspect many other desktop browsers) forgives the // error and displays the page anyway. But some systems are not so forgiving.

For example, see my screenshot of an emailed new topic notification as it appears on my Android mobile. It links only to the root, stopping at the incorrect //.

Can the erroneous duplication be found and weeded out?   smiley
7  Using Arduino / Programming Questions / Passing a string to a (void) function on: December 17, 2012, 12:48:47 pm
So I am having trouble getting my head round the fact you can't pass a string to a function.  And despite reading many posts about it I still don't understand how to do it.   smiley-cry

The goal is to have a SendData function which accepts a string like e=935 and calls a PHP page to save the data in a MySQL database (the PHP bit is working fine).

The script is a hacking together of various ideas...   smiley-eek  The code runs on my Arduino Mega 2560 with official Ethernet sheild.

At the moment the code:

- Reads current meter readings which are generated every 6 seconds by my CurrentCost connected to RX1 and tries to report that with the SendData function
- Checks if the state of digital input DryerPin has changed and if so tries to report that with the SendData function

Here are the relevant (I think) lines of code and I attach the full script so you can see the context.


Code:
char StrToSend[20];


Code:
strcpy (StrToSend, "e=" + power);
SendData(StrToSend);


Code:
void SendData (char *ToSend) {

// Idea from http://arduino.cc/en/Tutorial/WebClient
if (client.connect()) {
Serial.println("Connected");
// Make a HTTP request:
client.print("GET /~maidhous/dev/input/in.php?");
client.print(ToSend);
Serial.println(ToSend);
client.print("&p=");
client.print(DataWritePassword); // Password
client.println(" HTTP/1.0");
client.println();
}

// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
//Serial.println("Server responded");
//char c = client.read();
//Serial.print(c);
}

// if the server's disconnected, stop the client:
//if (!client.connected()) {
//Serial.println();
//Serial.println("Disconnecting.");
delay(30);
client.stop();
}



In my SendData function the Serial.println(ToSend); line prints just "1".  I am expecting it to print something like "e=935".

How can I call my SendData function with different instructions each time, or is there a better way entirely?

Thank you smiley

Full script:

Code:
/*

Home Automation script 03

CurrentCost power logging inspiration:
http://web.archive.org/web/20100724220142/http://e.inste.in/2008/06/16/interfacing-the-currentcost-meter-to-an-arduino/
*/

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x7A, 0x56 };
byte ip[] = { 192,168,1,177 };
byte gateway[] = { 192, 168, 1, 254 };
byte server[] = { 1,1,1,1 }; // IP to call to save data
String DataWritePassword = "xxx";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client(server, 80);

// Local variables
// char wattsStr[5];
// int watts = 0;
// int previousWatts = 0;

// Set up pins
int sensorPin = A0;    // select the input pin for the outside light sensor
int ledPin = 8;      // select the pin for the tell-tale LED
int sensorValue = 0;  // variable to store the value coming from the outside light sensor
int DryerPin = 30; // Digital in for dryer on/off digital sensor

// Set up CurrentCost variables
char startPattern[] = "<ch1><watts>"; // The start of the pattern to look for
char endPattern[] = "<"; // The end of the pattern to look for
int pos = 0; // Position of search
byte readByte = 0xFF; // Initial setting for CurrentCost serial in
int state = 0; // Tracks state of serial in from CurrentCost
int power = 0;

int FlashTimes; // For flashing the LED

// Set up dryer sensor
int DryerState = 0; // dryer off initially
int OldDryerState = 0; // Previous dryer state

char StrToSend[20];

void setup()  {
// Define pin modes for tx and rx pins:
//  pinMode(rxPin, INPUT);
//  pinMode(txPin, OUTPUT);
// Set the data rate for the SoftwareSerial port
//  softSerial.begin(9650);

// Set the data rate for the hardware serial port
Serial.begin(9600); // Diagnostic serial port
Serial1.begin(57600); // Pin 19 = RX1 on the Arduino Mega 2560 connects to CurrentCost

// start the Ethernet connection:
Ethernet.begin(mac, ip, gateway);

// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);

pinMode(DryerPin, INPUT);           // set pin to input
digitalWrite(DryerPin, HIGH);       // turn on pullup resistors

// give the Ethernet shield a second to initialize:
delay(1000);
}

void loop()
{
readByte = Serial1.read();
if (readByte == 0xFF) {
//digitalWrite(ledPin, LOW);
}
else
{
//Serial.print(readByte, BYTE);
gotByte();
//digitalWrite(ledPin, HIGH);
}

DryerState = digitalRead(DryerPin);
// Serial.println(DryerState);
if (DryerState != OldDryerState) { // Send the change of state
strcpy (StrToSend, "d=" + DryerState);
SendData(StrToSend);
OldDryerState = DryerState;
}
}

void gotByte() { // Interpret the serial data coming in from the CurrentCost
if (state == 0) { // Check for starting pattern in the data
if (readByte == startPattern[pos]) {
++pos;
if (startPattern[pos] == 0) { // Finished matching start pattern
++state;
power = 0;
}
}
else
{
pos = 0;
}
}
else if (state == 1) { // Start reading power
if (readByte == endPattern[0]) {
int i = 0;
// finished reading power
// light up lights

// while (wattLevels[i] < power) {
//digitalWrite(wattPins[i], HIGH);
//++i;
//}

// and clear remaining
//while (i < 5) {
//digitalWrite(wattPins[i], LOW);
//++i;
//}

state = 0;
Serial.println(power);

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

// Call SendData
strcpy (StrToSend, "e=" + power);
SendData(StrToSend);
     
// Flash the LED once for each 500W
// See this for the 0.5 bit: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260132933
FlashTimes = (int) ((power / 500) + 0.5);
Serial.println(FlashTimes);
if (FlashTimes == 0) { // Make it flash at least once
FlashTimes = 1;
}
for (int x = 0; x <= FlashTimes; x++) { // Changed < to <=
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}



}
else
{
// read another digit
power = power * 10 + readByte - '0';
}
}

}

void SendData (char *ToSend) {

// Idea from http://arduino.cc/en/Tutorial/WebClient
if (client.connect()) {
Serial.println("Connected");
// Make a HTTP request:
client.print("GET /~maidhous/dev/input/in.php?");
client.print(ToSend);
Serial.println(ToSend);
client.print("&p=");
client.print(DataWritePassword); // Password
client.println(" HTTP/1.0");
client.println();
}

// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
//Serial.println("Server responded");
//char c = client.read();
//Serial.print(c);
}

// if the server's disconnected, stop the client:
//if (!client.connected()) {
//Serial.println();
//Serial.println("Disconnecting.");
delay(30);
client.stop();
}
8  Topics / Home Automation and Networked Objects / Re: Arduino Controller android app on: December 04, 2012, 08:08:52 am
@marque: Great work!   smiley-wink

Any idea why my other pins are high? I can pop a led into any of my unused pins and the led lights up?

Is it likely to be this reason?
http://www.arduino.cc/en/Reference/digitalWrite
Quote
The pullup resistor is enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely cause.
9  Using Arduino / Project Guidance / Re: Automated lighting on: January 13, 2012, 04:35:33 pm
I read this and remembered this Dilbert comic strip from years ago:
http://dilbert.com/strips/comic/1995-02-22/
Personally I think my wife would be a bit upset if I installed something like this.  "Hey, the lounge lights keep going off when I am watching the TV." "Yes dear, you just have to move around a bit."

smiley
10  Using Arduino / Project Guidance / Re: Google Map display in living room on: January 12, 2012, 07:38:28 am
That was my understanding. According to the site, the Pis are being baked...

Yep, in the Far East which is presumably why they can sell them at that price ("So we have had to make the pragmatic decision and look to Taiwan and China for our manufacturing, at least for this first batch.")

A Raspberry Pi + a Gertboard to give digital IO looks to be an exciting combination and one which will surely compete with Arduino?
11  Using Arduino / Project Guidance / Re: Help!! building a Copy Cat light display on: January 06, 2012, 11:32:24 am
summarized... it doesn't work.

So.......what happens when you load and run your script?

I would throw in a few diagnostic statements which print to serial port.  It will probably then become obvious what's wrong.  Your code looked OK to me, but then I'm no expert.

BTW format posted code by highlighting it and pressing the # button:

Code:
Like this
12  Topics / Home Automation and Networked Objects / Re: Mega cannot read serial output from Current Cost CC128 on: January 05, 2012, 06:55:42 am
Does anyone have this working on software serial or will it just not go fast enough to do 57600?

I never got it working with software serial.  I believe you're right about the speed, but then I'm a relative noob Arduino-wise.

My Mega is still logging power using Serial1, it's on the desk in front of me now.  Still not been put in an enclosure.   smiley

Good luck.
13  Using Arduino / Project Guidance / Re: Digital 12 socket light dimmer. For christmas! on: December 17, 2011, 09:05:49 pm
This is awesome, great work with AppInventor!   smiley-grin
14  Topics / Home Automation and Networked Objects / Re: Home Heat Thermostat - 7day prog., IR control, Graphic LCD on: December 17, 2011, 06:35:07 pm
@supercrab if you can wait a few days, try ebay seller dailydealuk in China, bought 10 and they arrived the other day, work fine and a good price.

@gmcmicken thanks for posting and updating this project, your finished item looks great!
15  Using Arduino / Project Guidance / Re: Chicken Coop Automatic Door on: December 07, 2011, 11:42:26 am
I'm not sure if I should use an RTC with a calculation or an LDR?
........
Sunrise @23 june = 4:41 = 60 * 4 + 41 = 281
........

I suppose it depends on your chicken's bedtime routine  smiley  but if it were me I'd ditch the RTC and just go with a simple (and cheap) LDR.  As the other thread says, you could detect 15 minutes of light and call that sunrise and open the door.  Then detect 15 minutes of darkness and close the door.  This would allow the system to ignore quick dark storms, solar eclipses(!) etc. and the electronics and programming would be simpler.  The other thread shows some pics of the final installation (using RTC chip) - you would need half the components going the LDR route (no need for keypad or LCD).  And I don't think the chickens would care.   smiley-wink
Pages: [1] 2 3 4