/*
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.
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");
}
}
}