get value from bluetooth HC05 in order to control an engine

Dependencies:   mbed BluetoothSerial

https://os.mbed.com/media/uploads/chamo/resultat.png

This little prog link a simple engine to an android application. This application was designed on MIT AppInventor.

Here is the sketch

https://os.mbed.com/media/uploads/chamo/schema.png

Committer:
chamo
Date:
Thu Mar 11 15:50:53 2021 +0000
Revision:
0:01d1cf42e360
prog 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chamo 0:01d1cf42e360 1 #include "mbed.h"
chamo 0:01d1cf42e360 2 #include <cstdlib> //librairie pour convertir char en float (libraby that convert char into float)
chamo 0:01d1cf42e360 3 #include <string>
chamo 0:01d1cf42e360 4
chamo 0:01d1cf42e360 5 Serial tel(D8, D2); //module bluetooth HC-05 branché à D8 (TX) et D2 (RX) (Bluetooth Hc05, Tx is link to D8 pin and Rx to D2)
chamo 0:01d1cf42e360 6 DigitalOut moteur(D5); //broche du moteur (Engine to D5 pin)
chamo 0:01d1cf42e360 7 DigitalIn bouton(PC_13); //broche du bouton (if you want to use a button condition, it's PC_13 pin)
chamo 0:01d1cf42e360 8 PwmOut pwm(D11); //broche de la PWM (PWM is on D11 pin)
chamo 0:01d1cf42e360 9
chamo 0:01d1cf42e360 10
chamo 0:01d1cf42e360 11 int main()
chamo 0:01d1cf42e360 12 {
chamo 0:01d1cf42e360 13 float puissance;
chamo 0:01d1cf42e360 14 while(1) {
chamo 0:01d1cf42e360 15 if(tel.readable()) { //Si on peut utiliser le bluetooth (if Bluetooth is readable)
chamo 0:01d1cf42e360 16 tel.baud(9600); //FrameRate du bluetooth (Bluetooth framerate)
chamo 0:01d1cf42e360 17 puissance = tel.getc(); //récupère l'info en char (save as char what the bluetooth module got)
chamo 0:01d1cf42e360 18 printf("puissance: %f \n",puissance); //test (just a test to know the power between 1 and 255)
chamo 0:01d1cf42e360 19 pwm.write(puissance/255); //Info du bluetooth en char que l'on applique a la pwm (dividing power by 255 in order to have a pwm ratio)
chamo 0:01d1cf42e360 20 moteur=1; //On lance le moteur (starting engine)
chamo 0:01d1cf42e360 21 }
chamo 0:01d1cf42e360 22 }
chamo 0:01d1cf42e360 23 }