Stap 1: Code voor Robot - reciver of RX
//***********************
2 motoren en Joystik
motoren op motor sheld
//**********************
Libbrerie e costanti
#include < VirtualWire.h >
PIN voor geleid
#define LED_pin4 4 / / avanti GIALLO go geel
#define LED_pin5 5 / / rug BLU BLU bo indietro
#define LED_pin6 6 / / destra VERDE rechts groen
#define LED_pin7 7 / / sinistra ROSSO links rood
PIN del ricevitore pin van RX
#define RX_pin 2
PIN direzione e velocità del motore sinistro, dir en macht van de linker motor
int pinDirS = 13;
int pinVelS = 11;
PIN direzione e velocità del motore destro, dir en macht van de juiste motor
int pinDirD = 12;
int pinVelD = 3;
VOID Setup
{
Serial.begin(38400);
PIN PER SEGNALARE QUELLO CHE ARRIVA DAL JOYSTIK ACCENDENDO IK LED
deze pin is voor Zie het signaal van de joystik
pinMode (LED_pin4, uitvoer);
pinMode (LED_pin5, uitvoer);
pinMode (LED_pin6, uitvoer);
pinMode (LED_pin7, uitvoer);
spengo tutti ik geleid, die alle opende
digitalWrite (LED_pin4, laag);
digitalWrite (LED_pin5, laag);
digitalWrite (LED_pin6, laag);
digitalWrite (LED_pin7, laag);
vw_set_rx_pin(RX_pin);
Niet servirebbe: il standaard 㨠11
vw_setup(3000); Bits per sec vw_rx_start();
Inizio il processo di ricezione
Serial.println ("Ricevitore attivo:");
imposto ik pin del motore sinistro, liet motor
pinMode (pinDirS, OUTPUT);
pinMode (pinVelS, OUTPUT);
imposto ik pin del motore destro, rechts motor
pinMode (pinDirD, OUTPUT);
pinMode (pinVelD, OUTPUT);
}
void loop {}
uint8_t buf [VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
Als (vw_get_message (buf, & buflen)) / / Non-blocking
{buf [buflen] = '\0'; / / Metto il terminatore di stringa
Als (strncmp ((char *) buf, "Alto", buflen) == 0) {avanza();
}
Als (strncmp ((char *) buf, "Basso", buflen) == 0) {indietro();
}
Als (strncmp ((char *) buf, "Destra", buflen) == 0) {giraDX();
}
Als (strncmp ((char *) buf, "Sinistra", buflen) == 0) {giraSX();
}
anders als (strncmp ((char *) buf, "Bottone", buflen) == 0) ferma();
Serial.Print ("Comando Ricevuto (" + String(buflen) + "auto.):");
Serial.println ((char *) buf+String("//"));
}
}
functie
ongeldig avanza() {}
Serial.println("Avanzo");
digitalWrite (LED_pin4, hoge); Accendo il geleid 4 giallo
digitalWrite (LED_pin5, laag); Spengo il LED 5
digitalWrite (LED_pin6, laag); Spengo il LED 6
digitalWrite (LED_pin7, laag); Spengo il LED 7
digitalWrite (pinDirS, hoge);
digitalWrite (pinDirD, hoge);
analogWrite (pinVelS, 255);
analogWrite (pinVelD, 255);
}
ongeldig indietro() {}
Serial.println("Indietro");
digitalWrite (LED_pin4, laag); Spengo il LED
digitalWrite (LED_pin5, hoge); Accendo il leidde 5 blu
digitalWrite (LED_pin6, laag); Spengo il LED 6
digitalWrite (LED_pin7, laag); Spengo il LED 7
digitalWrite (pinDirS, laag);
digitalWrite (pinDirD, laag);
analogWrite (pinVelS, 255);
analogWrite (pinVelD, 255);
}
ongeldig giraDX() {}
Serial.println ("Giro een DX");
digitalWrite (LED_pin4, laag); Spengo il LED 4
digitalWrite (LED_pin5, laag); Spengo il LED 5
digitalWrite (LED_pin6, hoge); Accendo il leidde 6 verde
digitalWrite (LED_pin7, laag); Spengo il LED 7
digitalWrite (pinDirS, laag);
digitalWrite (pinDirD, hoge);
analogWrite (pinVelS, 255);
analogWrite (pinVelD, 255);
}
ongeldig giraSX() {}
Serial.println ("een SX Giro");
digitalWrite (LED_pin4, laag); Spengo il LED 4
digitalWrite (LED_pin5, laag); Spengo il LED 5
digitalWrite (LED_pin6, laag); Spengo il LED 6
digitalWrite (LED_pin7, hoge); Accendo il leidde 7 rosso
digitalWrite (pinDirS, hoge);
digitalWrite (pinDirD, laag);
analogWrite (pinVelS, 255);
analogWrite (pinVelD, 255);
}
ongeldig ferma() {}
Serial.println ("Mi fermo");
digitalWrite (LED_pin4, laag); Spengo il LED 7
digitalWrite (LED_pin5, laag); Spengo il LED 4
digitalWrite (LED_pin6, laag); Spengo il LED 6
digitalWrite (LED_pin7, laag); Spengo il LED 5
digitalWrite (pinDirS, hoge);
digitalWrite (pinDirD, hoge);
analogWrite(pinVelS,0);
analogWrite(pinVelD,0);
}