Read momentary switch state - press and press again must be On / Off

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

Moderator: Benj

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

Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi to all,

I have a:
PIC16F18346
Four momentary switches
Four LEDs

What I want to make is the following program:
When Switch 0 is pressed LED 0 must be go on.
When Switch 0 is pressed again LED 0 must go off.
(the same with the other switches and LEDs)

Must I do this with a EEPROM or is there is easier way to make this. (btw, there must be no room for errors on this)

I have looked to this flowchart from Medelec35:
viewtopic.php?f=54&t=16123&p=65873&hili ... gle#p66935

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

I think I have found it. Please wait with answering, first I will try by myself :-)

Sorry, it didn’t work.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Hello,

No need for EEPROM you can simply do something like this. I've coded the first two for you.
SwitchAndLED.fcfx
(16.2 KiB) Downloaded 528 times

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

This is great and fast ! Thanks.

I had made also something from a other flowchart found on this forum used as base. See attachment. But I didn’t get it to work.

But yours is much more better :-)

Thanks again and regards,

Frank
Toggle Momentary Switch V1.0.fcfx
(30.88 KiB) Downloaded 395 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Forgot to mention the values for the mask variable should be.

1
2
4
8
16
etc

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

Yes, I have seen this.

I have learned something from an earlier flowchart.
viewtopic.php?f=63&t=20241

But thanks for mention this.

Frank

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

Is it possible that this flowchart is not working in hardware?

I have changed your flowchart into my connections but I think that it is not running. The Switches S0 to S3 are in fact input pins and getting activated from a other microcontroller. The output pins from the other microcontroller are activated for 500ms per cylcle.

I have checked the output pins from the other microcontroller and they are working just fine (5V output)

EDIT: I think I see the problem. My four switches are on B4 to B7. Could it be in here?
I have set the mask for PORTB.4 to 16 and PORTB.5 to 32. But not working
Switch toggle V1.2.fcfx
(32.98 KiB) Downloaded 342 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Hello,

The mask values don't need to start at 16 the values 1, 2, 4, 8 should work fine. The components should take care of the bit renumbering for you.

With the code as is you can only energise one of the switch inputs at a time, e.g. B4 high, B4 low then B5 high and B5 low. This is because we wait for the switch to go low again before allowing the program to continues and check the other switches.

Have you connected ground between the two micros so they are on a common supply?

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

Thanks for answering.

Both chips are on the same PB with the same ground.

The other chip is a HCS512 chip with a four button remote control.

So when I press button S0 the output from the PIC16F18346 must go high until I press again the S0 button the output must go low.

The same with S1, S2 and S3. But this must be used in a mixed way. In the flowchart simulation this works perfect.

So I don’t know what you exactly mean with

Code: Select all

“With the code as is you can only energise one of the switch inputs at a time, e.g. B4 high, B4 low then B5 high and B5 low. This is because we wait for the switch to go low again before allowing the program to continues and check the other switches.”
Regards,

Frank

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Hi Frank,

I think I see the problem, You likely need pull down resistors on the data pins to pull the pin signal to 0V when the HCS512 switch is not active. Otherwise the trace will be floating and may be read as a 1 or a 0.
With the code as is you can only energise one of the switch inputs at a time, e.g. B4 high, B4 low then B5 high and B5 low. This is because we wait for the switch to go low again before allowing the program to continues and check the other switches.
Buy this I mean this situation will work

Switch 1 high
Switch 1 low
Switch 2 high
Switch 2 low

but this will not - in this case only switch 1 will be seen.

Switch 1 high
Switch 2 high
Switch 2 low
Switch 1 low

The lack of pull down resistors is likely the cause of the problem.

Unfortunately the micro you're using does not have internal pull down resistors so you will have to fit external ones.

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

