Remember last used momentary button

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

Moderator: Benj

Post Reply
Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Remember last used momentary button

Post by Frank607 »

I want to use some EEPROM functionality’s but I have never done this.

I use a PIC16F1788 for this.

Explanation:
I have 8 momentary switches with their own Macro program. So Switch 1 calls Macro 1, etc.

When I remove the power and plug it back than the microcontroller starts with (off course) Macro 1. So, I need something to write the last used momentary switch in EEPROM. So when the power comes back the last used Macro will run.

Maybe this is simple to do, but I don’t know where to start and how to use this EEPROM functionality.

If someone can give some very simple example than I will try to implement this in my Flowcode (7)

Thanks,

Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Remember last used momentary button

Post by medelec35 »

Hi Frank,
Have you check out EEPROM Wiki page as there are Flowcode examples.
After learning how to use EEPROM component, you can give it a go and implement EEPROM to your flowchart.
If you then get stuck, we can assist you further.

Martin
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

Thanks Martin,

I must say that I dint found this page. I will read it and try to understand it.

Thanks !

Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Remember last used momentary button

Post by medelec35 »

Hi Frank,

You're welcome.
My advice would be to look at the

Code: Select all

Read and Write Bytes
example.
You can view the contents of EEPROM if you are able to view the Console.

Martin
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

Yesterday and this morgen I tried to make my first EEPROM read from the last used switches (momentary buttons).

I have made some simple Flowcode with LEDs just to see what will happened. Unfortunately I have no luck at all.

I hope that someone can take a look to this Flowcode and correct what I have done wrong.

Thanks.
Frank
Standard Program 2 buttons -EEPROM.fcfx
(19.59 KiB) Downloaded 230 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Remember last used momentary button

Post by medelec35 »

Hi Frank,
The problem with your flowchart is you are writing to address 1 with the value of 0 or 1.
Then when flowchart starts, you are reading from address 0!
The EEPROM component has preset values for demonstration purposes, so I always go in to EEPROM and delete all of them.
I Have also Removed a recursive function call that was preventing flowchart from compiling.
Did you know that with masking you can use one byte variable for all eight switches?

Martin
Attachments
Standard Program 2 buttons -EEPROM.fcfx
(20.36 KiB) Downloaded 210 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

Hi Martin,

Thanks for chancing my flowchart in the right way. I see now what I have done wrong.

Yes I know that I could use the masking option for all my eight switches. I will implement that when I build further with this flowchart. In the hardware I have taken care of this. PORTA has all the eight switches and PORTC has all the LEDs.

One more question:

If I want to “break in” with switches in a running loop (macro1 to 8 ) must I do this with “Connection Points” or is there some other option?

Thanks,
Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Remember last used momentary button

Post by medelec35 »

Hi Frank,
Connection points should be the last resort as they can be classed as bad programming.
It would be far better to have the loop within Switches macro to only loop on a value of a variable
E.g instead

Code: Select all

loop while: 1

You could

Code: Select all

loop while: Exit = 0
Then ant any point if the Exit variable changes from 0 to 1 then the Switches macro will be exited and you will be back within Main macro.
Or a better way would be to stay within main macro all the time and place all the decision branches there instead of within switches call macro.

Martin
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

medelec35 wrote:...
Or a better way would be to stay within main macro all the time and place all the decision branches there instead of within switches call macro.
Martin
Hi Martin,

I have tried this:

My idea is that if I press a button in $PORTA the running macro will be exit and then run the macro what belongs to the “pressed button” in Main.

I have attached my idea but I can’t this to work. I know I must do something with Variables.

Thanks,
Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Remember last used momentary button

Post by medelec35 »

Hi Frank,
I have modified your flowchart so you can easily add more call macros and only having a single switch variable.
Also as a demonstration, it will stay in macro until a switched is pressed, then will go back to main.

Hope this is helpful?

Martin
Attachments
Standard Program 2 buttons 1a -EEPROM.fcfx
(21.24 KiB) Downloaded 212 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

Hi Martin,

Again thanks for helping me out. I will look to this at Tuesday.

Regards,

Frank

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

Hi Martin,

Finally I have time to work with your flowchart.

Can you explain to me why the LED (5mm clip1) in Marco_0 flashes for some 3 seconds while in the Macro_0 there is 50ms On and 50ms Off? (in simulation and in hardware)

I can’t find any delay in this flowchart. The OSCCON and Clock Speed (Hz) are both on 16MHz.

Thanks,

Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Remember last used momentary button

Post by medelec35 »

Hi Frank,
Frank607 wrote:Can you explain to me why the LED (5mm clip1) in Marco_0 flashes for some 3 seconds
Sorry it was my maths error.
Instead of

Code: Select all

(EEvalue_Last_Used_Switch = 50)

It should have been

Code: Select all

(EEvalue_Last_Used_Switch = 20)
That's because 1000ms / 50ms = 20 not 50
If left at 50 then dealy = 50 x 50ms = 2.5 seconds.
With

Code: Select all

EEvalue_Last_Used_Switch = 20
delay - 20 x 50ms = 1 Second.
I have attached a corrected flowchart to try.
The reason for the delay loop with short delay component of 50ms instead of a delay component 1 second is to allow to break out the loop practically instantly.
You wont have to wait for 1 to 2 seconds to elapse.
The way is to use timer interrupt for timing.
No matter how long the delay e.g seconds, minutes, hours, days etc.
With a timer interrupt it will allow you to break out instantly on a press of a switch!

Martin
Attachments
Standard Program 2 buttons 1b -EEPROM.fcfx
(22.07 KiB) Downloaded 149 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Remember last used momentary button

Post by Frank607 »

Hi Martin,

Ohh man :-) This looks very good! Exactly what I need.

With the explanation in your latest answer I understand now how it works. Great work Martin.

But as always…. I’m modifying Macro_0 to make it simpler to light up some LED’s. I want to make use of BYTE values as “0b11100000“ .

This works on $PORTB0 but not on $PORTC0 . Why not ?


EDIT: I have found it. Just a simple mistake what I have made.

Thanks,

Frank
Attachments
Standard Program 2 buttons 1c -EEPROM.fcfx
(28.39 KiB) Downloaded 128 times

Post Reply