Integrating MMA8452Q Tilt sensor with RF24 transceiver

Hi, I'm completely new to coding and I have two separate codes that work individually but I don't know how to integrate them.

MMA8452Q orientation code found on SparkFun_MMA8452Q_Arduino_Library/examples/Example3_Orientation/Example3_Orientation.ino at main · sparkfun/SparkFun_MMA8452Q_Arduino_Library · GitHub

/*
  Library for the MMA8452Q
  By: Jim Lindblom and Andrea DeVore
  SparkFun Electronics

  Do you like this library? Help support SparkFun. Buy a board!
  https://www.sparkfun.com/products/14587

  This sketch uses the SparkFun_MMA8452Q library to initialize
  the accelerometer and stream its orientation.

  Hardware hookup:
  Arduino --------------- MMA8452Q Breakout
    3.3V  ---------------     3.3V
    GND   ---------------     GND
  SDA (A4) --\/330 Ohm\/--    SDA
  SCL (A5) --\/330 Ohm\/--    SCL

  The MMA8452Q is a 3.3V max sensor, so you'll need to do some
  level-shifting between the Arduino and the breakout. Series
  resistors on the SDA and SCL lines should do the trick.

  License: This code is public domain, but if you see me
  (or any other SparkFun employee) at the local, and you've
  found our code helpful, please buy us a round (Beerware
  license).

  Distributed as is; no warrenty given.
*/

#include <Wire.h>                 // Must include Wire library for I2C
#include "SparkFun_MMA8452Q.h"    // Click here to get the library: http://librarymanager/All#SparkFun_MMA8452Q

MMA8452Q accel;                   // create instance of the MMA8452 class

void setup() {
  Serial.begin(9600);
  Serial.println("MMA8452Q Orientation Test Code!");
  Wire.begin();

  if (accel.begin() == false) {
    Serial.println("Not Connected. Please check connections and read the hookup guide.");
    while (1);
  }
}

void loop() {
  if (accel.available()) {      // Wait for new data from accelerometer
    // Orientation of board (Right, Left, Down, Up);
    if (accel.isRight() == true) {
      Serial.println("Right");
    }
    else if (accel.isLeft() == true) {
      Serial.println("Left");
    }
    else if (accel.isUp() == true) {
      Serial.println("Up");
    }
    else if (accel.isDown() == true) {
      Serial.println("Down");
    }
    else if (accel.isFlat() == true) {
      Serial.println("Flat");
    }
  }
}

RF24 transmitter code

#include <SPI.h>  //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem

//define the data pins
int joyX = A1;
int joyY = A2;

//define variable values
int dataX;
int dataY;

int data[2];

RF24 radio(10,9); //10 and 9 are a digital pin numbers to which signals CE and CSN are connected
                                      
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino


void setup(void){
  Serial.begin(9600);
  radio.begin();                      //it activates the modem
  radio.openWritingPipe(pipe);        //sets the address of the receiver to which the program will send data
}

void loop(){
  //Send signal data
  dataX = analogRead(joyX);
  dataY = analogRead(joyY);
  
  data[0] = dataX;
  data[1] = dataY;

  Serial.print("Data X:"); Serial.println(dataX);
  Serial.print("Data Y:"); Serial.println(dataY);
  radio.write(data, sizeof(data));
  
}


People with experience start with one of the working examples and add the necessary parts of the other, one small segment at a time, testing as they go.

It should be obvious that in order to do that, you first need to know what each line of code does, and why, in both programs individually.

Therefore, this is an advanced project and it would probably take most beginners quite a while to learn the necessary skills.

If you are in a hurry you can use the flag button (lower right on the post) to ask the moderator to move the post to the Jobs and Paid Collaborations forum section.

Here’s a simple way to merge them: instead of reading joystick values, just read orientation using the MMA8452Q, and send that over RF24 as a string.

Try this:

#include <Wire.h>
#include <SPI.h>
#include <RF24.h>
#include "SparkFun_MMA8452Q.h"

MMA8452Q accel;
RF24 radio(10, 9);
const uint64_t pipe = 0xE8E8F0F0E1LL;
char orientation[10];

void setup() {
  Serial.begin(9600);
  Wire.begin();
  if (!accel.begin()) while (1);
  radio.begin();
  radio.openWritingPipe(pipe);
  radio.stopListening();
}

