DC Motor Speed Control by Android phone
DC Motor Speed Control by Android phone

DC Motor Speed Control by Android phone

This application we made with Flutter-Dart codes is a DC Motor speed control application. Using the Slider widget, we generate analog values between 0 and 250 and send this value to the bluetooth adapter with the sendData function.

 

 

On the microcontroller side, rec_data=mySerial.readString(); We read the line. We convert this value to integer type and send it to DC motor with AnalogWrite command.

Flutter-Dart Codes: 

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 = 10;
  String mots="0";
  late int motint=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("DC MOTOR CONTROL with bluetooth"),
          ),
          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: 5, //add min and max
                  max: 250,
                  value: sval,


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

                Text("Motor Level 1---> " + mots,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

Arduino Codes:


#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
String rec_data="5";
int motor_level=10;

void setup() {
   pinMode(3, OUTPUT);   // for Analog 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();
               motor_level=rec_data.toInt();
     
      analogWrite(3, motor_level);

     delay(1000);
       
   Serial.println(motor_level);
    
  }
  
}

Industrial IoT Applications:

Norvi Industrial IoT Applications