I'm using two standalone atmega328p chips - one as a controller and the other as a rc car. I want to transmit data from the controller to the car (but preferably in both ways). Right now I'm trying to transmit data from two joysticks to the car as an array and display it on oled screen on the car.
But I never get past the receiver.available() condition leaving me with no data.
Code for both below
Code for the controller
#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
#define BatteryVoltagePin 4
#define JoyStickLeftX A0
#define JoyStickLeftY A3
#define JoyStickRightX A2
#define JoyStickRightY A1
#define CE 7
#define CS 8
#define SCK 13
RF24 transmitter(CE, CS);
U8X8_SSD1306_128X64_NONAME_HW_I2C display1(U8X8_PIN_NONE);
U8X8_SSD1306_128X64_NONAME_HW_I2C display2(U8X8_PIN_NONE);
const int joyStickZerothRange = 22; // Range from the middle to ONE side for Joysticks to still register as zero
const int joyStickMinimumPoint = 1;
const int joyStickMaximumPoint = 1024;
const int joyStickMiddlePoint = joyStickMaximumPoint / 2;
const int joyStickLowRangeTopPoint = joyStickMiddlePoint - joyStickZerothRange;
const int joyStickTopRangeLowestPoint = joyStickMiddlePoint + joyStickZerothRange;
char JoyStickCharValue[11];
char *BatteryVoltageMessageNormal = "BATTERY NORMAL";
char *BatteryVoltageMessageLow = "BATTERY LOW ";
const uint64_t addressReceiver = 0xE8E8F0F0E1LL;
const uint64_t addressTransmitter = 0xE8E8F0F1E1LL;
void drawJoyStickValues(int joyStickValuesInput[]){
sprintf(JoyStickCharValue,"X1:%03d", abs(joyStickValuesInput[0]));
display1.drawString(1, 1, JoyStickCharValue);
sprintf(JoyStickCharValue,"Y1:%03d", abs(joyStickValuesInput[1]));
display1.drawString(9, 1, JoyStickCharValue);
sprintf(JoyStickCharValue,"X2:%03d", abs(joyStickValuesInput[2]));
display1.drawString(1, 3, JoyStickCharValue);
sprintf(JoyStickCharValue,"Y2:%03d", abs(joyStickValuesInput[3]));
display1.drawString(9, 3, JoyStickCharValue);
}
int convertJoyStickValue(int inputValue){
if (inputValue > joyStickLowRangeTopPoint && inputValue < joyStickTopRangeLowestPoint){
return 0;
}
else if (inputValue <= joyStickLowRangeTopPoint){
return map(inputValue, joyStickMinimumPoint, joyStickLowRangeTopPoint, -255, -1);
}
else if (inputValue >= joyStickTopRangeLowestPoint){
return map(inputValue, joyStickTopRangeLowestPoint, joyStickMaximumPoint, 1, 255);
}
}
int * getJoyStickValues(){
static int helper[4];
helper[0] = convertJoyStickValue(analogRead(JoyStickLeftX));
helper[1] = convertJoyStickValue(analogRead(JoyStickLeftY));
helper[2] = convertJoyStickValue(analogRead(JoyStickRightX));
helper[3] = convertJoyStickValue(analogRead(JoyStickRightY));
return helper;
}
void drawBatteryVoltage(){
pinMode(BatteryVoltagePin, OUTPUT);
digitalWrite(BatteryVoltagePin, LOW);
pinMode(BatteryVoltagePin, INPUT);
if (digitalRead(BatteryVoltagePin) == HIGH){
display1.drawString(1, 7, BatteryVoltageMessageNormal);
}else{
display1.drawString(1, 7, BatteryVoltageMessageLow);
}
}
void sendAllData(int rotationInput, int speedInput){
transmitter.stopListening();
byte helper[2];
helper[0] = rotationInput;
helper[1] = speedInput;
transmitter.write(helper, sizeof(helper));
transmitter.startListening();
}
void setup(void) {
// set joystick and battery pins to inputs
pinMode(JoyStickLeftX, INPUT);
pinMode(JoyStickLeftY, INPUT);
pinMode(JoyStickRightX, INPUT);
pinMode(JoyStickRightX, INPUT);
pinMode(BatteryVoltagePin, INPUT);
// start transmitter
transmitter.begin();
transmitter.openWritingPipe(addressTransmitter);
transmitter.openReadingPipe(1, addressReceiver);
transmitter.startListening();
transmitter.powerUp();
transmitter.enableDynamicPayloads();
transmitter.setDataRate(RF24_250KBPS);
// start displays
display1.setI2CAddress(0x7A);
display1.begin();
display1.setFont(u8x8_font_victoriamedium8_u); // choose a suitable font
display1.clearDisplay();
display2.setI2CAddress(0x78);
display2.begin();
display2.setFont(u8x8_font_victoriamedium8_u); // choose a suitable font
display2.clearDisplay();
}
void loop(void) {
int * joyStickValues = getJoyStickValues();
drawBatteryVoltage();
drawJoyStickValues(joyStickValues);
int speedValue = joyStickValues[1]; // Y value of left joystick
int rotationValue = joyStickValues[2]; // X value of right joystick
sendAllData(rotationValue, speedValue);
}
Code for the car
#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
#define BatteryVoltagePin A3
// Define pins for left motor
#define LeftMotorForward 0
#define LeftMotorBackward 1
#define LeftMotorSpeed 5
// Define pins for
#define RightMotorForward 2
#define RightMotorBackward 3
#define RightMotorSpeed 6
#define CE 7
#define CS 8
// Initialze display
U8X8_SSD1306_128X64_NONAME_HW_I2C display1(U8X8_PIN_NONE);
RF24 receiver(CE, CS);
// Define variables for battery voltage measurment
char BatteryVoltageMapped[12];
char *dataMessage;
const uint64_t addressReceiver = 0xE8E8F0F0E1LL;
const uint64_t addressTransmitter = 0xE8E8F0F1E1LL;
char *NRFMessageNotAvailable = "NO DATA ";
char *NRFMessageAvailable = "READING DATA";
void drawBatteryVoltage(void) {
int helper = map(analogRead(BatteryVoltagePin), 852, 1023, 61, 74);
sprintf(BatteryVoltageMapped, "BATTERY:%02dV", helper); //TODO: add decimal point
display1.drawString(1, 1, BatteryVoltageMapped);
}
void getData(){
if (receiver.available()){
display1.drawString(1, 3, NRFMessageAvailable);
byte data[2];
receiver.read(data, sizeof(data));
sprintf(dataMessage,"ANGLE:%03d", int(data[0]));
display1.drawString(1, 5, dataMessage);
sprintf(dataMessage,"SPEED:%03d", int(data[1]));
display1.drawString(1, 7, dataMessage);
}
else {
display1.drawString(1, 3, NRFMessageNotAvailable);
}
}
void setup(void) {
// Set all pins regarding motors to outputs
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(LeftMotorSpeed, OUTPUT);
pinMode(RightMotorForward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(RightMotorSpeed, OUTPUT);
// Start display
display1.setI2CAddress(0x78); // Specify address
display1.begin(); // Start transmission
display1.setFont(u8x8_font_victoriamedium8_u); // Choose a suitable font
display1.clearDisplay(); // Reset contents to clear it
// Start receiver
receiver.openWritingPipe(addressReceiver);
receiver.openReadingPipe(0, addressTransmitter);
receiver.begin();
receiver.powerUp();
receiver.enableDynamicPayloads();
receiver.setDataRate(RF24_250KBPS);
receiver.startListening();
delay(50);
}
void loop(void) {
drawBatteryVoltage();
getData();
}



