Arduino Nano, interrupt and UART questions

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Arduino Nano, interrupt and UART questions

Post by MJU »

Hello,

I'm working on a project that uses a (Chinese), Arduino Nano.
This sends a serial datastream via a UART component, and expects a reply somewhat later.

I want to make sure that every time there's is data coming back, the UART catches it and stores it into a variable.

I got a nice reply from behind the scenes using an interrupt and a circular buffer.
But, the Nano only seems to have an interrupt on UART0 (?) and on both falling AND rising edges.
I can't select only falling edges for the RX pin (digital0 (PD0)) so it seems that the trick with the interrupt won't work?

What is the best way to capture the data that appears on the RX line of the UART
The trick I've got uses a header to search the circular buffer for the beginning of the data, but for this, the interrupt on UART0 must work.

So; what is the best tactic for me to make sure that data is captured on the RX of the UART and gets stored into a variable.
After that, I will compare the received data with data that is stored.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by chipfryer27 »

Hi

I don't think you will find much issue in using the RxInt feature with the Nano, but if your concern is that it could re-trigger whilst still in your Interrupt Handling Routine then it is easy enough to implement the following

IHR
Disable RxInt
Do whatever
Enable RxInt

Hope this helps

Regards

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

See
viewtopic.php?f=76&t=20668

For some examples - the RxInt works well (and if a 255 is 'received' in the interrupt routine - then ignore it when copying the data out of the circular buffer)

Martin

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

Ok thank you guys..!

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

Hey there,

I've been trying everything I can think of to get the thing working as I want it to work.

I've copied the circular buffer example from mnf in viewtopic.php?f=76&t=20668#p91988 but this doesn't work for me.

I don't get it. My guess is that there must be something different with the interrupts for the Arduino Nano?
I'm using the standard UART from the Arduino.

I send a string to a receiver and this sends the string unaltered back. This takes about 2 seconds. (confirmed by what I see on my logic analyser).
Meanwhile I want to make sure the Arduino listens to the RX and stores the string it receives in a variable, and compares it with the string it initially send.

I tried using UART receive string with several wait times. I saw that the data came onto the RX pins, but I never managed to store these RX data back in a variable.
Then I tried several attempts with a circular buffer, but all these were no success.

I'm getting a bit frustrated :oops:

Nobody can blame me to not try, I think I tried a million different manners, but no avail.

What do you think is the best tactic? Use UART receive string, or the circular buffer?

There are a few signs that the data is underway.
- First there is a IO pin that goes low 5ms before the UART will receive data.
- The time it takes to receive the data back is about 2 seconds.
- I send the data every 20 seconds, so I expect it back every 2 seconds later.
- The microcontroller doesn't have to do anything while waiting for the data to get back.

I just want this to work to check over a long period of the data I send, and gets send back is valid, if there are times the data is corrupted over time.
This to test the E32-868-T20D error rate.

I just need to get the Nano receive and compare the string it sends and receives..

Any help is welcome!

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

I used a Nano for my example program. It received from Bluetooth or Usb aok. Can you post your code, or part thereof

Martin

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

mnf wrote:I used a Nano for my example program. It received from Bluetooth or Usb aok. Can you post your code, or part thereof

Martin
Thanks Martin,

I've searched for a version that I thought worked, but didn't.
This one sends a text (sendtext) and listens in the macro "receive" if it comes back.
This needs to be compared and can only be different a few times.
TX test.fcfx
(26.76 KiB) Downloaded 250 times
I really hope I someone can help me with this one, I'm working on it far to long.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

Hi,

Just had a quick look and a few thoughts...

1) What are you trying to send the string to? (Another Arduino etc)
2) You are not using an interrupt for receive - it really does make the code / timing simpler.
3) How are you marking the end of the string for send and receive - you are attempting to receive a string of 40 chars but send text is only Testing 1.2.3
4) The Compare$ is wrong - syntax is Compare$(strA, strB, compare_mode) (not length) (and what is in the string after the Testing 1.2.3??)
5) The receive is only waiting 200 ms (0.2 seconds) for a reply once AuxPin is toggled - the receive loop only runs one on toggle - is your timing right?
6) What are you trying to achieve - send a 'I'm here' message and receive a 'Yes me too' reply ?
7) Can you send and receive to a terminal on your PC ??
8 ) I did another example (viewtopic.php?f=26&t=20701&p=92129&hilit=mood#p92129) that reads and parses strings from UART.
9) I'll try and do an example using interrupts (and terminal / 2 Nanos) this evening after work.

Martin

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

mnf wrote: Just had a quick look and a few thoughts...

1) What are you trying to send the string to? (Another Arduino etc)
I have made RF (LORA) Flowcode example that sends data from one node to another.
My goal is to put the receiver in different spots to receive and resend data it receives.
At the sender side I want to check if the data doesn't get corrupted while it gets the message back.
So in short: sender sends a string "Testing 12241744 and so on". The receiver receives that string and resends it back to the original sender.
This checks the string it receives from the "receiver" against the string it originally send and indicates after a few days (with a green or red LED), if there were errors in that period of testing.
After that I put the receiver in another place and the testing starts again.

