Hallo,
Ich habe ein kleines Problem, ich möchte über meine Handbewegung ein ferngesteuertes Auto steuern.
Dazu habe ich in einen Handschuh einen Arduino-nano, eine 9V Batterie, ein GY-521 Module und ein nRF24L01+ Modul eingebaut.
An sich funktioniert alles, Beispielprogramme zum senden und empfangen der Daten, und im Seriellen Monitor die Lageerkennung der Hand.
Mir stellt sich nur folgendes Problem bei der Programmierung, ich weiß nicht wie ich die nachricht über eine “if”-schleife auslesen lassen kann.
Code des Handschuhs:
(#include "Wire.h"
#include "SPI.h"
#include "RF24.h"
static const uint64_t pipes[6] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0C3LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL};
float rechts = 10;
float gerade = 20;
float links = 30;
float vor = 40;
float stehen = 50;
float zurueck = 60;
bool radioNumber = 1;
RF24 radio(6,7);
byte addresses[][6] = {"1Node","2Node"};
const int MPU = 0x68;
int16_t acc_x, acc_y, acc_z;
int16_t gyro_x, gyro_y, gyro_z;
int16_t temp;
char tmp_str[7];
char* convert_int16_to_str(int16_t i)
{
sprintf(tmp_str, "%6d", i);
return tmp_str;
}
void setup()
{
radio.begin();
delay(20);
radio.setChannel(1);
radio.setAutoAck(0);
radio.setRetries(15,15);
radio.setPALevel(RF24_PA_HIGH);
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
radio.begin();
Serial.println("checking if chip connected");
bool check = radio.isChipConnected();
Serial.print("check-");
Serial.println(check);
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
radio.startListening();
}
void loop()
{
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 7*2, true);
acc_x = Wire.read()<<8 | Wire.read();
acc_y = Wire.read()<<8 | Wire.read();
acc_z = Wire.read()<<8 | Wire.read();
temp = Wire.read()<<8 | Wire.read();
gyro_x = Wire.read()<<8 | Wire.read();
gyro_y = Wire.read()<<8 | Wire.read();
gyro_z = Wire.read()<<8 | Wire.read();
//Serial.print(" | aX = "); Serial.print(convert_int16_to_str(acc_x));
//Serial.print(" | aY = "); Serial.print(convert_int16_to_str(acc_y));
//Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(acc_z));
//Serial.print(" | tmp = "); Serial.print(temp/340.00+36.53);
//Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
//Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
//Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
//Serial.println();
long message[6] = {long(rechts*100),long(gerade*100),long(links*100),long(vor*100),long(stehen*100),long(zurueck*100)};
radio.stopListening();
radio.write(&message, sizeof(message));
radio.startListening();
if (acc_x > 5300)
{
rechts++;
Serial.print("| rechts |");
}
if(acc_x < 5300)
{
gerade++;
Serial.print("| gradeaus |");
}
if(acc_x < -5300)
{
links++;
Serial.print("| links |");
}
Serial.print(" ][ ");
if (acc_y > 5300)
{
zurueck++;
Serial.print("| zurück |");
}
if(acc_y < 5300)
{
stehen++;
Serial.print("| stop |");
}
if(acc_y < -5300)
{
vor++;
Serial.print("| vor |");
}
Serial.println();
delay(100);
}
)
________________________
Code des Autos(unfertig):
________________________
[code]#include <Servo.h>
#include "SPI.h"
#include "RF24.h"
static const uint64_t pipes[6] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0C3LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL};
uint8_t ClientNummer;
float rechts, gerade, links, vor, stehen, zurueck;
Servo gas;
bool radioNumber = 0;
RF24 radio(6,7);
byte addresses[][6] = {"1Node","2Node"};
int state = 0;
int state2 = 180;
int state3 = 90;
void setup()
{
radio.begin();
delay(20);
radio.setChannel(1);
radio.setAutoAck(0);
radio.setPALevel(RF24_PA_HIGH);
radio.openReadingPipe(0,pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.openReadingPipe(2,pipes[2]);
radio.openReadingPipe(3,pipes[3]);
radio.openReadingPipe(4,pipes[4]);
radio.openReadingPipe(5,pipes[5]);
//servo
gas.attach(3);
Serial.begin(9600);
Serial.println("checking if chip connected");
bool check = radio.isChipConnected();
Serial.print("check-");
Serial.println(check);
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
radio.startListening();
}
void loop() {
if(radio.available(&ClientNummer))
{
long got_message[6] = {0,0,0,0,0,0};
bool done = false;
while(!done)
{
radio.read(&got_message, sizeof(got_message));
}
rechts = (float(got_message[0])/100);
links = (float(got_message[1])/100);
gerade = (float(got_message[2])/100);
vor = (float(got_message[3])/100);
stehen = (float(got_message[4])/100);
zurueck = (float(got_message[5])/100);
}
}