Hi everyone I am considered a noob in this world. I would like to get your expertise in the problem I am facing.
in a simple way I am trying to create a network communication between 1 arduino lets call it (server) and 5 other arduino's ( slave) but the slave has to send specific data to receive instructions for movement.
that mean's each Arduino needs to send and receive Data.
my choice of networking is to use the RF-433MHZ transceiver module which seems to work (poor) after a day of playing with it. and that's communication between only 2!
the problem is that amount of data being sent is not consistent and right now I am only sending one digit data back and forth and a photo sensor reading, which is lagging by 6 to 8 seconds.
I am not sure if the code structure is wrong? or my choice of network is just crap!
and if there is any techniques to use in the code to help make the connection better ?
what do you guys suggest ?
I attached the code for both sending and receiving!
I am sorry I am not a good coder I just started to learn. if you have any suggestion on improvement I would really appreciate it!
Thanks!
#include <VirtualWire.h>
int lightsend;
int photopin =0;
int photocellval =0;
char photopinCharMsg[21];
char StringReceived[22]; // rec
boolean tra = true;
boolean sent = false;
void setup() {
Serial.begin(9600);
pinMode(photopin, INPUT);
vw_setup(1000); // Bits per sec
vw_set_tx_pin(12);// Set the Tx pin. Default is 12
vw_set_rx_pin(11);
}
void loop() {
// SENDING
photocellval = analogRead(photopin);
sprintf(photopinCharMsg, "%d,%d,", photocellval);
vw_send((uint8_t *)photopinCharMsg, strlen(photopinCharMsg));
vw_wait_tx();
Serial.println("messageSent");
delay(1000);
// RECEIVING
vw_rx_start();
delay(1000);
uint8_t buf[VW_MAX_MESSAGE_LEN]; // max length of the message
uint8_t buflen = VW_MAX_MESSAGE_LEN; // buffer we have to go thtough to get data
if (vw_get_message(buf, &buflen))
{
for (int i = 0; i < buflen; i++) {
StringReceived[i] = char(buf[i]);
}
sscanf(StringReceived, "%d,%d",&lightsend);
Serial.println(lightsend);
}
Serial.println("messagereceived");
memset( StringReceived, 0, sizeof( StringReceived));
vw_rx_stop();
delay(50);
}
#include <ServoTimer2.h>
#include <VirtualWire.h>
#define servopin 8
ServoTimer2 testservo;
boolean tra = true;
int servoval;
int photocellval;
int led =12;
int val;
char StringReceived[22]; // rec
char lightsend[21]; // send
void setup() {
Serial.begin(9600);
testservo.attach(servopin);
vw_setup(1000); // Bits per sec
vw_set_tx_pin(12);// Set the Tx pin. Default is 12
vw_set_rx_pin(11);
}
void loop() {
vw_rx_start();
delay(1000);
uint8_t buf[VW_MAX_MESSAGE_LEN]; // max length of the message
uint8_t buflen = VW_MAX_MESSAGE_LEN; // buffer we have to go thtough to get data
if (vw_get_message(buf, &buflen))
{
for (int i = 0; i < buflen; i++) {
StringReceived[i] = char(buf[i]);
}
sscanf(StringReceived, "%d,%d",&photocellval);
}
Serial.println(photocellval);
vw_rx_stop();
sprintf(lightsend, "%d", led);
vw_send((uint8_t *)lightsend, strlen(lightsend));
vw_wait_tx();
Serial.println("message sent ");
delay(1000);
}
// if( photocellval < 500){
// testservo.write(1500);
// }
//
// else if ( photocellval >= 500 && photocellval < 1000 ) {
//
// testservo.write(1300);
//
// }
//
// else if ( photocellval >= 1000 ){
// testservo.write(1500);
// }
//
// // photocellval = 0;
// memset( StringReceived, 0, sizeof( StringReceived));
// //vw_rx_stop();
// Serial.println(photocellval);
// delay(1000);
void tr() {
}
void rc() {
}