rf Link Receiver & transmitter with accelerometer

Hi people around here again asking for some help I’m working with 3-axis accelerometer MMA7361L. My problem is when I have data to transmit RF modules and work with these 2 parts for a car to wireless but the program was pretty easy. Here we present my programs for the car.

Accelerometer:
http://www.robodacta.mx/index.php?dispatch=products.view&product_id=30201

RX
http://www.robodacta.mx/index.php?dispatch=products.view&product_id=30324

TX
http://www.robodacta.mx/index.php?dispatch=products.view&product_id=30326

TX:

int x;
int y;

void setup()
{
  Serial.begin(4800);
  
  pinMode(x,INPUT);
  pinMode(y,INPUT);
}

void loop()
{
  x = analogRead(A0);
  y = analogRead(A1);
  
  if(x<250)
   Serial.print('s');
   
  else if(x>420)
   Serial.print('w');
   
  else if(y>400)
   Serial.print('a'); 
  
  else if(y<240)
   Serial.print('d');
   
  else
  Serial.print('n');
   
   delay(100);
}

RX:

int led=12;
int A = 4; 
int B = 5;
int C = 6;
int D = 7;


void setup() {
  
  Serial.begin(4800);
  pinMode(12,OUTPUT);
   pinMode(A,OUTPUT);
   pinMode(B,OUTPUT);
   pinMode(C,OUTPUT);
   pinMode(D,OUTPUT);  
}
void loop() {
     
    byte Byte;

   
    if (Serial.available()) {
    Byte = Serial.read();
   
    switch(Byte){
    case 's':
    
      digitalWrite(A,LOW);
      digitalWrite(B,HIGH);
      digitalWrite(C,LOW);
      digitalWrite(D,HIGH);
      delay(200);
      digitalWrite(A,LOW);
      digitalWrite(B,LOW);
      digitalWrite(C,LOW);
      digitalWrite(D,LOW);
      Serial.flush();
      break;
     
      case 'w':
    
      digitalWrite(A,HIGH);
      digitalWrite(B,LOW);
      digitalWrite(C,HIGH);
      digitalWrite(D,LOW);
      delay(200);
      digitalWrite(A,LOW);
      digitalWrite(B,LOW);
      digitalWrite(C,LOW);
      digitalWrite(D,LOW);
      Serial.flush();
      break;
      
      case 'a':
 
      digitalWrite(A,HIGH); 
      digitalWrite(B,LOW);
      digitalWrite(C,LOW);
      digitalWrite(D,HIGH);
      delay(50);
      digitalWrite(A,LOW);
      digitalWrite(B,LOW);
      digitalWrite(C,LOW);
      digitalWrite(D,LOW);
      Serial.flush();
      break; 
      
      case 'd':
      
      digitalWrite(A,LOW);
      digitalWrite(B,HIGH);
      digitalWrite(C,HIGH);
      digitalWrite(D,LOW);
      delay(50);
      digitalWrite(A,LOW);
      digitalWrite(B,LOW);
      digitalWrite(C,LOW);
      digitalWrite(D,LOW);
      Serial.flush();
      break;
      
      case 'n':
      
      digitalWrite(A,LOW);
      digitalWrite(B,LOW);
      digitalWrite(C,LOW);
      digitalWrite(D,LOW);
      Serial.flush();
      break;

     
     }
}
}

As you can see it was only considering a range and send a byte for easy recpecion but what I want is to transmit the signals from the accelerometer 3 to Receive and use them but eh achieve success. This is one of the programs that use.

const int rf[] = { 0, 1, 2};
int rf[]= {0, 0, 0};

void setup()
{
  Serial.begin(4800);
}

void loop()
{
  for(int i=0; i<3; i++){
    rf[i]= analogRead(rf[i]);
    Serial.print(rf[i]);
  }
  delay(100);
}

and recover the same way but I only get the data receiver noise values ??from 0 to 255 there will be some way to transmit accelerometer variables with these transmitters.

Thanks for your time.

void loop()
{
  for(int i=0; i<3; i++){
    rf[i]= analogRead(rf[i]);
    Serial.print(rf[i]);
  }
  delay(100);
}

Have you tried looking at what is being sent to the serial port, using the Serial Monitor?

rf was initially set to all 0s. So you are reading noise from the analog pin 0, unless you actually have something connected to it, and assigning that to rf[ i ]. Why?

Yes, I'm using the Serial monitor to verify the data, data from the TX I have aceleromtro values??. 3503212951235334534343

These values ??are the 3 axes, but I can not separate in some form and send it.

array (rf ) was a way to save the 3 accelerometer values ??separately but do not get the drill results expected.

array (rf ) was a way to save the 3 accelerometer values ??separately

Is the array rf the accelerometer values or the pin numbers? It is being used, incorrectly, for both.

Thanks for your time.

Yes, I have done several tests to transmit accelerometer data.

But without good results. Here another program of the transmittance of the accelerometer data but the problem is not as separate values ??in each axis at the receiver

TX:

int xpin = A1;
int ypin = A2;
int zpin = A3;

void setup(){
  Serial.begin(4800);
}

