#include <SPI.h>
#include <RF24.h>
#include "drive.h"
const int Bat = A0;
// Pin Definitions
int InA1 = 2;
int InA2 = 4;
int EnL = 3;
int InB1 = 6;
int InB2 = 7;
int EnR = 5;
int stby = 8;
int LED = 6;
// Other global variables
int Spd = 0;
int Trn = 0;
bool b1 = false;
bool b2 = false;
bool b3 = false;
bool led = false;
RF24 radio(10, 9);//ce/cs
bool isTransmitter = false; //if true, device starts as transmitter
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(00001);//needs to be opposite on other device
radio.setPALevel(RF24_PA_HIGH);// ^
radio.openReadingPipe(1, 000002);// |
radio.startListening();
pinMode(InA1, OUTPUT);
pinMode(InA2, OUTPUT);
pinMode(InB1, OUTPUT);
pinMode(InB2, OUTPUT);
pinMode(EnL, OUTPUT);
pinMode(EnR, OUTPUT);
pinMode(stby, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
Drive();
if (isTransmitter) {
handleTransmitterMode();//if transmitter is true, go to that function
} else {
handleReceiverMode();//if transmitter is false, go to that function
}
while (b1){
digitalWrite(stby, HIGH);
analogWrite(EnL, 255);
analogWrite(EnR, 255);
digitalWrite(InA1, HIGH);
digitalWrite(InA2, LOW);
digitalWrite(InB1, HIGH);
digitalWrite(InB2, LOW);
handleReceiverMode();
handleTransmitterMode();
}
while (b2){
digitalWrite(stby, HIGH);
analogWrite(EnL, 255);
analogWrite(EnR, 255);
digitalWrite(InA1, LOW);
digitalWrite(InA2, HIGH);
digitalWrite(InB1, HIGH);
digitalWrite(InB2, LOW);
handleReceiverMode();
handleTransmitterMode();
}
if(b3){
digitalWrite(LED, HIGH);
}
}
void handleTransmitterMode() {
int rawValue = analogRead(Bat);
float batteryVoltage = (float)rawValue * (12.6 / 1023.0);
float batteryPercentage = map(batteryVoltage, 7.2, 12.6, 0, 100);
Serial.println();
radio.stopListening(); //turns radio into transmitter
delay(10);
String bat = "bat" + String(batteryPercentage);
radio.write(bat.c_str(), bat.length() + 1); // +1 to include null terminator
//toggles transmitter role
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
// char text1[] = "bat50"; //converts the text "change" into an array under the name text
// radio.write(&text1, sizeof(text1)); // radio sends the message and the size of the message
isTransmitter = !isTransmitter;
}
void handleReceiverMode() {
radio.startListening(); //toggles radio into reciever mode
if (radio.available()) {
char text[32] = ""; //clears the 32 byte variable known as "text"
radio.read(&text, sizeof(text)); //reads the transmission
if (strcmp(text, "Zero") == 0) {
digitalWrite(EnL, LOW);
digitalWrite(EnR, LOW);
Spd=0;
Trn=0;
}
if (strcmp(text, "Change") == 0) { //checks if teh transmission is "change", if it is
isTransmitter = !isTransmitter; //then toggle
}
if (strncmp(text, "spd", 3) == 0) {
int spd = extractNumber(text);
// Serial.print("Received SPD value: ");
// Serial.println(spd);
Spd=spd;
// delay(500); //unblock these comments to read the variables
}
if (strncmp(text, "trn", 3) == 0) {
int trn = extractNumber(text);
// Serial.print("Received trn value: ");
// Serial.println(trn);
Trn=trn;
//delay(500);
}
if (strncmp(text, "b1", 2) == 0) {
b1 = !b1;
}
if (strncmp(text, "b2", 2) == 0) {
b2 = !b2;
}
if (strncmp(text, "b3", 2) == 0) {
b3 = !b3;
Serial.println("b3 pressed!!!!!!!");
led = true;
}
}
}
int extractNumber(const char* text) {
int result = 0;
int sign = 1; // Positive by default
int i = 3; // Start from the fourth character after "spd"
if (text[i] == '-') {
sign = -1;
i++;
}
while (text[i] >= '0' && text[i] <= '9') {
result = result * 10 + (text[i] - '0');
i++;
}
return sign * result;
}
void Drive(){
int Speed = map(Spd, 0, 512, 0, 255);
if(Spd>10){ //forward
digitalWrite(stby, HIGH);
analogWrite(EnL, Speed);
analogWrite(EnR, Speed);
digitalWrite(InA1, HIGH);
digitalWrite(InA2, LOW);
digitalWrite(InB1, HIGH);
digitalWrite(InB2, LOW);
//Serial.println(Speed);
}
if(Spd<-10){ //backward
Speed=Speed*-1;
digitalWrite(stby, HIGH);
analogWrite(EnL, Speed);
analogWrite(EnR, Speed);
digitalWrite(InA1, LOW);
digitalWrite(InA2, HIGH);
digitalWrite(InB1, LOW);
digitalWrite(InB2, HIGH);
//Serial.println(Speed);
}
if (Trn > 15) {
digitalWrite(stby, HIGH);
int Left = map((Spd + Trn), 0, 510, 0, 255);
int Right = map(Spd, 0, 510, 0, 255);
analogWrite(EnL, Left);
analogWrite(EnR, Right);
Serial.print("Left: ");
Serial.println(Left);
Serial.print("Right: ");
Serial.println(Right);
//delay(100);
}
if (Trn < -15) {
digitalWrite(stby, HIGH);
Trn*-1; // Make Trn positive
int Left = map(Spd, 0, 510, 0, 255);
int Right = map((Spd - Trn), 0, 510, 0, 255);
analogWrite(EnL, Left);
analogWrite(EnR, Right);
Serial.print("Left: ");
Serial.println(Left);
Serial.print("Right: ");
Serial.println(Right);
// delay(100);
}
}
so the robot works perfectly fine when it drives forward, backwards, or turns right. But when i put the joystick fully left, so we want the right motors to spin, in serial i get "right 255, left 0". However, the right motors just stall out.
digitalWrite(stby, HIGH);
analogWrite(EnL, 255);
analogWrite(EnR, 255);
digitalWrite(InA1, LOW);
digitalWrite(InA2, LOW);
digitalWrite(InB1, LOW);
digitalWrite(InB2, HIGH);
delay(2000);
when i try just this, the right motors spin just fine, and the left stay still. im stumped.