Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - Nick_W

Pages: [1] 2 3 4
mc-Module / Question about Division
« on: February 10, 2017, 09:56:18 am »
I know that it was mentioned a while ago that division always returns a Float.

So I have been doing things like this:

Code: [Select]
Dim Value As Integer
..... stuff
Value = ((Value + List(currentsample)) / 2).ToInteger()

everywhere. Now I have to say this seems to be odd behavior, as most languages would simply do integer arithmetic in this case. If I were to do this:

Code: [Select]
Value = ((Value + List(currentsample)) / 2.0).ToInteger()

I would expect to have to cast to an integer, as 2.0 is explicitly a Float, and so Value etc. would usually be promoted to a Float.

This means that common constructs like:

Code: [Select]
Value /= 2

Don't work as Value is an Integer and the division returns a float (but Value *= 2, +=, -= etc do work - this seems inconsistent).

Looking at the documentation, it gives two division operators (never noticed that before!).

This is what the documentation says:

Shared Operator /(x As Integer, y As Integer) As Integer
Shared Operator \(x As Integer, y As Integer) As float

So according to the documentation, / should return an Integer, and \ should return a float. In fact, having tested this, it is the opposite way round.

So this does work:

Code: [Select]
Dim Value As Integer
..... stuff
Value = (Value + List(currentsample)) \ 2

And Value \= 2 works too!

I would really like to go through my code and remove all the unneeded type casting, but I'm not sure which is the right form. The documentation does not agree with the compiler.

So which is right? should I refactor my code, or will the compiler be fixed and I can just take out all the type casts as is (in the future)?


mc-Module / Arrays (Lists)
« on: February 09, 2017, 11:48:43 am »
I have 7 arrays (listofbytes), and I was looking to find a way of making them into a 2D array. ie a listoflistofbytes.

I was looking at listofobject as a way of adding a listofbyte to a listofobject, so that I have a listofobject with 7 entries, each it's own listofbyte.

Would this work? I don't want to go too far down this route if it's a dead end. It's confusing enough with all the listof things to deal with, without getting into lists of lists...

Another thought was to use a class as a container for lists.

What do you think is the best way to make a 2D array?

mc-Module / AnalogInput units
« on: February 06, 2017, 09:18:21 am »
I'm reading an analogInput pin, and I would like to convert the reading from mV to voltage independent units (ie 0-1023), as the battery voltage may vary.

I'm currently assuming that reading the Battery Voltage will give me the maximum value (1023 units) and using the following to convert:

Code: [Select]
((1023 * reading) / batvolt).ToInteger()

However it occurs to me that this cannot in fact be correct (or the battery voltage would always read maximum), so there must be an internal voltage reference.

The documentation does say this "When a pin is in “AnalogInput” mode, the system reads the value based on the internal band-gap reference value."

Reading the data sheet, the internal reference is 0.6V, so assuming a gain of 1/6 this would give a range of 0-3.6V (which seems to be the case).

This would then make my conversion:
Code: [Select]
((1023 * reading) / 3600).ToInteger()

Is this correct? am I making the correct assumptions here?


mc-Module / Anyone know how ExtractShort() works?
« on: January 31, 2017, 04:29:12 pm »
I have the following code:

Code: [Select]
    Dim count As Integer = 0
    While count < TotalBytes.Count
            //Dim val As Short = (TotalBytes.ExtractShort(count) & RESULT_MASK) >> 2
            Dim val As Short = ((TotalBytes(count) << 8 | TotalBytes(count + 1)) & RESULT_MASK) >> 2
            count += 2
        End While

Where TotalBytes is a ListofByte (and result is a ListofShort). The above code works and extracts the shorts correctly. If I use the commented out version (using ExtractShort), then it does not, the values returned are 1/2 the number of entries in TotalBytes (ie if TotalBytes is a list of 128 bytes, val is 64).

RESULTS_MASK just masks the top 4 bits. The values should be from 0 to 1023 (10 bits). As I say, my manual version works, ExtractShort does not.

Can anyone explain if I'm doing something wrong? I figure count is an offset into the ListofByte, but it doesn't seem to work.


mc-Module / ListofShort.Min() Not working
« on: January 31, 2017, 03:37:54 pm »
Just FYI,

