Danke für eure Hilfe aber leider bin ich noch nicht zum gewünschten ziel gekommen. Ich bin mit programmieren leider noch nicht so vertraut.
@sschultewolter baud hab ich auf 9600 reduziert und bei den bluetooth modulen mit AT+UART=9600,0,0 gesetzt.
@serenifly
Ich habe mich dazu entschlossen es mit deiner String variante zu versuchen aber iwas mach ich falsch.
im serial monitor kommt immer nur:
00
0
0
0
0
0
,0
0
0
0
0
0
Wenn ich deinen teil auskommentiere und nur mehr: if (BTSerial.available()) {
Serial.write(BTSerial.read()); verwende bekomme ich das über den serial.
Wo jetzt aufeinmal die letzen Zahlen(44787) herkommen weis ich auch nicht.
0,0,0,0,48,44787,
0,0,0,0,48,45287,
0,0,0,0,48,45787,
0,0,0,0,48,46287,
0,0,0,0,48,46787,
Der ganze Master Code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const int SERIAL_BUFFER_SIZE = 30;
const int MAX_VALUES = 6;
char serialBuffer[SERIAL_BUFFER_SIZE];
int values[MAX_VALUES];
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(9600); // HC-05 default speed in AT command more
}
void loop()
{
if (BTSerial.available()) {
// Keep reading from HC-05 and send to Arduino Serial Monitor
delay(50);
Serial.write(BTSerial.read());
//empfangeneDaten = BTSerial.read();
//Serial.write(empfangeneDaten);
parseSerial();
for (int i = 0; i < MAX_VALUES; i++)
Serial.println(values[i]);
Serial.println("---");
}
}
bool readSerial(Stream& stream)
{
static byte index;
while (BTSerial.available())
{
char c = BTSerial.read();
if (c >= 32 && index < SERIAL_BUFFER_SIZE - 1)
{
serialBuffer[index++] = c;
}
else if (c == '\n' && index > 0)
{
serialBuffer[index] = '\0';
index = 0;
return true;
}
}
return false;
}
void parseSerial()
{
int Sensor1 = atoi(strtok(serialBuffer, ","));
int Sensor2 = atoi(strtok(NULL, ","));
int Sensor3 = atoi(strtok(NULL, ","));
int Sensor4 = atoi(strtok(NULL, ","));
int Sensor5 = atoi(strtok(NULL, ","));
}
Der ganze Slave Code:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(12, 11); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//
char c = ' ';
// ---------------------------------------------------------------------------
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
// would normally process the sensor results in this function (for example, decide if a robot needs to
// turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define SONAR_NUM 5 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 100 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(2, 3, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(4, 5, MAX_DISTANCE),
NewPing(6, 7, MAX_DISTANCE),
NewPing(8, 9, MAX_DISTANCE),
NewPing(10, 13, MAX_DISTANCE),
};
void setup() {
Serial.begin(115200);
Serial.println("Enter AT commands:");
BTserial.begin(9600);
//Sensoren
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
//delay(1000);
// Other code that *DOESN'T* analyze ping results can go here.
}
void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
// The following code would be replaced with your code that does something with the ping results.
//Serial.print('<');
//BTserial.print('<');
for (uint8_t i = 0; i - 1 < SONAR_NUM; i++) {
//Serial.print(i);
//BTserial.println(i);
Serial.print(cm[i]);
BTserial.print(cm[i]);
Serial.print(",");
BTserial.print(",");
delay(100);
}
Serial.println();
BTserial.println();
}
Vielleicht könnt ihr mir anhamd davon sagen wo das Problem liegt. Ich bin echt ratlos, aber kann ja nur besser werden =)
Danke.