News:

;) This forum is the property of Proton software developers

Main Menu

How can I solve this?

Started by Mapo, Dec 16, 2025, 09:41 PM

Previous topic - Next topic

Mapo

Hi everyone,
I have two buttons and would like to change the position of the controls.
In simple terms:
Dim Button1 as byte = 0
Dim Button2 as byte = 1
If portb.button2 = 0 Then
....
The compiler writes this error:
*** Invalid Cast for this type of variable! ***

How can I fix this?


RGV250

Hi,
Dim Button1 as bit = 0
Dim Button2 as bit = 1
If button2 = 0 Then

Bob

RGV250

Hi,
Or did you want to do something like this?
 Symbol Button2 = PortB.1
 if button2 = 1 then

Bob

Mapo

Example: key 1 moves forward and key 2 moves backward.
Now, using a switch, I'd like the ability to swap key 1 with key 2.
Of course, I'm not limited to just two keys.

RGV250

Hi,
If I understand correctly

if Switch = left
Button 1 = forwards
Button 2 = backwards

if Switch = Right
Button 1 = backwards
Button 2 = forwards

If this is the case I think it would be best to have a couple of subroutines or procedures, one for left and one for right which returns a temporary variable for the direction.

Regards,
Bob

trastikata

Dim Button1 As Pin
Dim Button2 As Pin
Symbol Switch = PORTC.0
 
Main:
    If Switch = 0 Then
        Button1 = PORTB.0
        Button2 = PORTB.1 
    Else
        Button1 = PORTB.1
        Button2 = PORTB.0
    EndIf
End


Make sure you have the correct port direction set i.e. I/O

Stephen Moss

Quote from: Mapo on Dec 16, 2025, 11:23 PMExample: key 1 moves forward and key 2 moves backward.
Now, using a switch, I'd like the ability to swap key 1 with key 2.
Of course, I'm not limited to just two keys.
I am sure you have a reason for wanting to do that, if it is an internal switch for factory customisation so it is a set once and leave then it would be fine. However, if it is an external switch I am not sure that is a particularly practical solution I think it would get annoying in use as you press the button for one direction but it goes the other way due to the switch position being changed.

Quote from: Mapo on Dec 16, 2025, 11:23 PMOf course, I'm not limited to just two keys.
If it was just the two then as you are using switch anyway I would have suggested using a DTDT switch to swap them instead of remapping them in code, but that may not work so well if you are swapping more than two inputs.