/*Code zur Steuerung eines RC Cars mit DC-Motor mithilfe einer Fernbedienung
Erstellt für T1000 TMB21 DHBW Mannheim
PFW Aerospace GmbH Speyer
programmiert von Lars Peters*/
#include <VirtualWire.h>
#include <ServoTimer2.h>
#define motorpin1 13
#define motorpin2 12
#define motorpinspeed 6
#undef int
#undef abs
#undef double
#undef float
#undef round
ServoTimer2 lenkung;
void setup()
{
Serial.begin(9600);
pinMode(motorpinspeed, OUTPUT);
lenkung.attach(9);
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(2); // We will be receiving on pin 2 i.e the RX pin from the module connects to this pin.
vw_rx_start(); // Start the receiver
}
void loop()
{
int values[3];
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // check to see if anything has been received
{
for (int i = 0; i < 3; i++)
{
values[i] = word(buf[i*2+1], buf[i*2]);
Serial.print("values[");
Serial.print(i, DEC);
Serial.print("]=");
Serial.print(values[i], DEC);
Serial.print(" ");
}
Serial.println();
}
int geschw = map(values[0], 510,1023,0,255); //Umrechnen der Joystick-Werte in PWM-Wert für Geschwindigkeit
int winkel = map(values[1], 510,1023,1500,2250); //Umrechnen in Winkel für Lenkservo
//Serial.println(winkel);
lenkung.write(winkel);
if(geschw>50)
{
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
analogWrite(motorpinspeed, geschw); //Motor mit mind. 50 PWM, sonst ungleichmäßiger Lauf
}
else if(geschw<-50)
{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
analogWrite(motorpinspeed, abs(geschw));
}
else
{
analogWrite(motorpinspeed, 0);
}
}