void loop() {
  if (accel.available()) {
    if (accel.isRight()) strcpy(orientation, "Right");
    else if (accel.isLeft()) strcpy(orientation, "Left");
    else if (accel.isUp()) strcpy(orientation, "Up");
    else if (accel.isDown()) strcpy(orientation, "Down");
    else if (accel.isFlat()) strcpy(orientation, "Flat");
    else strcpy(orientation, "Unknown");

    Serial.println(orientation);
    radio.write(&orientation, sizeof(orientation));
  }

  delay(200);
}

Let me know if you need help with the receiver code too!

The setup code is simple, just place the 2nd after the first in any order. The loop code is slightly more problematic. You MAY need advanced skills to integrate the two pieces of code.

Thanks so much for the code. Originally, I was using the following code for my receiver, but it uses digital values. If you have suggestions on how to change it to suit the transmitter code, it would be much appreciated.

#include <SPI.h>      //the communication interface with the modem
#include "RF24.h"     //the library which helps us to control the radio modem (nRF24L)

//Motor A
const int RightMotorForward = 2;    // IN1
const int RightMotorBackward = 4;   // IN2

//Motor B

const int LeftMotorForward = 7;     // IN3
const int LeftMotorBackward = 8;    // IN4


int data[2];

RF24 radio(10, 9); //10 and 9 are a digital pin numbers to which signals CE and CSN are connected

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino


void setup() {
  Serial.begin(9600);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);

  radio.begin();                    //it activates the modem
  radio.openReadingPipe(1, pipe);   //determines the address of our modem which receive data
  radio.startListening();           //enable receiving data via modem
}

void loop() {
  if (radio.available()) {
    radio.read(data, sizeof(data));

    //data X
    if (data[0] > 700) {
      digitalWrite(RightMotorForward, HIGH);
      digitalWrite(RightMotorBackward, LOW);
      digitalWrite(LeftMotorForward, HIGH);
      digitalWrite(LeftMotorBackward, LOW);
      Serial.println("FORWARD");
    }

    //data X
    if (data[0] < 100) {
      digitalWrite(RightMotorForward, LOW);
      digitalWrite(RightMotorBackward, HIGH);
      digitalWrite(LeftMotorForward, LOW);
      digitalWrite(LeftMotorBackward, HIGH);
      Serial.println("BACKWARD");
    }

    //data Y
    if (data[1] > 600 ) {
      digitalWrite(RightMotorForward, LOW);
      digitalWrite(RightMotorBackward, HIGH);
      digitalWrite(LeftMotorForward, HIGH);
      digitalWrite(LeftMotorBackward, LOW);
      Serial.println("TURN RIGHT");
    }

    //data Y
    if (data[1] < 250 ) {
      digitalWrite(RightMotorForward, HIGH);
      digitalWrite(RightMotorBackward, LOW);
      digitalWrite(LeftMotorForward, LOW);
      digitalWrite(LeftMotorBackward, HIGH);
      Serial.println("TURN LEFT");
    }

    if (data[0] < 450 && data[0] > 350 && data[1] < 420 && data[1] > 350) {
      digitalWrite(RightMotorForward, LOW);
      digitalWrite(RightMotorBackward, LOW);
      digitalWrite(LeftMotorForward, LOW);
      digitalWrite(LeftMotorBackward, LOW);
      Serial.println("STOP");
    }

  }
}

Sure. Try This,

#include <SPI.h>
#include "RF24.h"

// Motor A
const int RightMotorForward = 2;
const int RightMotorBackward = 4;

// Motor B
const int LeftMotorForward = 7;
const int LeftMotorBackward = 8;

RF24 radio(10, 9);  // CE, CSN
const uint64_t pipe = 0xE8E8F0F0E1LL;

char orientation[10];  // To hold received orientation string

void setup() {
  Serial.begin(9600);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);

  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&orientation, sizeof(orientation));
    Serial.print("Received orientation: ");
    Serial.println(orientation);

    controlMotors(orientation);
  }
}

// Function to control motors based on orientation string
void controlMotors(const char* direction) {
  // Stop all motors first
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, LOW);

  // Move based on orientation
  if (strcmp(direction, "Up") == 0) {
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorForward, HIGH);
  } else if (strcmp(direction, "Down") == 0) {
    digitalWrite(RightMotorBackward, HIGH);
    digitalWrite(LeftMotorBackward, HIGH);
  } else if (strcmp(direction, "Right") == 0) {
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorBackward, HIGH);
  } else if (strcmp(direction, "Left") == 0) {
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, HIGH);
  } else if (strcmp(direction, "Flat") == 0) {
    // Optional: stop or idle state
  } else {
    Serial.println("Unknown direction received.");
  }
}