So, in fact this will never work how I want it. :-(

I must make new PCBs because of adding extra connectors. Do you know what will be a good chip for this? I thought that the PIC16F18346 was pretty new.

Regards,

Frank

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Hi Frank,

I'm looking at the HCS512 datasheet again and I may be wrong about the pins being switches. It says the S1-S4 pins are TTL outputs and so should always pull high or pull low. If this is the case then no external resistors should be required.

Have you confirmed the micro is running on the PCB? The one second flasher test is a nice sanity check to confirm the chip is running.
https://www.matrixtsl.com/wiki/index.ph ... ED_flasher

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Indeed. I have measured the S0 to S3 outputs from the HCS512 and they are in normal state LOW. When I push button S0 from the remote then the HCS pin has 5V for 500ms.

So this is perfect. I also have measured if this 5V goes the PIC and that is true. (just to be sure)

I’m afraid as I mentioned earlier that the PIC is not running. The PIC has 5V. But I don’t know what I must change in the Configuration Menu from this PIC in FlowCode. Only change to INTOSC.

I will test the LED Blink tomorrow because I must go away. I will let you know.

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

This morning I have tested with a simple LED flasher on A2. This is working perfect.

But when I add a simple switch on B4 to let light up the LED by pressing a button on the FOB key (S0) the LED does not light up.

I have measured this and the HCS5212 is given 5V to port B4 when pressing the button S0 on the FOB key.

So I have the feeling that the whole port B is not working for some reason. Can this be true ?
Simpel LED Flasher.fcfx
(8.9 KiB) Downloaded 232 times

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

I have changed the chip to an older PIC16F690 (same pinout) and everything is now working perfect with all the KEY FOB buttons. Including total random use of the buttons.

So there must be something with PORTB of the PIC16F18346 but I don’t know what. I hope someone can explain to me what went wrong with this PORTB.

I have also in stock a PIC16F1829 (same pinout) I will try this one later.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Aha,

I've found the cause of the problem with the 16F18346.

We are disabling the analog pins for port A and port C but seem to have missed out port B. By default the device is in analogue mode which means that digital input doesn't work.

I'll get the issue fixed in the definition file but for now simply add this code in a C icon at the start of your program.

Code: Select all

ANSELB = 0;

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Ben,

Ok, this kind of mistakes can happen and no problem at all :-) I’m glad that it is solved for now. I can’t test it anymore because this was my last PIC16F18346 and I had to cut the pins to remove this PIC from the PCB.

I will order new ones.

I have just send my new PCB to the manufacturer with some modifications. But I can always use the following PICs because of the same PINOUT. (there are more, I know)

PIC16F690
PIC16F1829
PIC16F18346

Which one do you prefer: the PIC16F1829 or the PIC16F18346 for this application ? I have now a choice to make :-)

Many thanks for solving this issue. There is no way that I could find this error / issue by myself in a definition file.

The new definition file I will get in some way; I hope so :-)

The program FlowCode is great and the support is great. Stay on this road ! :-) Thanks agian for helping me Ben !

Regards,

Frank

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read momentary switch state - press and press again must be On / Off

Post by Benj »

Hi Frank,

The bug should now be fixed and you can get the latest fixes in v8 by clicking help -> Check for updates. I'll push the fixes here for v7.
viewtopic.php?f=63&t=19743

As for which chip to use, I generally would go with using the cheapest chip that will do the job you need or which ever chip you feel more comfortable with, there are a lot of chips now available and selecting the correct one is becoming harder and harder. Still nice to have a choice :wink:

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: Read momentary switch state - press and press again must be On / Off

Post by medelec35 »

Hi Frank,
The link of mine you was using had EEPROM only so the value can be stored in the even of power failure.
The most efficient way I can think of to include switch debounce is attached.
Hopefully it will do what you want it to do?
You may be surprised how little amount of icons are used considering there are 4 switches, each with 2 states.
Attachments
Switch toggle V1.3.fcfx
(31.32 KiB) Downloaded 243 times
Martin

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Martin,

