1#include<SoftwareSerial.h>
2SoftwareSerial bt_ser(4,5);
3 to RX and TX pins for serial data communication
4char c[6];
5int i=0,speed_value=0,send_value;
6
7#define
8 pwm1 9
9#define pwm2 10
10
11boolean motor_dir
12 = 0;
13
14void setup()
15{
16 Serial.begin(9600);
17 bt_ser.begin(9600);
18
19 pinMode(pwm1, OUTPUT);
20 pinMode(pwm2, OUTPUT);
21}
22
23void loop()
24{
25
26
27 while(bt_ser.available())
28 {
29 if(bt_ser.available()>0)
30
31 {
32 c[i] = bt_ser.read();
33 device
34 Serial.print(c[i]);
35 i++;
36 }
37 if(c[i-1]=='N')
38
39 {
40 motor_dir = !motor_dir;
41 direction variable
42 if(motor_dir)
43 pwm1 and pwm2 are opposites
44 digitalWrite(pwm2, 0);
45 else
46
47 digitalWrite(pwm1, 0);
48 }
49 }
50
51 speed_value
52 = (c[1]-48)*100+(c[2]-48)*10+(c[3]-48)*1;
53
54 if(motor_dir)
55 {
56 if(c[i-1]=='#'){
57
58
59 analogWrite(pwm1,
60 speed_value-100);
61 i=0;
62
63 }
64 }
65 else{
66
67 if(c[i-1]=='#'){
68
69 analogWrite(pwm2, speed_value-100);
70
71 i=0;
72 }
73
74 }
75
76
77}
78