[Tutor] Seismometer alarm

Alan Gauld alan.gauld at btinternet.com
Sun Jan 4 16:55:53 CET 2015

On 04/01/15 13:17, Ted wrote:
> On 03/01/15 19:19, Ted wrote:
>> Alan Thank you so much for the reply,

No probs, but a few comments further to Dave A's reply.

Some folks pay by the byte so please don;t attach large files such as 
screenshots. Post a link to a web site if you really need to show us an 

Also many/most folks on the list are using plain text so they
can't see colours or bold etc. So you really need to
1) send your messages in plain text if at all possible
2) keep your comments on separate lines.
3) If possible turn on quoting so we can see what is new
    and what is the previous message.

Most mail tools support these features if you look in the options.

>>         myData = int (arduinoSerialData.readline())
>>        if myData >33500:
>>             print(arduinoSerialData.readline())   Here I would like to see it printed, AS WELL AS, doing the following.
> Note this is printing the next thing from Arduino but not storing it
> anywhere.
> You are throwing it away...

HERE IS MY MAIN GOAL,  Here I do want to go to the next step..and not 
throw the numbers away.

So you need to store the value in a variable and
then print the variable.

> 1. These numbers come it very fast 18 per second?

To a computer that's very slow.
1800 per second might be a problem.
18000 per second definitely would.

> 2. Perhaps I need to say, save these numbers for 30 seconds, and give me one average number every 30 seconds.

They are slow enough that you could update the average
on every reading if you wish. You could also store them
to a file for later analysis, in a spreadsheet, say...

> If that average number, increases or decrease by 10% go to the next step.  if not do nothing.

OK, so you now need another variable to hold the previous average.
Something like

current_data = read arduino
old_average = new_average
new_average = calc_new_Ave(current_data)

if (new_average > old_average * UPPER) or   # upper = 1.12 for +12%
     (new_average < old_average * LOWER):    # lower = 0.88 for -12%
     # do something

How you calculate the averages depends on how you store the readings...

> 4. If this average number stays above/below this 10% for 30 seconds, trigger the alarm.

Now you have introduced a timer.
You can create variables to hold the current time
and the change time:

current_time = time.now()
if changed_time < current_time - TIMEDELTA  # eg 30 seconds

> 5. Hopefully both the IF’S   (10%), and (30) seconds, would be changeable,
 > as I don’t know for sure these conditions.

Make them variables, define them near the top of your code for easy 
access. Make the letters uppercase to indicate they are constants.

TIMEDELTA = 30   # seconds
VARIANCE  = 0.1     # percent as a decimal

>>             time.sleep(1) #Slows to 1000ms

You could create a delay constant too

DELAY = 1  # second

to produce


As a future enhancement you could put the constant values in
a file and read that file at startup. That way even
non-programmers can control the program without messing
with the source code.

> Please let me know if, I need to change my Q&A’s style.

See comments above and from Dave

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list