mnf wrote: 2) You are not using an interrupt for receive - it really does make the code / timing simpler.
I know, I'm sorry for that, but the latest file is so chaotic that even I can't make anything of it anymore.
Sometimes I need to start again from a version I know that worked and throw ever after that attempt away.
mnf wrote: 3) How are you marking the end of the string for send and receive - you are attempting to receive a string of 40 chars but send text is only Testing 1.2.3
I think that in this way of testing, the length of the data isn't fixed.
In real life the sender will send a string of a length of 10bytes, but somewhat later it will send a string of 40bytes.
mnf wrote: 4) The Compare$ is wrong - syntax is Compare$(strA, strB, compare_mode) (not length) (and what is in the string after the Testing 1.2.3??)
This I didn't know! For someone that doesn't has English as it's own language, the Wiki can be difficult to read.
Thanks for pointing this to me.
mnf wrote: 5) The receive is only waiting 200 ms (0.2 seconds) for a reply once AuxPin is toggled - the receive loop only runs one on toggle - is your timing right?
I this example, the pin goes low because the receiver has something that it will send over the RX in 5ms after the pin goes low.
I can follow this on my logic analyser.
mnf wrote: 6) What are you trying to achieve - send a 'I'm here' message and receive a 'Yes me too' reply ?
No, same message has to be bounced back and received back. Just to test that even after days the same message is received.
mnf wrote: 7) Can you send and receive to a terminal on your PC ??
I didn't tried that because the sending and receiving works. I can monitor this via my logic analyser.
I see the string arrive at the sender module, the way the AUX pin reacts, the timing, the reception of the data comming back from the receiver and again the way the aux pin react.
I see that the same string is send back to the sender so this part works.
The only thing that doesn't work is the storage of the received string in a variable and the compare$.
mnf wrote: 8 ) I did another example (viewtopic.php?f=26&t=20701&p=92129&hilit=mood#p92129) that reads and parses strings from UART.
As usual, I'm going to check this out! Thank you.
mnf wrote: 9) I'll try and do an example using interrupts (and terminal / 2 Nanos) this evening after work.
Some people are far too good for this world.
I really appreciate this.

Sometimes my problem is that when I read certain things on the Wiki, and translate them in my own language, this data gets corrupted in my head.
I need a compare function in my brain also. :-)
Because of this, and because I drop in and out Flowcode regularly I get confused.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

Okay - not sure this will help as much as I'd hoped though...


So step 1:

I got two Arduinos chatting happily via UART (including 115200 - though wires are short!)

One runs a simple 'sender'
SenderUART.fcfx
(11.51 KiB) Downloaded 233 times
And one a simple receiver - I'm outputting the characters received to an I2C OLED:
ReceiverUART.fcfx
(12.7 KiB) Downloaded 234 times
Note that I have a delay because the i2c is slow... Note also that to reprogram the Nanos you must unplug the TX/RX connection (the USB uses this connection as well)

This just churns through characters 0..255 to UART (with very little refinement - trailing digits are not cleared for example - but enough to show comms working)

Step 2:

Receive a string.... This is relatively easy - if you know what the string terminator is ! Here is a very simple test program that reads a char(13) (this is sent by terminal program for the enter key) as the end of string. Again the string is sent to the OLED. The string I used is 20 bytes and I wrap on overflow. Again not very elegant (trailing chars from prev string not wiped on display)- but I wanted to get it to work first. It would also be easy to modify to read a fixed number of chars (left as an exercise :-) )
Step2.fcfx
(13.97 KiB) Downloaded 186 times
Can you get these to work - I'd try and get the basic comms working first and worry about errors later. I do have a LORA board waiting some time for a play - so I might be able to try with some more similar hardware to what you are using (although I have a Uno shield?)

- coincidentally I was trying at the weekend to get a Wemos Mega/ESP8266 to talk to it's ESP chip (via UART4) - I failed having screwed up the ESP8266 programming :-( Can now run microPython on the ESP8266 but can't reprogram it to AT command set. Also failed to get it to talk to the Arduino side of things.....

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

Another quick thought:

Do you have a common ground for Arduino and LORA board?

Martin

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

mnf wrote:
- coincidentally I was trying at the weekend to get a Wemos Mega/ESP8266 to talk to it's ESP chip (via UART4) - I failed having screwed up the ESP8266 programming :-( Can now run microPython on the ESP8266 but can't reprogram it to AT command set. Also failed to get it to talk to the Arduino side of things.....

Martin
Thank you Martin,

The first example I found already in another post? Can this be so?

But my problem is that I can't read code like this:

Code: Select all

.test[.pos] = .c
.pos = (.pos + 1) % 20

OR

.c != 255
Why do you use a dot before the variable name? I can't find anything about this notation on the Wiki.
What I do read in that code is that you set the character on position "pos" to the variable "c".
Then pos is incremented by 1 and then "%20" :oops: what does the %20 do?

Before that you used the .c !=255, is that "if NOT C = 255"?
Why don't you test if "if is equal to 255" and then set the switch the YES/NO?