I found that ListofShort.Min() returns the same value as ListofShort.Max()

I have a ListofShort called Values, and was publishing Values.Max - Values.Min to see what the swing was. I kept getting 0 as the value.

I wrote a workaround that calculates Values.Min, and now all is well (I'm assuming ListofShort.Max() works, it seems to return believable values).

Just add to the bug list.

Oh and "For a As Integer To b as Step 2" doesn't work either you always just get 1 step. I don't know if other step values work or not, but 2 doesn't.
It's the Step value that is the problem, without this (ie leave Step off), the default (Step 1) works (as long as you want Step 1).

mc-Module / Fast reading from sensors
« on: January 29, 2017, 06:33:11 pm »
I have been toying with adding an acoustic sensor to the 120 module.

This would require sampling audio data at something like 22khz (44 would be best). Should be no problem for am M4.

So I did some tests. Reading an analog input pin as fast as possible, results in 3.2ms per read. Doing the same with a digital input, gives the same value.

The spec for the chip says the ADC conversion is between 10 and 40 us, so that is not the bottleneck. I'm assuming the mcOs is limiting the data rate by time slicing or something.

Could this be improved by increasing priority of the event somehow? I was not running this in a shared event, but from a shared routine run by the boot() event.

Another thought was to add an external fast adc, using SPI to transfer data. I have one in mind that will transfer 12 bits of data at up to 1Msps.

As conversion is initiated by setting SS low, you have to read 16 bits at a time (then SS goes high, and the cycle continues). Trying this results in a data rate of 16 bits (short) every 1.7 ms.

None of this is fast enough to capture audio data.

If I read 1024 bytes at once via SPI, I get a data rate of 1 byte every 9us, which is fine, however I can only read 16 bits (2 bytes) at a time, and there is some sort of delay between successive SPI reads.

Again, I suspect mcOs of time slicing between various processes. I seem to need some way of making a process atomic, so that it doesn't get time sliced by the OS (obviously you couldn't do this for long), or a way of increasing the priority over some OS tasks. Something like nointerrupts, or a way of toggling SS between so many X bytes.

I'm not looking at speech, more like loud sounds, or specific frequencies to be detected, but I can't sample fast enough to do it. It seems a bit of an overkill to add another microcontroller to do this, and would probably suck battery.

The idea is to use a comparator to detect a large sound transient, this triggers an interrupt on the 120 module, that captures say 1 second of audio, analyzes it for specific frequencies, and triggers a message if it matches a profile.

I know I only have 20k or so of RAM to work with, but so far I can't even capture the data.

Any suggestions?

mc-Module / Convert String to Integer
« on: January 26, 2017, 03:03:52 pm »
I'm trying to convert a string to an Integer

Page 91 of the guide gives this function for object type String:

Code: [Select]
Public Function ToInteger(Hex As String) as Integer

But I can't get it to work ("Undefined Function"), and I don't know what Hex is supposed to be (some kind of formatting string I assume).

Im using

Code: [Select]
Integer.TryConvert(motionStopTime, stopInt)

Instead. Anyone know the syntax for converting a String to an Integer?


mc-Demo205 / RTC update via GPS
« on: January 24, 2017, 09:38:00 pm »
Does the RTC on the Demo 205 get updated after a GPS fix? if not, is there a way to manually do this? How would a device in the field get its RTC updated, especially when changing batteries?

Also, can the GNSS give altitude information? With a 4 satellite fix? Just curious.


mc-Module / New 120 Module/Demo 205 Accelerometer Library Released!
« on: January 22, 2017, 05:48:52 pm »
Check out my github

For many new programs, including:
All New LIS2DH12 library (for the 120 module and Demo 205 accelerometer)
Example programs for the accelerometer
Updated DoorSensor program for module 120
Updated Python logging program (makes viewing the accelerometer 120 example programs easy).

The new accelerometer library (V1.5) is completely re-written, and fully tested. Programs using the old incomplete library may need updating, as some function names have changed.

All bugs have been fixed* (and there were many).
All features of the accelerometer (except FIFO) have been implemented with easy to configure functions. These include.

  • Motion Detection
  • Transient Detection
  • Orientation Detection 6D and 4D
  • FreeFall Detection
  • Single and a Double Click Detection (tap)

