#include <Arduino.h>
#include <SPI.h>
#include <RF24.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int Bat = A0; //r1=1000ohme r2=1250ohme
#define joyX A6
#define joyY A7
int arrbtn[] = {3, 4, 5};
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int spd=0;
int trn=0;
bool joy=false;
bool bt1 = false;
bool bt2 = false;
bool bt3 = false;
RF24 radio(10, 9); // ce/cs
bool isTransmitter = true; // if true, device starts as transmitter
void setup() {
pinMode(A6, INPUT);
pinMode(A7, INPUT);
pinMode(A0, INPUT);
for (int i = 0; i < 3; i++) {
pinMode(arrbtn[i], INPUT_PULLUP);
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30, 30);
// Display static text
display.println("R.O.B.E.R.T.O");
display.display();
// delay(2000);
display.display();
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(00002); // needs to be opposite on the other device
radio.setPALevel(RF24_PA_HIGH); // ^
radio.openReadingPipe(1, 000001); // |
radio.startListening();
}
float extractDecimalNumber(const char* text) {
float result = 0.0;
float sign = 1.0; // Positive by default
int i = 3; // Start from the fourth character after "bat"
bool decimalPoint = false;
float divisor = 10.0;
if (text[i] == '-') {
sign = -1.0;
i++;
}
while (text[i] != '\0') {
if (text[i] >= '0' && text[i] <= '9') {
if (decimalPoint) {
result += (text[i] - '0') / divisor;
divisor *= 10.0;
} else {
result = result * 10.0 + (text[i] - '0');
}
} else if (text[i] == '.') {
decimalPoint = true;
} else {
// Handle other characters or break on non-numeric characters if needed.
break;
}
i++;
}
return sign * result;
}
void handleTransmitterMode() {
//Serial.println("in tran mode");
radio.stopListening(); // turns radio into transmitter
delay(10);
char text[] = "Change"; // converts the text "change" into an array under the name text
radio.write(&text, sizeof(text)); // radio sends the message and the size of the message
// Serial.println("Transmitted message");
isTransmitter = false; // toggles transmitter role
}
float batc = 0;
void handleReceiverMode() {
//Serial.println("in rev mode");
radio.startListening(); // toggles radio into receiver mode
if (radio.available()) {
char text[32] = ""; // clears the 32 byte variable known as "text"
radio.read(&text, sizeof(text)); // reads the transmission
Serial.print("Received message: ");
Serial.println(text);
if (strncmp(text, "bat", 3) == 0) {
Serial.println(extractDecimalNumber(text));
batc = extractDecimalNumber(text);
}
}
else {
// This section only runs if no "bat" message was received.
radio.startListening(); // toggles radio into receiver mode
if (radio.available()) {
char text2[32] = ""; // Use a different variable name for the second text buffer
radio.read(&text2, sizeof(text2)); // reads the transmission
Serial.print("Received message: ");
Serial.println(text2);
if (strcmp(text2, "Change") == 0) { // checks if the transmission is "change", if it is
isTransmitter = true; // then toggle
Serial.println("jayveer");
delay(100);
}
}
}
}
void joystick() {
int xValue = analogRead(joyX) - 512;
int yValue = analogRead(joyY) - 515;
for (int i = 0; i < 31; i++) {
if (digitalRead(arrbtn[i]) == LOW) {
switch (i)
{
case 0:
//autodrive
bt1 = !bt1;
{
radio.stopListening();
char b1[] = "b1";
radio.write(&b1, sizeof(b1));
Serial.println("b1");
isTransmitter = true;
}
break;
case 1:
bt2 = !bt2;
{
radio.stopListening();
char b2[] = "b2";
radio.write(&b2, sizeof(b2));
Serial.println("b2");
isTransmitter = true;
}
break;
case 2:
bt3 = !bt3;
{
radio.stopListening();
char b3[] = "b3";
radio.write(&b3, sizeof(b3));
Serial.println("b3");
isTransmitter = true;
}
break;
}
}
}
// Perform any additional joystick-related actions here
if (xValue > 15 || yValue > 15 || xValue < -15 || yValue < -15) {
joy=true;
spd=xValue;
trn=yValue;
radio.stopListening();
String trnT = "trn" + String(trn);
radio.write(trnT.c_str(), trnT.length() + 1); // +1 to include null terminator
Serial.println(trnT);
String spdT = "spd" + String(spd);
radio.write(spdT.c_str(), spdT.length() + 1); // +1 to include null terminator
Serial.println(spdT);
isTransmitter = true;
}
else if(isTransmitter){ char text[] = "Change";
radio.write(&text, sizeof(text));
Serial.println("this");
joy=false;}
}
void kill(){
radio.stopListening();
char text1[] = "Zero";
radio.write(&text1, sizeof(text1));
//Serial.println("Zero");
}
void LCD() {
int xValue = analogRead(joyX) - 512;
int yValue = analogRead(joyY) - 515;
int rawValue = analogRead(Bat);
float batteryVoltage = (float)rawValue * (9.0 / 1023.0);
float batteryPercentage = map(batteryVoltage, 0.0, 9.0, 0, 100);
display.setTextSize(1);
display.clearDisplay();
display.setCursor(0, 0);
display.println("CB% : ");
display.println(batteryPercentage);
display.setCursor(90, 0);
display.println("DB% :");
display.setCursor(90, 9);
display.println(batc);
display.display();
display.display();
display.drawRect(55, 0, 18, 16, WHITE);
display.display();
if(xValue>35){
display.setTextSize(2);
display.setCursor(10, 20);
display.print("FWRD ");
display.print(xValue/2);
display.display();
}
else if(xValue<-35){
display.setTextSize(2);
display.setCursor(10, 20);
display.print("BKWD ");
display.print(xValue/2);
display.display();
}
if(yValue>35){
display.setTextSize(2);
display.setCursor(10, 40);
display.print("RGHT ");
display.print(yValue/2);
display.display();
}
else if(yValue<-35){
display.setTextSize(2);
display.setCursor(10, 40);
display.print("LEFT ");
display.print(yValue/2);
display.display();
}
}
void loop() {
LCD();
joystick();
if(!joy){
kill();
}
// Only execute transmitter or receiver functions when the joystick is not being used
if (isTransmitter && !joy) {
handleTransmitterMode(); // if transmitter is true, go to that function
} else if (!isTransmitter && !joy) {
handleReceiverMode(); // if transmitter is false, go to that function
}
if(bt1){
display.setTextSize(2);
display.setCursor(60, 0);
display.println("1");
display.display();
}
if(bt2){
display.setTextSize(2);
display.setCursor(60, 0);
display.println("2");
display.display();
}
if(bt3){
display.setTextSize(2);
display.setCursor(60, 0);
display.println("3");
display.display();
}
}
this code all works fine, except for the fact that my battery percentage of the controller "cb" changes values with the joystick, but only on the x axis.
void setup() {
pinMode(A6, INPUT);
pinMode(A7, INPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
analogRead(A6);
analogRead(A7);
int rawValue = analogRead(A0);
float batteryVoltage = (float)rawValue * (9.0 / 1023.0);
float batteryPercentage = map(batteryVoltage, 0.0, 9.0, 0, 100);
Serial.println(batteryPercentage);
}
I tried this, and the exact same thing happens. Im using a voltage divider on a 9v battery, 1000 ohme r1 and 1220 r2. battery percentage with the joystick unplugged is 66%, as soon as i plug it in, it drops to 55%, and then when i move along the x axis it goes up +-30%.