I’m new to Arduino and tried to edit a code I downloaded to make an RC Car with RF 433 modules and a joystick, but it doesn’t work.
This is the code:
#include <VirtualWire.h>
const int receive_pin = 11;
String message;
String myString;
// para el joystick
int xvalue;
int yvalue;
int buttonf;
int buttonl;
int xdata;
int ydata;
int bfdata;
int bldata;
// para el driver de motores
int BIA = 3;
int AIA = 6;
int BIB = 5;
int AIB = 9;
int velocidadr;
int velocidadi;
int reversar;
int reversai;
// para el led y el buffer
int led;
int buf;
int ledp;
void setup()
{
delay(1000);
pinMode(AIA, OUTPUT);
pinMode(AIB, OUTPUT);
pinMode(BIA, OUTPUT);
pinMode(BIB, OUTPUT);
pinMode(buf, INPUT);
pinMode(ledp, INPUT);
analogWrite(AIA, 0);
analogWrite(AIB, 0);
analogWrite(BIA, 0);
analogWrite(BIB, 0);
delay(1000);
// iniciar módulo rf
vw_set_rx_pin(receive_pin);
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
int i;
for (i = 0; i < buflen; i++)
{
char c = (buf[i]);
message = message + c; // hacer un mensaje con los carácteres recibidos
}
myString = message;
String l = getValue(myString, ',', 0); // xvalue
String m = getValue(myString, ',', 1); // yvalue
String n = getValue(myString, ',', 2); // buttonf
String k = getValue(myString, ',', 3); // buttonl
xdata = l.toInt();
ydata = m.toInt();
bfdata = n.toInt();
bldata = k.toInt();
// parado
if ( ((xdata >= 480)&&(xdata <= 510)) && ( (ydata>=480) && (ydata<=510) ) )
{
velocidadr = 0;
velocidadi = 0;
}
// si está apretado el botón del buffer
if (bfdata == 0)
{
digitalWrite{buf, HIGH};
}else if (bfdata == 1)
{
digitalWrite(buf, LOW);
}
// si está apretado el botón del led
if (led == 1)
{
digitalWrite(ledp, HIGH);
}else if (led == 0)
{
digitalWrite(ledp, LOW);
}
if (bldata == 0)
{
if (led == 0)
{
led = 1;
} else if (led == 1)
{
led = 0;
}
}
// adelante
if ((xdata > 510)&&(xdata <= 1023))
{
int xmapped = map(xmapped, xdata, 1023, 255,0);
velocidadr = xmapped;
velocidadi = xmapped;
reversar = 0;
reversai = 0;
}
// atras
if ((xdata >= 0)&&(xdata < 480));
{
int xmapped = map(xmapped, xdata, 479, 255,0);
velocidadr = 0;
velocidadi = 0;
reversar = xmapped;
reversai = xmapped;
}
// derecha
if ((ydata> 510)&&(ydata <= 1023));
{
int ymapped = map(ymapped, ydata, 1023, 255,0);
velocidadr = velocidadr- ymapped;
reversar = reversar - ymapped;
}
// izquierda
if ((ydata >= 0)&&(ydata <= 479));
{
int ymapped = map(ymapped, ydata, 479, 255,0);
velocidadi = velocidadi - ymapped;
reversai = reversai - ymapped;
}
// eliminar negativos reversa
if (reversai < 0)
{
reversai = 0;
}
if (reversar < 0)
{
reversar = 0;
}
// eliminar negativos velocidad
if (velocidadi < 0)
{
velocidadi = 0;
}
if (velocidadr < 0)
{
velocidadr = 0;
}
int velocidadt = velocidadr + velocidadi;
int reversat = reversar + reversai;
// comenzar movimniento
// en reversa
if (reversat > 0)
{
analogWrite(AIA, reversai);
analogWrite(AIB, 0);
analogWrite(BIA, reversar);
analogWrite(BIB, 0);
}else if (velocidadt > 0)
{
analogWrite(AIA, 0);
analogWrite(AIB, velocidadi);
analogWrite(BIA, 0);
analogWrite(BIB, velocidadr);
} else
{
analogWrite(AIA, 0);
analogWrite(AIB, 0);
analogWrite(BIA, 0);
analogWrite(BIB, 0);
}
message = "";
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
The error is:
error: expected ';' before '{' token
And many warnings