question

OwaisAhmed-0825 avatar image
0 Votes"
OwaisAhmed-0825 asked RobCaplan edited

How to Toggle LED ON and OFF ?

Hi , I have these two red and gray images I want to toggle . How do you possibly do that ? I have no clue whats so ever how to do it ? Basically Ill toggle them and keep toggling them if some boolean variable is false in that case there's some form of error or fault other wise the state will be gray all times ! , You could easily do that in some embedded system or c++ but how do I actually do that in android . I could always show my implementation but I do not know what I'm to write . If for example I do change states continuously I have to also do other other task without waiting for the fault to correct itself .

Thanks,

dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How to Toggle LED ON and OFF

For this function, try using the Switch control which allows a user to toggle between two states. You could detect the CheckedChange event to listen the change of the swtich's state to perform the work.

var theSwitch = FindViewById<Switch>(Resource.Id.theSwitch);
theSwitch.CheckedChange += TheSwitch_CheckedChange;
private void TheSwitch_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
    if (e.IsChecked)
        //
    else
        //
}

Related doc: https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/switch

0 Votes 0 ·

@OwaisAhmed-0825

May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·

Extremely sorry for the late reply , I think I haven't clearly defined my problem , Im trying to simulate a blinking light every 2 seconds . Now that would mean If im do that Im bascially inside a loop until some flag becomes true. for example Ill show a pseudo code below.

while(! isTrue)
{
LedRed_ImageView.Visibility == Visible ;
LedGray_ImageView.Visibility == Invisible;
Delay(2000) // delay for 2 seconds ;
LedRed_ImageView.Visibility == Invisible ;
LedGray_ImageView.Visibility == Visible;

}

Is this possible to do in C# ? ,I believe I'll have to Alot a separate thread for this to happen and another thread to do some other work ? I don't know how to approach this problem !

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

A timer will execute a piece of code on a timed interval. Try using the Device.StartTimer to achieve the function.

bool theValue = true;

var value = testImg.Source.ToString().Split(':')[1];
Device.StartTimer(TimeSpan.FromSeconds(2), () =>
{
    //Console.WriteLine("testing times:"+count++);
    if (value == "ImgSource2")
        testImg.Source = "ImgSource1";
    else
        testImg.Source = "ImgSource2";
    return theValue;//change the value to false to stop the timer
});

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.