Hallo zusammen, ich komme bei meinem kleinen Projekt nicht weiter.
Ich würde gerne ein RC-Auto auf Arduino Steuerung umbauen, d.h. kleinen Elektromotor an die Hinterachse für Vorwärts/ Rückwärts und an der Vorderachse ein Servo Motor für die Lenkung. Dieser soll erstmal mit einem Wii Nunchuck per Beschleunigungssensor und den hinteren Tasten "C" und "Z" funktionieren.
Da ich nur ein Gelegenheitsbastler bin versuche ich aus verschiedenen Codes aus dem Web mein Ziel zu erreichen.
Hatte dann ein funktionierenden Code für Vorwärts/ Rückwärts und einen für die Lenkung. Wenn ich diese aber in einen Code verpacke läuft die Lenkung wie gewünscht aber an der Hinterachse zur Beschleunigung kommt nichts.
Da ich wirklich nicht so erfahren in Sachen Programmierung bin hoffe ich, dass ich einfach ein kleinen Fehler übersehen habe. Über jeden Tipp oder jeder Hilfe wäre ich sehr dankbar.
#include <Wire.h>
#include <Servo.h>
#include <wiinunchuck.h>
Servo servo2;
#define ZEROX 530
#define ZEROY 530
#define ZEROZ 530
#define WII_NUNCHUK_I2C_ADDRESS 0x52
int counter;
uint8_t data[6];
const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
const int enablePin = 9; // connected to pin 1 on the H-bridge
const int directionSwitchPin = 4; // connected to the switch for direction
const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
const int potPin = A0; // connected to the potentiometer's output
int potVal;
int switch1Val;
int switch2Val;
// create some variables to hold values from your inputs
int onOffSwitchState = 0; // current state of the on/off switch
int previousOnOffSwitchState = 0; // previous position of the on/off switch
int directionSwitchState = 0; // current state of the direction switch
int previousDirectionSwitchState = 0; // previous state of the direction switch
int motorEnabled = 0; // Turns the motor on/off
int motorSpeed = 0; // speed of the motor
int motorDirection = 1; // current direction of the motor
void setup() {
//Nunchuck lenkung
servo2.attach(12);
Wire.begin();
Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
Wire.write(0xF0);
Wire.write(0x55);
Wire.endTransmission();
Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
Wire.write(0xFB);
Wire.write(0x00);
Wire.endTransmission();
// initialize the inputs and outputs
pinMode(directionSwitchPin, INPUT);
pinMode(onOffSwitchStateSwitchPin, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// pull the enable pin LOW to start
digitalWrite(enablePin, LOW);
Serial.begin(9600);
nunchuk_init();
delay(100);
nunchuk_calibrate_joy();
delay(100);
nunchuk_get_data();
delay(100);
}
void loop() {
//Nunchuck lenkung
Wire.requestFrom(WII_NUNCHUK_I2C_ADDRESS, 6);
counter = 0;
while(Wire.available())
{
data[counter++] = Wire.read();
}
Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
if(counter >= 5)
{
double accelX = ((data[2] << 2) + ((data[5] >> 2) & 0x03) - ZEROX);
double accelY = ((data[3] << 2) + ((data[5] >> 4) & 0x03) - ZEROY);
double accelZ = ((data[4] << 2) + ((data[5] >> 6) & 0x03) - ZEROZ);
int value = constrain(accelY, -180, 180);
value = map(value, -180, 180, 0, 180);
value = constrain(accelX, -90, 90);
value = map(value, -180, 180, 0, 180);
servo2.write(value);
delay(20);
}
nunchuk_get_data(); // Daten vom Nunchuk empfangen
delay(10);
switch1Val = digitalRead(4);
switch2Val = digitalRead(5);
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
Serial.print("Switch1: ");
Serial.print(switch1Val);
Serial.print("Switch2: ");
Serial.println(switch2Val);
// read the value of the pot and divide by 4 to get a value that can be
// used for PWM
motorSpeed = analogRead(potPin) / 4;
// change the direction the motor spins by talking to the control pins
// on the H-Bridge
if (nunchuk_cbutton() == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
} else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
// if the motor is supposed to be on
if (nunchuk_zbutton() == 1) {
// PWM the enable pin to vary the speed
analogWrite(enablePin, motorSpeed);
} else { // if the motor is not supposed to be on
//turn the motor off
analogWrite(enablePin, 0);
}
}