HC-05 Bluetooth Modülü İle Elektronik Org Yapımı
HC-05 Bluetooth Modülü İle Elektronik Org Yapımı
HC-05 Bluetooth Modülü İle Elektronik Org Yapımı

HC-05 Bluetooth Modülü İle Elektronik Org Yapımı

Bu uygulama bir elektronik org uygulamasıdır. Android telefon veya tablet için kodları Flutter-Dart kodlarıyla yazdık. Bluetooth modülünün bağlı olduğu Arduino için de buzzer’dan ses çıkaran kodları yazdık.

Android uygulamamızda temel notalar için sekiz adet buton var. Bir de bluetooth modüle bağlantı sağlayan “connect ” butonu var.

Flutter-Dart kodları aşağıda:

//  bluetooth ORG with Arduino-------------------------
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

  @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("Electronic ORG width 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,),

                Row(
                  children: [
                    ElevatedButton(child:Text(" DO "),onPressed: () {
                      sendData("Do");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text("RE "),onPressed: () {
                      sendData("Re");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text(" MI "),onPressed: () {
                      sendData("Mi");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text(" FA "),onPressed: () {
                      sendData("Fa");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text(" SOL "),onPressed: () {
                      sendData("Sol");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text(" LA "),onPressed: () {
                      sendData("La");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text(" SI "),onPressed: () {
                      sendData("Si");
                    },),
                    SizedBox(width: 4.0,),

                    ElevatedButton(child:Text(" DOO "),onPressed: () {
                      sendData("Do2");
                    },),
                    SizedBox(width: 4.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 kodları aşağıda:

 

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
String rec_data="off";
int buzzer=2; //Uno digital out pin number
//freq------------------------------
int Do=261; int Re=294; int Mi=329; int Fa=349; int Sol=392; int La=440; int Si=493; int Do2=523;

void setup() {
  
   pinMode(buzzer,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();

    if(rec_data=="Do") {tone(buzzer,Do); delay(300); noTone(buzzer); delay(20); }
    if(rec_data=="Re") {tone(buzzer,Re); delay(300); noTone(buzzer); delay(20); }
    if(rec_data=="Mi") {tone(buzzer,Mi); delay(300); noTone(buzzer); delay(20); }
    if(rec_data=="Fa") {tone(buzzer,Fa); delay(300); noTone(buzzer); delay(20); }
    if(rec_data=="Sol") {tone(buzzer,Sol); delay(300); noTone(buzzer); delay(20); }
    if(rec_data=="La") {tone(buzzer,La); delay(300); noTone(buzzer); delay(20); }
    if(rec_data=="Si") {tone(buzzer,Si); delay(300); noTone(buzzer); delay(20); }
     if(rec_data=="Do2") {tone(buzzer,Do2); delay(300); noTone(buzzer); delay(20); }
    
    
    Serial.println(rec_data);
    delay(10);
  }
  
}

Elektronik org uygulaması ile ilgili videomuzu izleyebilirsiniz: Electronic Organ with Android