Page 1 of 1

Double Click Single Switch Code

Posted: Thu Mar 08, 2012 9:43 pm
by Collimare
I have been having a great time programing our new LED timer control board. We only have one control switch, a momentary push button, and I have been successful with single press and various press and hold timing functions but cannot get the double click function to work well at all. I have done various searches but cannot find a good example for double click with a single button. Any good code examples? Thanks, Howard

Re: Double Click Single Switch Code

Posted: Thu Mar 08, 2012 10:26 pm
by medelec35
We wont be able to post anything for you until we know what version of Flowcode, target device and osc.speed + type e.g internal. external etc.

By the way I would do this is have a timer interrupt that starts timing when switch is first detected resetting count to 0.
Every time switch is detected add 1 to a count variable up to a maximum of 2. this can be done by if count <2 then count = count +1
Then when timer times out after 1 second for example, use a decision branch:
If count = 1 then single switch is detected so do something

If count = 2 then double switch is detected so do something else.

Martin

Re: Double Click Single Switch Code

Posted: Thu Mar 08, 2012 10:38 pm
by Collimare
Hi Martin,

Thank you for the quick reply!

Sorry for the omissions:
Version 5.1
Using standard HP-488-00-3 on board PIC 16F88. I have not changed anything from start.

How does the counter accommodate a press, release, press again, release? This seems like a hold function similar to what I have now. I must be missing something.

Thanks, Howard

Re: Double Click Single Switch Code

Posted: Thu Mar 08, 2012 11:02 pm
by medelec35
No problem, Welcome to the forum.

If you post the flowchart you have created, then I will modify it for you so you can use a switch to double or single click it.

Also can you read this please:
http://www.matrixmultimedia.com/mmforum ... =46&t=9958

This topic will be moved to V5 section soon.
You should only post your own tips in this section and not start off with a question.
The reason is if anyone is looking for a tip it makes tips easier to find.

Its not a hold function as routine is not waiting to carry on after 1st switch is detected.
Its more of a time out function.
If switch is pressed once within a second, then single switch det.
If pressed twice within a second, then double switch det.

Martin

Re: Double Click Single Switch Code

Posted: Thu Mar 08, 2012 11:21 pm
by Collimare
Hi Martin,

Thank you very much. I am really excited about programing again though it has been decades since my Z180 assembly days. I did fill out the form in the link with regards to V5.1. Yes please move it my apologies.

In the attached code you will see a single push turns on the LED and Lasers. A push and hold for short time turns only the LED on. A push and hold for a longer time turns the LED on in low power mode.

What I would like is to single click and the LED & Lasers come on, double click the button to have only the LED come on (Similar to a double left mouse button) push and hold for 1 to 2 seconds sets the LED to low power mode.

Thanks, Howard

Re: Double Click Single Switch Code

Posted: Thu Mar 08, 2012 11:30 pm
by medelec35
Collimare wrote:Hi Martin,

Thank you very much. I am really excited about programing again though it has been decades since my Z180 assembly days. I did fill out the form in the link with regards to V5.1. Yes please move it my apologies.
Hi Howard,
Programming V5 is exciting, I'm loving evey minute of it.
As for moving, It's fine for now, I was going to give it a couple of days before moving to give your registration for V5 section time to be sorted, otherwise you won't be able to post on the V5 section.
Collimare wrote:What I would like is to single click and the LED & Lasers come on, double click the button to have only the LED come on (Similar to a double left mouse button) push and hold for 1 to 2 seconds sets the LED to low power mode.


I will post that for you, but may not be until tomorrow, as time is running out for staying up tonight.

Martin

Re: Double Click Single Switch Code

Posted: Fri Mar 09, 2012 8:05 am
by medelec35
Hi Howard,
Do you need two switches as in your descriptions you describe using one switch, yet on your flowchart you have two switches.
If you only require one switch, is there a pin this switch must be connected to?
If necessary, is it OK to use RB0 or RB7 for connection of the switch?
I'm only asking in case I need to use port B0 INT or RB4 to RB7 interrupt on change (IOC)


Martin

Re: Double Click Single Switch Code

Posted: Fri Mar 09, 2012 10:44 am
by medelec35
Hi Howard,
I thought I would post a non interrupt version for you to have a play with.

