News:

Let's find out together what makes a PIC Tick!

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