Ich versuche mit einem arduino seeeduino atmega168 4 Motoren anzusteuern (Signal kommt von einer selbst geschriebenen APP) Zuerst sollten sich die Motoren alle gleich schnell drehen - Kein Problem Jetzt ist eine Art Steuerung dazugekommen Folgendes Prinzip: Das Signal besteht aus 3 Zahlen Dies letzte Zahl ist die Richtung der Bewegung (1 Vor; 2 Zurück; 3 Links; 4 Rechts; ) die forderen 2 Zahlen geben die Beschleunigung an BSP: 51; Vor - Stärke 5 73; Links - Stärke 7 Ich versuche generell die Drehzahl rauszuschreiben und wenn ein Motor schneller Drehen soll, einfach die Stärke dazu zu addieren Anfangs Drehen sich die Motoren kurz und dann steht alles still?
1 | #include <SoftwareSerial.h> |
2 | |
3 | int bluetoothTx = 7; |
4 | int bluetoothRx = 5; |
5 | |
6 | int M1 = 3; // Vorne Links |
7 | int M2 = 9; // Vorne Rechts |
8 | int M3 = 10; // Hinten Links |
9 | int M4 = 11; // Hinten Rechts |
10 | |
11 | int zdM1=0; |
12 | int zdM2=0; |
13 | int zdM3=0; |
14 | int zdM4=0; |
15 | |
16 | int x1 = 0; |
17 | int x2 = 0; |
18 | int x3 = 0; |
19 | int x4 = 0; |
20 | |
21 | int b; |
22 | int bWert; |
23 | |
24 | int x = 0; |
25 | int befehlsWert = 0; |
26 | int befehl = 0; // 0 - Drehzahl |
27 | // 1 - Vorwärts
|
28 | // 2 - Rückwärts
|
29 | // 3 - Links
|
30 | // 4 - Rechts
|
31 | |
32 | // 5 - Drehen Links
|
33 | // 6 - Drehen Rechts
|
34 | // 7 - KameraSerbo
|
35 | |
36 | SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); |
37 | |
38 | void setup() |
39 | {
|
40 | |
41 | //Setup usb serial connection to computer
|
42 | Serial.begin(9600); |
43 | |
44 | //Setup Bluetooth serial connection to android
|
45 | bluetooth.begin(115200); |
46 | bluetooth.print("$$$"); |
47 | delay(100); |
48 | bluetooth.println("U,9600,N"); |
49 | |
50 | bluetooth.begin(9600); |
51 | }
|
52 | |
53 | |
54 | void loop() |
55 | {
|
56 | //Read from bluetooth and write to usb serial
|
57 | if(bluetooth.available()) |
58 | {
|
59 | char toSend = (char)bluetooth.read(); |
60 | x = toSend; |
61 | if(x < 0) // Korrektur des empfangenen Signal |
62 | {
|
63 | x = 255 + x; |
64 | }
|
65 | if(x>120) |
66 | {
|
67 | x = x+1; |
68 | }
|
69 | |
70 | if(x>0) |
71 | {
|
72 | |
73 | //infoZerlegen(x);
|
74 | //Serial.println(x);
|
75 | |
76 | b = x % 10; |
77 | bWert = x/10; |
78 | //Serial.println(b);
|
79 | if(b == 0){x = bWert*10;} //GAS |
80 | if(b == 1){zdM3 = bWert; zdM4 = bWert;} //Vor |
81 | if(b == 2){zdM1 = bWert; zdM2 = bWert;} //Zurueck |
82 | if(b == 3){zdM2 = bWert; zdM4 = bWert;} //Links |
83 | if(b == 4){zdM1 = bWert; zdM3 = bWert;} //Rechts |
84 | |
85 | x1 = x + zdM1; |
86 | x2 = x + zdM2; |
87 | x3 = x + zdM3; |
88 | x4 = x + zdM4; |
89 | |
90 | analogWrite(M1,x1); |
91 | analogWrite(M2,x1); |
92 | analogWrite(M3,x1); |
93 | analogWrite(M4,x1); |
94 | |
95 | |
96 | |
97 | }
|
98 | |
99 | delay(100); |
100 | }
|
101 | |
102 | //Read from usb serial to bluetooth
|
103 | if(Serial.available()) |
104 | {
|
105 | |
106 | |
107 | char toSend = (char)Serial.read(); |
108 | bluetooth.print(toSend); |
109 | }
|
110 | }
|