Controlling 8 LEDs with Android phone
Controlling 8 LEDs with Android phone
Controlling 8 LEDs with Android phone

Controlling 8 LEDs with Android phone

In this application, we control 8 LEDs with an Android program written in Flutter-Dart codes. We are sending data to bluetooth module using Flutter-Dart, slider object.


On the microcontroller side, these data received by the bluetooth module are converted into digital data and we send a signal to the leds.


At the beginning of our training video, we explain how to set the Master/Slave of the Bluetooth module.


The data obtained with the Myserial.readString function from the microcontroller codes is of string type. The value of this string type led_count=rec_data.toInt(); We convert it to an integer value with the function. For example, if the value read from the Bluetooth module is "6", this numeric is converted to 6. With the for loop, the value "HIGH", that is, "1", is sent to the pins from 1 to 6.

In the Flutter-Dart coding part, the value from the Slider object is sent with the basic functions "sendData()". The sendData() function runs whenever the value of the Slider object changes.

 

Flutter-Dart Codes are given below:

import 'dart:async';
//import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';


void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<BluetoothDevice> _devices = [];
  late BluetoothConnection connection;
  String adr="00:21:07:00:50:69"; // my bluetooth device MAC Adres
  double sval = 1;
  String leds="--";
  late int ledint=0;

  @override
  void initState() {
    super.initState();
    _loadDevices();
  }

  Future<void> _loadDevices() async {
    List<BluetoothDevice> devices = await FlutterBluetoothSerial.instance.getBondedDevices();

    setState(() {   _devices = devices;    });
  }
//----------------------------
  Future<void> sendData(String data)  async {

    data = data.trim();
    try {
      List<int> list = data.codeUnits;
      Uint8List bytes = Uint8List.fromList(list);
      connection.output.add(bytes);
      await connection.output.allSent;
      if (kDebugMode) {
        // print('Data sent successfully');
      }
    } catch (e) {
      //print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Array leds level control"),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text("MAC Adress: 00:21:07:00:50:69"),

                ElevatedButton(child:Text("Connect"),onPressed: () {
                  connect(adr);

                },),
                SizedBox(height: 30.0,),

            Slider(
              min: 1, //add min and max
              max: 8,
              value: sval,


            onChanged: (double value) {
              //by default value will be range from 0-1
              setState(() {
                sval = value;
              });
              ledint=sval.toInt();  // double to int
              leds=ledint.toString();  // int to string
              sendData(leds);    // send to bluetooth module
            },
          ),

                Text("Leds:1---> " + leds,style: const TextStyle(fontSize: 50,color: Colors.green),),

                const SizedBox(height: 30.0,),
              ],
            ),
          ),






        )

    );
  }

  Future connect(String address) async {
    try {
      connection = await BluetoothConnection.toAddress(address);
      sendData('111');
      //durum="Connected to the device";
      connection.input!.listen((Uint8List data) {
        //Data entry point
        // durum=ascii.decode(data);
      });



    } catch (exception) {
      // durum="Cannot connect, exception occured";
    }
  }

// --------------**************data gonder
//Future send(Uint8List data) async {
//connection.output.add(data);
//await connection.output.allSent;
}
//------------*********** data gonder end

Microcontroller codes are given below:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
String rec_data="-";
int led_count=0;

void setup() {
   pinMode(8, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(1, OUTPUT);
   
  Serial.begin(9600);
  mySerial.begin(9600);   // BlueTooth Data baud,set the data rate for the SoftwareSerial port
}

void loop() { // run over and over
  if (mySerial.available()) {
    
     rec_data=mySerial.readString();
               led_count=rec_data.toInt();
     
     for(int s=1;s<=8;s++)
               {digitalWrite(s, LOW);}
       
               
       for(int s=1;s<=led_count;s++)
               {digitalWrite(s, HIGH);}

       delay(1);
       
   Serial.println(rec_data);
    
  }
  
}