void loop(){
  Serial.print(analogRead(xpin));
  Serial.println();
  delay(50);
  
  Serial.print(analogRead(ypin));
  Serial.println();
  delay(50);
  
  Serial.print(analogRead(zpin));
  Serial.println();

  delay(50);
}

RX:

int xpin;
int ypin;
int zpin;

void setup(){
  Serial.begin(4800);
}

void loop(){
  
  if(Serial.available()>0){
    xpin = Serial.read();
    delay(100);
    ypin = Serial.read();
    delay(100);
    zpin = Serial.read();
    delay(100);
  }
}

Yes, I have done several tests to transmit accelerometer data.

But, have you paid any attention to the output?

  Serial.print(analogRead(xpin));

This reads the value of a pin, and sends the result to the serial port AS A STRING.

    xpin = Serial.read();

This reads one character of that string.

ok, thanks I will do other tests and I'll be comment my results

I again, I have a program that can be a good option but now I have another problem, I think my transmitters are damaged because they do not get any response. With other programs by sending a sequence of bytes is set on the led board TX, but now I have not got any response from the plates or the tx and rx.

What do you think the program works well and believes that the problem be my TX and RX modules.

TX:

int x;
int y;
int z;

void setup(){
  Serial.begin(4800);
  Serial.flush();
}
 void loop(){
   x = analogRead(A1);
   y = analogRead(A2);
   z = analogRead(A3);
   
   Serial.print('X');
   Serial.print(x);
   Serial.print(',');
   Serial.print('Y');
   Serial.print(y);
   Serial.print(',');
   Serial.print('Z');
   Serial.print(z);
   Serial.print(',');
   delay (1000);
 }

RX:

#include <Servo.h>

char buffer[15];
Servo servo1;
Servo servo2;
Servo servo3;

void setup(){
  servo1.attach(5);  
  servo2.attach(6);
  servo3.attach(7);
  servo1.write(90);
  servo2.write(90);
  servo3.write(90);
  
  Serial.begin(4800);
  Serial.flush();
}

void loop(){
  if(Serial.available()>0){
    int index = 0;
    delay(500);
    int numChar = Serial.available();
    if(numChar>15){numChar=15;}
    
    while(numChar--){
      buffer[index++] = Serial.read();
    }
   splitString(buffer);
  }
}

void splitString(char* data){
  Serial.print("Dato ingresado: ");
  Serial.println(data);
  char* parametro;
  parametro = strtok(data, ",");
  while ( parametro != NULL){
    setServo(parametro);
    parametro = strtok(NULL, ",");
  }
  
  for(int i=0; i<14; i++){
    buffer[i]='\0';
  }
  Serial.flush();
}

void setServo(char* data){
  if((data[0] == 'X') || (data[0] =='1')){
    int x = strtol(data+1, NULL, 15);
    x = map(x, 130, 500, 0, 180);
    servo1.write(x);
    Serial.print("Servo1 esta listo a: ");
    Serial.println(x);
  }
  
  if((data[0] == 'Y') || (data[0] == '1')){
    int y = strtol(data+1, NULL, 15);
    y = map(y, 190, 570, 0, 180);
    servo2.write(y);
    Serial.print("Servo2 esta listo a: ");
    Serial.println(y);
  }
  
  if((data[0] == 'Z') || (data[0] == '1')){
    int z = strtol(data+1, NULL, 15);
    z = map(z, 150, 550, 0, 180);
    servo3.write(z);
    Serial.print("Servo3 esta listo a: ");
    Serial.println(z);
  }
}
  Serial.begin(4800);
  Serial.flush();

Immediately after establishing a serial receive buffer, dump any unread data in that buffer.

Seems useless to me.

  if(Serial.available()>0){
    int index = 0;
    delay(500);

There is some data to read, so lets sit around doing nothing for a while.

Why?

  Serial.flush();

Dump random amounts of unread data. What a great idea. Not.

Serial.begin(4800);
  Serial.flush();

Then begin serial communications and carry out a Serial.flush() command, which flushes any characters out of the serial buffer so it is empty and ready to receive commands for the servos.

 if(Serial.available()>0){
    int index = 0;
    delay(500);
    int numChar = Serial.available();
    if(numChar>15){numChar=15;}
    
    while(numChar--){
      buffer[index++] = Serial.read();
    }
   splitString(buffer);
  }

Let the buffer fill up and obtain the length of the string, ensuring it does not overflow above the maximum of 15 characters. Once the buffer is full, you call the splitString routine sending the buffer array to the function.

Ok, I'll make some changes to the program and comment on my results, but as a recommendation to the devices I do some testing with an oscilloscope to see which generate the TX and RX but the truth is not to wait.

Thanks for your time.

Then begin serial communications and carry out a Serial.flush() command, which flushes any characters out of the serial buffer so it is empty and ready to receive commands for the servos.

The serial receive buffer was just created, nanoseconds before hand. Where is the data in the buffer supposed to have come from in that period of time?

Let the buffer fill up and obtain the length of the string, ensuring it does not overflow above the maximum of 15 characters. Once the buffer is full, you call the splitString routine sending the buffer array to the function.

It would be smarter to add start and end of packet markers to the packets, and read whatever data is available, adding it to the buffer, resetting the buffer, or breaking out of the while loop, as appropriate, depending on what was read. No delays are required.