Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
CAVEDU EducationDFRobot
Published © GPL3+

Control RGB LED by Dragging – Arduino 101 & App Inventor

This topic will teach you how to control a RGB LED on an Arduino 101 board with an Android device (MIT App Inventor).

IntermediateFull instructions provided3 hours17,157
Control RGB LED by Dragging – Arduino 101 & App Inventor

Things used in this project

Hardware components

Arduino 101
Arduino 101
×1
DFRobot RGB LED breakout
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
DFRobot Gravity: IO Expansion Shield
×1

Software apps and online services

Arduino IDE
Arduino IDE
App Inventor

Story

Read more

Code

App Inventor

Scratch
Ger RGB data of the touch point, then send to Arduino 101. sorry no item for App Inventor .aia file~
No preview (download only).

Arduino 101

Arduino
Receiving data from Android phone and change the RGB LED light intensity.
#include <CurieBLE.h>
#include <stdlib.h>
#define LEDr 9
#define LEDg 6
#define LEDb 3

BLEPeripheral blePeripheral;  
//BLE Peripheral Device (the board you're programming)
BLEService ControlLED("19B10010-E8F2-537E-4F6C-D104768A1214"); 
//initialize a BLE Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedIntCharacteristic LEDStatus("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite  );

int incom = 0;
int r, g, b ;

void setup() {
  Serial.begin(9600);

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("ControlLED");
  blePeripheral.setAdvertisedServiceUuid(ControlLED.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(ControlLED);
  blePeripheral.addAttribute(LEDStatus);

  // begin advertising BLE Light service:
  blePeripheral.begin();

  Serial.println("BLE RGBLED control.");

  // set Light pin to output mode
  pinMode(LEDr, OUTPUT);
  pinMode(LEDg, OUTPUT);
  pinMode(LEDb, OUTPUT);
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      //Serial.println(LEDStatus.written());
      if (LEDStatus.written())
      {
        incom = LEDStatus.value();//take 110225101 for exmaple
        r = incom / 1000000 ;//110
        g = (incom / 1000 - r * 1000) ; //110225-110000=225
        b = (incom - r * 1000000 - g * 1000) ; //110225101-110000000-2250000=101
        Serial.println(incom);
        Serial.println(r);
        Serial.println(g);
        Serial.println(b);  //show RGB data on serial monitor
        analogWrite(LEDr, r);
        analogWrite(LEDg, g);
        analogWrite(LEDb, b); //Light up LED
        delay(10);
      }
    }
    digitalWrite(LEDr, LOW);
    digitalWrite(LEDg, LOW);
    digitalWrite(LEDb, LOW);  //LED turn off
    delay(100);
  }

  // when the central disconnects, print it out:
  Serial.print(F("Disconnected from central: "));
  Serial.println(central.address());
}

Credits

CAVEDU Education

CAVEDU Education

11 projects • 28 followers
We provide tutorials of robotics, Arduino, Raspberry Pi and IoT topics. http://www.cavedu.com
DFRobot

DFRobot

66 projects • 147 followers
Empowering Creation for Future Innovators

Comments