The ESP8266, it's been a long time since I used it, but there are tools te put the firmware back.
I think the last time I used the ESP flash tool I used version 2.4 from http://domoticx.com/esp8266-wifi-softwa ... l-flasher/. And I think I used firmware version 1.3 from this Github: https://github.com/mlwmlw/esp8266-works ... r/firmware
For me these files worked. But read about flashing the ESP because, as I recall, there is one pin that has to be pulled low (or high) before flashing the ESP.

I am going to study your files and try to make something from it.
The E32 pulls down the AUX pin after sending it's data via the RX, so I guess this must become my terminator signal.

Thanks for your help and hopefully I helped you with the ESP stuff..

BTW, the E32 and the Arduino share the same GND.

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by kersing »

The dot is used for local (in contrast to global) variables.

The percentage sign is modulus and is used to calculate the remainder after division. So 105 % 20 = 5.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

- Written from scratch.... It's only a few macros - so chances are it's cropped up before and will crop up again?

The receive was based (mostly cut and paste) on my previous example (circular_buffer) - I added the OLED code. Code re-use is good!

The '.' (dot) notation lets you refer to local (as opposed to global) variables. I would strongly encourage their use - it makes code much easier to read and understand (I think FC should default to local variables rather than globals when defining a new macro). For example I tend to use 'i' as a loop index many times - and if 'nested' loops then I would come unstuck. It also allows for recursion.
A good habit - recommended. Also try and split code into small macros - that you can test and debug easily - large 'monolithic' programs sometimes seem the norm for MCUs but maintenance is a nightmare. It goes without saying that comments are essential (and we're all guilty of not using enough!!!)

'%' is the mod (modulus) operator - so .i = (.i + 1) % 20 will loop 0..19..0..19 - so my string buffer will never overflow (which would usually result in bad things (unexpected things) happening). Note that hard-coding the length is bad practice - and I should really have used a constant - then if I ever change the length of the buffer I wouldn't have to check every instance (and this is a very simple example!)

!= is NOT EQUAL (so read 'if a is NOT EQUAL to b') - it's a matter of taste. Try and be consistent in style - makes your code easier to understand when you come back to it :-)

If '.c = 255' and swap yes / no - again a matter of style. I tend to think that I want to process the character if it's not equal to 255. There is no performance penalty either way - so do which you prefer... (I would say that swapping is less common though :?: )

I used esptool to reprogram the ESP device. I can get microPython running smoothly - but the AT command interface (which needs several files uploaded to different addresses) crashes the chip. I guess I am using the wrong addresses and/or the wrong files (there are several in the download from espressif).
More googling probably needed.

Martin

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

Thanks all, but it seems to boil down to a very simple thing..

With all the testing, especially with your tips of using RXint, it seems that the main problem is putting all the single received strings into one larger.

The following chart I tested and it may seem a little strange, but this gives me a lot of information.

What it does is following.
- Initialise everything (UART, and interrupt on RX).
- Send the message via the E32 module
- Waits for reaction from another module and in the macro where the strings are received, sends it right back on the TX (for me to see what is going on).

Till this point everything works (confirmed)..
But then, in the macro "receive" I try to add all separate bytes to one string.
Normally this must be the same as the message I've send.
I see the separate letters appear on my logic analyser, but I can't get it to make one single string from them.
.

I want these to become 1-single string that can be compared with the original message.
So it's not a matter of RX, TX, but more like handling strings (combine the separate strings to one, and later compare them with the original string).

In the Flowchart, from after a small pauze (4 seconds), I know (and see) that all data is send and received back.
I send the data that is combined from the receive macro, but that doesn't return anything..

So the problem seems to be in combining the RX data and compare them against the original message.
Any tips on that problem?
TX FC09072019.fcfx
(24.08 KiB) Downloaded 148 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by mnf »

The Receive interrupt is 'odd' and I don't think it will work as you expect.

You are using ReceiveString - use ReceiveChar (with a byte variable)

Don't use SendString in the interrupt handler. (Receive the string and then reply - this happens in main - if each character needs echoing then do it outside of the interrupt. Currently each char is echoed (probably won't in interrupt) and then whole string in main)

ReceiveText[Counter] = receive doesn't do what you think (should be receive[0]) as receive is a string (see above)

The received text needs a terminator character (0) - for comparisons etc to work correctly.

See step2.fcfx

Martin

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Arduino Nano, interrupt and UART questions

Post by MJU »

mnf wrote:The Receive interrupt is 'odd' and I don't think it will work as you expect.

You are using ReceiveString - use ReceiveChar (with a byte variable)

Don't use SendString in the interrupt handler. (Receive the string and then reply - this happens in main - if each character needs echoing then do it outside of the interrupt. Currently each char is echoed (probably won't in interrupt) and then whole string in main)

ReceiveText[Counter] = receive doesn't do what you think (should be receive[0]) as receive is a string (see above)

The received text needs a terminator character (0) - for comparisons etc to work correctly.

See step2.fcfx

Martin
I've been testing all day and night.
I think I'm going to let it rest for a while.

This seamed like a simple thing, but it turns out to be a nightmare :-)
Even after comparison with what I made before with Flowcode.

I think I need to study the CB first..
Thanks for all the help so far!

Post Reply