Blinking 3 Rainbow Hat Led in same time using Android Things Nxp Pico i.MX7D

Android Things

Hi folks, in our previous article - Rainbow Hat Peripherals/Component Example, we see some components and how to use those things. In this tutorial we are going to see how to blink Rainbow Hat LEDs. 

To create a project please follow the tutorial given above.

Hardware Requirements: 

1. Nxp Pico Starter Kit

2. Rainbow Hat

Programming Example: 

We have to add this dependencies in build.gradle(Module: app).

implementation 'com.google.android.things.contrib:driver-rainbowhat:+'

here '+' means the latest available dependency.

next, to use the latest Rainbow Hat we need minimum sdk version 27 in build.gradle(Module: app) file.

minSdkVersion 27

next, we have to specify the app permission in AndroidManifest.xml file.

<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />

next, we have to import the driver in our MainActivity.java file

import com.google.android.things.contrib.driver.rainbowhat.RainbowHat;

Now, we will see the code.

This is our MainActivity.java

package com.sodiarc.rainbowhat1;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import com.google.android.things.contrib.driver.rainbowhat.RainbowHat;
import com.google.android.things.pio.Gpio;

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Gpio redLed = null, greenLed = null, blueLed = null;
    private boolean toggle = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Opening the LEDs 
        try {
            redLed = RainbowHat.openLedRed();
            greenLed = RainbowHat.openLedGreen();
            blueLed = RainbowHat.openLedBlue();
            blinkLed();
        } catch (Exception e){
            Log.d("Exception 1: ", e.getMessage());
        }
    }
    //This method is for setting the value of LED
    public void blinkLed(){
        //This anonymous class will run after every 500 mili seconds
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toggle = !toggle;
                try {
                    redLed.setValue(toggle);
                    blueLed.setValue(toggle);
                    greenLed.setValue(toggle);
                    blinkToggle();
                }catch (Exception e){
                    Log.d("Exception 2: ", e.getMessage());
                }
            }
        },500);
    }

    public void blinkToggle() throws Exception{
        blinkLed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

Rainbow Hat

So, if you have any doubt, please let us know in the comment section, thank you.

Android Things Article Author

Author

Hi folks, I am co-founder and CEO at Sodiarc Technologies Pvt. Ltd. I am working on android for more than 5 years. I have skills, which includes Android, iOS, PHP and lot more. If you have any project related queries we can discuss it briefly, mail me at info@sodiarc.com

Android Things Article Author

Author

Hi folks, I am CTO at Sodiarc Technologies Pvt. Ltd. I am programming enthusiast. I have skills, which includes Android, iOS, PHP and lot more. If you have any project related queries we can discuss it briefly, mail me at info@sodiarc.com

Android Things Article Author

Author

Hi folks, I am a content writer and a blogger. Writing is my passion. A B.Tech graduate in Information Technology, who is still paving the way for achieving the goals of her life.


Website Security Test