It should be easier than an interrupt version to follow.

I have implemented single press, double press and long press all with just a single switch.
I have mapped key 0 to the switch, so whether your single stepping or running simulator, switch is activated all the time key 0 is held down.
Or you can just push switch with mouse as usual of course. :)

Not tried with hardware, will do that later.

Hope it what your after and have fun playing.

Martin

Re: Double Click Single Switch Code

Posted: Fri Mar 09, 2012 12:52 pm
by medelec35
Now you are registered as a V5 user, I have moved this topic to the V5 forum as it is more relevant.

Martin

Re: Double Click Single Switch Code

Posted: Fri Mar 09, 2012 4:08 pm
by Collimare
Hi Martin,

Thank you very much. The laser only switch is not standard, but a special add on for some customers. Normally there is just the one switch. I am very open to the interrupt version if it is better. I have not committed anything to hardware yet just using the demo board for now.

I will start playing with your non-interrupt version.

Thanks again, Howard

Re: Double Click Single Switch Code

Posted: Fri Mar 09, 2012 4:40 pm
by medelec35
Hi Howard,
If I get a chance a bit later I will see what I can sort out with interrupt version.

I have just tried on hardware the not interrupt version and it works very well indeed.
Seems 100% reliable

Hopefully you will agree, but you never know.
You may have a different opinion? :lol:


Martin

Re: Double Click Single Switch Code

Posted: Sat Mar 10, 2012 1:02 am
by Collimare
Hi Martin,

I have incorporated your double click design and it is working well as is. No need for interrupts. Note: The the loop time is 15 for the hardware and 75 for the simulator.

Thank you very much for your time and efforts.

Have a great weekend.

Cheers, Howard

Re: Double Click Single Switch Code

Posted: Sat Mar 10, 2012 10:24 am
by medelec35
Hi Howard,

Thanks & Your welcome.

I did not realise that you required active low. So if i/p is +5V then switch is not pressed.
I need to look in at the switch macro, as I was not happy with the way it simulated. I think there was a post about this, I will check it out.

So I have changed the Flowchart to work as active low both in simulator and hardware
I have just incorporated the laser + switch so that each time switch is pressed:

1) If laser is not running and PWM is >0 then PWM = 0 and laser will go on.

2) If laser is running and PWM is >0 then PWM goes to 0 and laser stays on

3) If PWM is not running, then laser will toggle. E.g if laser is on, it will go off. If laser is off it will go on etc.

Hope you like modification.

There should be no need to have different values for simulation and hardware. Should work well in both.


You have a good weekend to.

Martin

Edit:
The thing that is puzzling me is:
Collimare wrote: Using standard HP-488-00-3 on board PIC 16F88. I have not changed anything from start.
Looking at the circuit, that is active high and not active low. So pressing a switch will cause the voltage on the pin to go from 0V to +5V (well nearly!)

So try both Flowcharts and see which one works for your application.

Re: Double Click Single Switch Code

Posted: Mon Mar 12, 2012 5:13 pm
by Collimare
Hi Martin,

Thank you very much for your continued help. It looks like the active high version matches the simulator and hardware and this is great. You have simplified the program nicely, but pressing the main button after the LED and Lasers or LED in the on state should turn them back off and it does not appear to. It does appear that the LED only switch is working correctly. If the LED only switch has turned the LED on then pressing the main button should turn it back off as well. Then the main switch program should be back in the normal monitor mode. Hope this makes sense?

This is as far as I have gotten.

Warm regards, Howard

Re: Double Click Single Switch Code

Posted: Mon Mar 12, 2012 6:41 pm
by medelec35
Hi Howard,
Collimare wrote: but pressing the main button after the LED and Lasers or LED in the on state should turn them back off and it does not appear to
Correct as that's what I thought you wanted?
But that could be just my misinterpretation, so sorry if that was not right.

Try this one, I have changed it so every time you keep pressing Main switch once, the laser and LED are toggled.
Except for: If only laser is on then pressing main switch will only shut laser off since pwm is already off.

Martin

Re: Double Click Single Switch Code

Posted: Tue Mar 13, 2012 2:16 pm
by Collimare
Hi Martin,

This is great. I will play with it for awhile.

Warm regards, Howard