Mobile App Continuous Toggle Button

 
#1

I am working on a mobile app and have a button that I want to be able to toggle. First push would turn leds on and next push would turn leds off. This action should be repeated each time I push the button. The following script toggles button on then off but does not repeat these actions again. Here is the code:

Code:



#In Connect Script

$Pressed = 0

#Script for Mobile App Button

if (!$Pressed)
#High Beams On
Set (D17,ON)
Set (D19,ON)
Sleep (100)
$Pressed = 1
Else
#High Beams Off
Set (D17,OFF)
Set (D19,OFF)
Sleep (150)
$Pressed = 0
endif



The first time I push the button the lights come on and the second time I push the button the lights go off. This is good but any further pushes result in no action. I tried putting in a loop but still didn't work. Any thoughts would be appreciated. Rick Smile

#2

@DJ:

There is an EZ-Script bug:

script to reproduce the bug:

Code:


$pressed=false
$pressed=!$pressed
if ($pressed)
#correct
print("true")
else
print("false")
endif

#generates:
#Error on line nn: Missing String Quotes or Invalid Expression at index 0: !
$pressed=!$pressed
if ($pressed)
print("true")
else
print("false")
endif

#3

@Rbonari,

Change your code to:

Code:


if ($Pressed)
# High Beams On
Set(D17,ON)
Set(D19,ON)
$Pressed=0
ELSE
# High Beams Off
Set(D17,OFF)
Set(D19,OFF)
$Pressed=1
endif



Note: Spaces between Set and ( generates a parser error.

#4

@PTP

Thanks much, I will give this a go. In my mobile app script I don't have a space between Set and ( but thanks for the reminder. Rick

#5

Use the Blockly interface to learn how to use ez-script. Click on the word Blockly in this message to read about it. Do not put spaces between brackets and commands, that is not correct syntax. Please read the ez-script manual, examples are provided. Blockly also provides examples.

Code:


if ($pressed = true)

set(D0, true)
$pressed = false

else

set(D0, false)
$pressed = true

endif



User-inserted image

#6

@PTP

Thanks PTP, that works great. I have two other buttons that I want to do the same thing with but they don't work at all. I use the same exact code but different sets of leds but no go. I put the following code in the connect script for the second and third buttons:

Code:



$Pressed1=0
$Pressed2=0



Do I need to name these button variables with a unique name for each, other than Pressed? Thanks much...Rick

#7

@PTP

Evidently it is invalid to have variables $Pressed, $Pressed1, etc. When I changed the variable names to $Pressed, $Highbeams and $Taillights all three buttons worked as advertised. Normally $Pressed, $Pressed1 and $Pressed2 would be considered valid variables. Any thoughts on this. This appears not to be the case with ez-script? Thanks again...Rick

#8

@rbonari,

No problem with variables ending in numbers, must be something else.

Example project:
test_buttons.EZB

1) Start the init script
2) press the buttons and listen the press state on your desktop speakers.

#9

@PTP

Thanks PTP. I will give it a try and keep you posted....Rick

#10

@PTP

I tried again and included $Pressed, $Pressed1 and $Pressed2 in my connection script and the three mobile app buttons are working. Not sure why it didn't work yesterday. I was wondering if it has something to do with the variables being initialized in the connection script rather than a startup script. What is the difference between connection startup script and a separate startup script. Seems that the connection startup script is the best way to go as the script is run automatically on connection. Your thoughts are appreciated....Rick