View Full Version : 1 button multiple functions

08-31-2007, 09:32 PM
Hello all,

I just got my NEW propeller kit http://forums.parallax.com/images/smilies/smilewinkgrin.gif·and now I am going to try and convert the code I have for a project to the propeller chip. I figured I would start with·this forum, and get some help.

I have 2 questions I hope someone can help me with

The chip I am using is a ZX-24

Question #1:

On my project I have a single button that has to do 4 different things.

1st press: Turn on the power to the system
2nd press: Turn on wireless receiver
3rd press: Turn on second receiver
4th press: Turn off system after holding for 3 seconds I have not gotten this to work in current code

The main reason for not just turning everything on is that the system is battery operated. The wireless video receivers draw a bit of current, and I only want to turn them on if they are needed.

The forth press which turns off the whole system is what I have been struggling with.

Below is the code for the power button in my current chip:

Private PowerButtonTaskStack(1 to 75) as Byte

CallTask PowerButtonTask, PowerButtonTaskStack

'Check to see if the start button has been pressed
Sub PowerButtonTask()
·· Do
' Wait for a button press
····· Call WaitForInterrupt(zxPinFallingEdge, 0)
' Toggle the device state
·Call PutPin(RelayPin, IIf(deviceIsOn, Power_off, Power_On))
·Call PutPin(RedLED, LEDon)
·Call Putpin(PowerLED, LEDon)
·deviceIsOn = not deviceIsOn
·Button_Status = Button_Status + 1
·'Call Delay(0.500) '500mS time delay
'If the power is on then go check if button has been pressed a second time
'Turn on the wireless receiver
If deviceIsOn = True and LowBatt = False then
··Call WaitForInterrupt(zxPinrisingEdge, 0)
··Call PutPin(GreenLED, LEDon)
··Call PutPin(Wirelesspin, Power_On)
··Call LCD_DisplayStrAt("W", 1, 15)
··Button_Status = Button_Status + 1
end if
'If the button has been pressed a 3rd time turn everything off
LongPressTime = 5.0 'time in seconds
If Button_Status = 2 and deviceIsOn = False then
·If GetPin(SwitchPin) = 1 Then 'uh oh, someone pressed the button
··········· 'Check if 5 seconds have passed
·············· Call PutTime(0, 0, 0.0) 'Reset the RTC
·············· Do While GetPin(SwitchPin) = 1
················· Sleep(0.0) 'just wait
·············· Loop
·············· Call GetTime(ButtonHour, ButtonMinute, ButtonSecond)
'Check the time If ButtonSecond > LongPressTime Then 'Check if the button was pressed for more than the Long press time
····LongButton = True
····Call PutPin(GreenLED, LEDoff)·
····Call PutPin(RedLED, LEDoff)
····Call PutPin(PowerLED, LEDoff)
····Call Putpin(Wirelesspin, Power_off)
····Call LCD_Clear()
····ShortButton = True 'Short button press
···End If
·············· ShortButton = False
·End If
End If
' Time delay for switch debounce
Call Delay(0.250) '500mS time delay
·· Loop
End Sub

Question #2

What is the best way to measure voltage and current with the Propeller chip?

I am using a simple voltage divider and using the internal A/D converter to do the work. It seems to work ok, bit is there a better way to do it with the Propeller chip.

I would like to do some kind of running average, and I would like to get rid of the multiplier.

' Get the voltage measurement
Private Sub Read_Voltage()
·Voltage_Multiplier = 14.98
·Call Getadc(VoltagePin, Voltage)
·OutputVoltage = Voltage * Voltage_Multiplier
·Call Battery_Status
·If ((Status_Flag = 1) and (OutputVoltage > 11.70) _
··and (OutputVoltage < 13.77)) then
·Call Wireless_Power
·Lowbatt = False
·End If
·call Delay(0.25)
End sub

Thanks for the help

09-01-2007, 05:53 AM
I think it is best you convert this BASIC Code into SPIN and - when questions occur - ask with giving that SPIN code.
You can do some debugging using Andy's PropTerminal window on the PC.

Reading voltage and current is done usind the same external parts as with your former micro, however you have to connect a additional externa ADC. You have to add communication to the ADC; this is more complex than with build-in ADCs...

But you can also use very simple ADCs (e.g. using Delta-Sigma-Modulators) There are examples in the Propeller object collection..

Mike Green
09-01-2007, 08:40 AM
When you look at the Propeller manual, keep in mind that a lot of the calls your Basic program is doing represent the same actions as some simple Spin statements. For example, WAITCNT is used to wait for a particular time to occur. The I/O pins are effectively variables and can be accessed directly. The ADC function depends on how you implement it, whether you use the built-in delta-sigma capability (which requires a few resistors and a capacitor close to the Propeller) or use an external ADC.

Most of these more complex functions are done using "objects" you can download from the Propeller Object Exchange. These are effectively packages of library routines for interfacing with some kind of external device like a LCD, VGA, or TV display or ADC or even the sigma-delta ADC is done this way. Have a look through the library.