Also many thanks for your support. I want to say this because other companies don’t have this great support. For example the support from Infineon is a real disaster :-(

I have downloaded your FlowChart and it looks very good :-) I can’t test it right now but the new PCBs are now in production so I will expect them beginning next week.

You talk about power failure and it gets me again thinking… It could be nice that I have also a flowchart that’s stores the current state of switches in EEPROM in case of a power failure.

The reason that I don’t know this is simple: The current HCS512 PCB was totally burnt out. The company who have installed this system some 20 years ago is bankrupt. They have destroyed all schematics, documents, administration and hard disks with a clean up. So there is nothing. But they have leaved the Manufacturer Code, needed for HCS chips to work.

I was thinking this is a piece of cake. :-) But forget it. Programming of this HCS512 chip with the Manufacturer Code was a real disaster for me. No today programs from Microchip can do this. I had really no clue on how to program this device.

So, I had to make an old school HCS programmer with old Microchip programming software what I have found on internet. The GUI software from Microchip is that old that it only runs on Windows XP with a COM port. But it works :-)

Now I can easily program almost all HCS chips. I have learned a lot :-)

It took me almost one year to get (almost) finish this project. Because I had no idea on how to program this HCS devices.
viewtopic.php?f=63&t=20381#p90112

I learn also a lot with FlowCode and especially from this forum. But when it comes on making a safe FlowChart than I need some help to reduce any errors.
Thumb.png
(1.62 KiB) Downloaded 4309 times
( I wrote to much I see, sorry :-( )
Last edited by Frank607 on Mon Jul 29, 2019 6:08 pm, edited 1 time in total.

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: Read momentary switch state - press and press again must be On / Off

Post by medelec35 »

Hi Frank,
Frank607 wrote:Also many thanks for your support.
You'e welcome.
Sounds like you had your work cut out!
Well done for getting there at least it works.
Frank607 wrote:The software from Microchip is that old that it only runs on Windows XP with a COM port. But it works
As PC's don't last forever would you be able to set up a PC with a virtual machine running XP and use a USB to RS232 converter?
Frank607 wrote: It could be nice that I have also a flowchart that’s stores the current state of switches in EEPROM in case of a power failure.
I have attached one way of doing that.
Attachments
Switch toggle V1.4.fcfx
(32.9 KiB) Downloaded 218 times
Martin

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Martin,

I will try your 1.4 Flowchart when I have received my PCB’s.

I have VMware but in this case I needed to be sure that the programmer will work. Now it works, I can indeed load Windows XP in VMware. I will also going to make use of USB to RS232.

To eliminate all possible new errors in this case, I just install a clean XP machine for now. :-)

But thanks for your ideas.

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Hi Martin,

Can you explain how I get the Relays to the switches?

I don’t understand this: Output = Output XOR 1 << 1 . Because I have never worked with XOR. I know it is a Exclusive OR.

Thanks.

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: Read momentary switch state - press and press again must be On / Off

Post by medelec35 »

Hi Frank,
Frank607 wrote: Can you explain how I get the Relays to the switches?
Not sure what you mean?
XOR is just used to toggle a single bit of a byte.
The output is using a byte variable or which only bits 1,2,4 & 5 are used.
If you XOR an output bit with a value that is 1 for bit 5, then the bit part of the output will toggle (change from 0 to 1, or from 1 to 0)
For example, if the output = 0b00110000 & you XOR with bit 5 = 1 i.e 0b00100000 the the output will change to 0b00010000.
If you XOR the output again with 0b00100000 the the result will be 0b00110000, i.e back to with what we started with.

Hope that answers your question?
Martin

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

Re: Read momentary switch state - press and press again must be On / Off

Post by Frank607 »

Thanks Martin,

I work with “0b00110000” in my LED flashers to toggle the LEDs. So this part I know how it works. But how can I implement this is your flowchart?

I see that the LEDs are going On when I push a button. My relays are on PORTC and I don’t know how I can activate them in your flowchart.

I’m sorry :-(

Post Reply