All interrupts, modes, ranges and pins implemented.
6D, 4D (landscape/portrate) and high pass filter configurations made simple.
Readout of x,y,z pitch and roll in 8,10 and 12 bit resolution.
Sleep mode, data rates from 1 Hz to 1.5kHz
Ultra low power modes (2uA).
Easy read out functions return strings (such as "up" and "down" or "portrate_up" for Orientation detection)

Super easy to configure and use!

Plus much more.

Check it out and let me know what you think.

*This may not be true.

JSON / Duplicate keys
« on: January 21, 2017, 07:23:12 pm »
I'm just getting ready to release my new LIS2DH12 library (which actually works), and I'm writing some example programs.

One thing I ran into, was putting a line like this into the accelerometer ISR

Code: [Select]
JData.add("Orientation", Orientation)

Where jData is a json object, and Orientation is a string giving the Orientation "up" "down" etc. The json is published ever 10 seconds by a shared event.

The json object is a shared variable, and is cleared after it is published.

I thought that as the orientation was changed, the new orientation would overwrite the old, and when published you would have the orientation at the point it was published.

Instead, I get a run time error, saying that the publish string is too long. Looking at the culprit (jData) it has dozens of entries (I couldn't tell what the entries were, the Debugger shows (" for the content, but the Count() was 120 in one instance.

This seems to imply that duplicate pairs are allowed in a json object (and the standard does not disallow this).

Is this the case? Is this the intended operation of your implementation of the json object?

It's an easy fix (just clear before adding the new value), but I was wondering if this was intended or not, as most json implementations do not allow duplicate keys.

I also ran this through slowly, to see how duplicate keys deserialize to strings, and lo! No duplicate keys in the string. But the count says there is. Not sure exactly what is going on.

mc-Studio / SOLVED: Select Case not working with Byte variable
« on: January 18, 2017, 09:56:42 pm »
Just found a new bug (feature).

The following give a build error "object not set to an instance...", you know, the usual one.

Code: [Select]
Dim var1 as Byte = something()
Dim text as string
Select var1
  Case 0x01
  Case 0x02
End select 

Works OK with variable (expression I guess) as string, haven't tested it with other data types, but Byte does not work.

mc-Demo205 / Get GPS data into 12 bytes
« on: January 17, 2017, 12:25:46 am »
Does anyone know the accuracy of the GPS module? It's giving 6 decimal places, but it's hard to believe anything more than 4 decimal places are accurate.

Anyone have any code for shoehorning the gps lat and long into 24 or 32 bits, so they will fit in a SigFox payload?

If the floats are IEEE 754 format it should be easy enough to add them plus a timestamp into 12 bytes. Can you just add them to a ListOfBytes? I have tried the add and extract methods for ListofBytes without much luck. I think it's the extract methods that don't work.

Can anyone confirm these assumptions?


mc-Module / Time zones and RTC updates
« on: January 17, 2017, 12:20:47 am »
Does anyone know how time zones work? And how does the RTC on modules get updated? Is there a way to force an update?

I've been playing with my Time library, and find that quit often modules do not get their RTC updated, and so report 1999 or 2000. I assume the gateway is getting the time via NTP, and sends it to the modules. Is this just on boot, or does it periodically update? Can you get a module to request an update?

There is a time zone setting on the gateway config setup. How does this work? I have tried entering various values -4:00 for instance, but I don't know how this is passed to the modules, on boot? Plus it seems to make random changes to the module time (unrelated to the actual number). For instance entering -4:00 made 10 hours difference to the time, 0 made -4 hours difference to the time.

Z time is 1 hour off (DST I assume), so -4 hours should be eastern, but no, it's 10 hours off. Bit confusing how this works.

Anyone know?

mc-Module / Maximum Length of a String?
« on: January 16, 2017, 04:43:23 pm »
Is there a maximum length of a string?

I ask, because I have a json object, which I am publishing via MQTT, and I'm getting a run time error "arguments too long", which is where I convert the json to a string (prior to ListofByte).

Depending on how long the strings in the json are, I get the error, or not. The magic number seems to be around 280 characters.

I'm working around it by splitting the strings into two json objects, and publishing both of them (which works).

What are the limitations?


Pages: [1] 2 3 4