Analog reading timer, I think!

Hi,

This is my first post and I am relatively new to Arduino and all of its magic!

I am working on a project with an accelerometer, which if it detects a tilt for more than 60 seconds then it will send a text message. Basically, if something has fallen over I want it to text me.

The complication I have is that this thing sways, so the timer needs to start when it has reached a certain value, but reset if it goes back to normal.

Is this possible? This is the code I have so far, with no timer attached as of yet.

#include <SerialGSM.h>
#include <SoftwareSerial.h>



SerialGSM cell(2,3);


void setup() {



 Serial.begin(9600); 
 
cell.begin(9600);
  cell.Verbose(true);
  cell.Boot(); 
  cell.FwdSMS2Serial();
  cell.Rcpt("************");
  cell.Message("Preset message to be sent.");

}

void loop() {
  // put your main code here, to run repeatedly:

 int zPin = 7;
  int rightPin = 8;
  int leftPin = 9;
  int forwardPin = 10;
  int backwardPin = 11;
  int x, y, z;     
  
  
  
  x = analogRead(A0);           // Read the X axis reading
Serial.print("X:");
Serial.print(x);
Serial.print(" ");
y = analogRead(A1);           // Read the Y axis reading
Serial.print("Y:");
Serial.print(y);
Serial.print(" ");
 z = analogRead(A0);           // Read the Z axis reading
Serial.print("Z:");
Serial.print(z);
Serial.print(" ");
  


   
  if (x < 240)
digitalWrite(leftPin, HIGH);

if (x < 240)
cell.SendSMS();



 if (x > 241)
digitalWrite(leftPin, LOW);




if (x > 440)
digitalWrite(rightPin, HIGH);



 if (x < 439)
digitalWrite(rightPin, LOW);



  
 if (y < 270)
digitalWrite(backwardPin, HIGH);



 if (y > 271)
digitalWrite(backwardPin, LOW);
 



if (y > 470)
digitalWrite(forwardPin, HIGH);



 if (y < 469)
digitalWrite(forwardPin, LOW);




if (z > 450)
digitalWrite(zPin, HIGH);


 if (z < 400)
digitalWrite(zPin, LOW);







  if (cell.ReceiveSMS()){
    Serial.println(cell.Message());
    cell.DeleteAllSMS();
  }


}

Just to iterate, I need it to text only if it has been on it’s side for 60 seconds at one time, not built up.

Any help would be fantastic, I’ve been trying for a week or so now to get some sort of timer working.

Thanks,

Andrew

Remember the time when it is first tilted. Later see if it is still tilted and if 60 seconds has elapsed.

Something like:

unsigned long tiltedTime;
...
if (onItsSide)
  {
  if (tiltedTime == 0)   // just tilted?
    tiltedTime = millis ();
  else
    {
    if (millis () - tiltedTime >= 60000)
       { 
       // send a message
       tiltedTime = 0;
       }
    }
else
  tiltedTime = 0;   // not tilted
  if (x < 240)
digitalWrite(leftPin, HIGH);

if (x < 240)
cell.SendSMS();

Why can’t you do both of these with one if statement?

Your indenting is horrid. Given that there is a menu item on the Tools menu to fix it, there really is no excuse for posting such crappy looking code.

Most of your if statements look like they should be else if statements, instead. If is unlikely that x will be less than 240 and greater than 440 on any given pass through loop(). So there is no reason to test for x greater when 440 if you’ve already determined that it is less then 240 (twice).

Is this possible?

Of course. Maybe read this: http://www.gammon.com.au/switches

[quote author=Nick Gammon link=msg=2166437 date=1427836863] Remember the time when it is first tilted. Later see if it is still tilted and if 60 seconds has elapsed.

[/quote]

I don't think that's quite the same. That would false trigger if the two tilts happen to be a minute apart by coincidence. I think he wants "no untilt for at least 60 seconds". For that you have to keep testing. Your code does that, but you didn't express it that way here.

"still tilted" expresses that idea.

If I had said "also tilted when you check again" it would be different.

Perhaps I should have said "remains tilted".

[quote author=Nick Gammon link=msg=2166531 date=1427841876] "still tilted" expresses that idea.

If I had said "also tilted when you check again" it would be different.

Perhaps I should have said "remains tilted". [/quote]

In fact, I think what caught me was the "later see". But if there's no harm, there's no foul.

Thanks everybody for your inputs. I have only been playing with Arduino coding (or any sort of coding for that matter) for 2 weeks so it's all new and confusing to me!

Nick, the code you provided looks promising, I haven't a clue how to put it in my sketch but I shall be working on that today.

Paul, after the telling off I've now discovered the auto format tool ;) bit harsh, but hey.

I have tried to include the 2 outputs from 1 if statement, but the text message repeats itself over and over rather than just numbers from the accelerometer, slowing everything down. Now I'm sure there must be ways around it and that my code is probably wrong, but this is incredibly difficult for me to work out!

Thanks again guys

To generalize a bit, there are two aspects of programming - tools and design. Tools are the languages, appropriate methods and so on. Design is often underestimated by beginners. It's defining the problem and/or task, devising a strategy to solve/accomplish it, and then finally translating it into code. Too often this sequence is followed backwards, starting with gung-ho coding marathons. As if the solution will somehow evolve from the implementation. A teacher of mine called this, "ready, fire, aim".

Learn to design, learn to research, and you will learn 10 times faster how to create effective, easy to read, and easy to maintain code. Also solve problems that you thought were almost impossible.

That makes a lot of sense! I think I probably need to spend a bit more time on the design side then.