ciao a tutti dovrei far comunicare 2 arduini con moduli da 433 mhz il primo arduino invia valori analogici di un joystik e nell'altro dovrei ricevere 2 dati per far muovere 2 servi motore Sto impazzendo vi posto i codici TX e RX
codice TX
/*
PIN DESCRIPTION ARDUINO PIN
1 GND GND
2 VCC (3.5-12V) VCC
3 TX DATA D2
*/
#include <VirtualWire.h>
const int TX_DIO_Pin = 2;
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(13, OUTPUT);
initialize_transmitter();
}
/* Main program */
void loop() {
int counter=100;
int counter1=101;
int xpin=analogRead(X_pin);
int ypin=analogRead(Y_pin);
transmit_integer(counter);
delay(100);
transmit_integer(xpin);
delay(100);
transmit_integer(counter1);
delay(100);
transmit_integer(ypin);
delay(100);
}
/* DO NO EDIT BELOW */
void initialize_transmitter() {
/* Initialises the DIO pin used to send data to the Tx module /
vw_set_tx_pin(TX_DIO_Pin);
/ Set the transmit logic level (LOW = transmit for this version of module)*/
vw_set_ptt_inverted(true);
/* Transmit at 2000 bits per second */
vw_setup(2000); // Bits per sec
}
void transmit_integer(unsigned int Data) {
/* The transmit buffer that will hold the data to be
transmitted. /
byte TxBuffer[2];
/ ...and store it as high and low bytes in the transmit
buffer */
TxBuffer[0] = Data >> 8;
TxBuffer[1] = Data;
/* Send the data (2 bytes) */
vw_send((byte )TxBuffer, 2);
/ Wait until the data has been sent */
vw_wait_tx();
}
Codice RX
#include <VirtualWire.h>
const int RX_DIO_Pin = 2;
int received;
int received1;
int received2;
void setup() {
pinMode(13,OUTPUT);
Serial.begin(9600);
initialize_receiver();
}
/* Main program */
void loop() {
received = receive_integer();
if(received==100||received != -1)
{
if (received!=100){
received1 = received;
}
if (received!=101){
received2 = received1;
}
Serial.println(received1);
}
}
/* DO NOT EDIT BELOW */
void initialize_receiver() {
/* Initialises the DIO pin used to receive data from the Rx module /
vw_set_rx_pin(RX_DIO_Pin);
/ Receive at 2000 bits per second /
vw_setup(2000);
/ Enable the receiver */
vw_rx_start();
}
int receive_integer() {
/* Set the receive buffer size to 2 bytes */
uint8_t Buffer_Size = 2;
/* Holds the recived data */
unsigned int Data;
/* The receive buffer */
uint8_t RxBuffer[Buffer_Size];
/* Has a message been received? */
if (vw_get_message(RxBuffer, &Buffer_Size)) // Non-blocking
{
/* Store the received high and low byte data */
Data = RxBuffer[0] << 8 | RxBuffer[1];
return Data;
}
return -1;
}