I'm am a beginner, so I'm sorry I have written this wrong.
Is this piece of code correct? If not where?
Transmitter:
#include <SPI.h>
#include "RF24.h"
int* Msg, mSg, msG;
RF24 radio(9, 10);
const uint8_t pipe = 1;
void setup() {
// put your setup code here, to run once:
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(pipe);
}
void loop() {
// put your main code here, to run repeatedly:
Msg = analogRead(A1);
radio.write(Msg, sizeof(Msg));
mSg = analogRead(A2);
radio.write(mSg, sizeof(mSg));
msG = analogRead(A3);
radio.write(msG, sizeof(msG));
delay(100);
}
Receiver:
#include <SPI.h>
#include "RF24.h"
#define in1 2
#define in2 3
#define in3 4
#define in4 5
#define in5 A2
#define in6 A3
#define in7 A4
#define in8 A5
const int maxMsgLength = 64;
char Msg[maxMsgLength];
const int maxmSgLength = 64;
char mSg[maxmSgLength];
const int maxmsGLength = 64;
char msG[maxmsGLength];
RF24 radio(9, 10);
const uint8_t pipe = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(1, pipe);
radio.startListening();
}
void loop() {
// put your main code here, to run repeatedly:
if (radio.available()) {
radio.read(Msg, maxMsgLength - 1);
radio.read(mSg, maxmSgLength - 1);
radio.read(msG, maxmsGLength - 1);
}
int xAxis = Msg;
int yAxis = mSg;
int YAxis = msG;
if (xAxis < 450) {
// Forward
digitalWrite(in1, 1);
digitalWrite(in2, 0);
digitalWrite(in3, 1);
digitalWrite(in4, 0);
Serial.println("Forward");
}
else if (xAxis > 550) {
// Backward
digitalWrite(in1, 0);
digitalWrite(in2, 1);
digitalWrite(in3, 0);
digitalWrite(in4, 1);
Serial.println("Backward");
}
else {
// Stop
digitalWrite(in1, 0);
digitalWrite(in2, 0);
digitalWrite(in3, 0);
digitalWrite(in4, 0);
Serial.println("Stop");
}
if (yAxis < 450) {
// Left
digitalWrite(in5, 1);
digitalWrite(in6, 0);
Serial.println("Left");
}
else if (yAxis > 550) {
// Right
digitalWrite(in5, 0);
digitalWrite(in6, 1);
Serial.println("Right");
}
else {
// Stop
digitalWrite(in5, 0);
digitalWrite(in6, 0);
Serial.println("Stop");
}
if (yAxis < 450) {
// Flipper Up
digitalWrite(in7, 1);
digitalWrite(in8, 0);
Serial.println("Up");
}
else if (yAxis > 550) {
// Flipper Down
digitalWrite(in7, 0);
digitalWrite(in8, 1);
Serial.println("Down");
}
else {
// Stop
digitalWrite(in7, 0);
digitalWrite(in8, 0);
Serial.println("Mid");
}
delay(250);
}
Edit:
Transmitter:
#include <SPI.h>
#include "RF24.h"
int x, y, f;
RF24 radio(9, 10);
const uint8_t pipe = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(pipe);
}
void loop() {
// put your main code here, to run repeatedly:
x = analogRead(A1);
radio.write(x, sizeof(x));
y = analogRead(A2);
radio.write(y, sizeof(y));
f = analogRead(A3);
radio.write(f, sizeof(f));
delay(100);
}
Receiver:
#include <SPI.h>
#include "RF24.h"
#define in1 2
#define in2 3
#define in3 4
#define in4 5
#define in5 A2
#define in6 A3
#define in7 A4
#define in8 A5
int x;
int y;
int f;
RF24 radio(9, 10);
const uint8_t pipe = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(1, pipe);
radio.startListening();
}
void loop() {
// put your main code here, to run repeatedly:
if (radio.available()) {
radio.read(&x, sizeof(x));
radio.read(&y, sizeof(y));
radio.read(&f, sizeof(f));
}
if (x < 450) {
// Forward
digitalWrite(in1, 1);
digitalWrite(in2, 0);
digitalWrite(in3, 1);
digitalWrite(in4, 0);
Serial.println("Forward");
}
else if (x > 550) {
// Backward
digitalWrite(in1, 0);
digitalWrite(in2, 1);
digitalWrite(in3, 0);
digitalWrite(in4, 1);
Serial.println("Backward");
}
else {
// Stop
digitalWrite(in1, 0);
digitalWrite(in2, 0);
digitalWrite(in3, 0);
digitalWrite(in4, 0);
Serial.println("Stop");
}
if(x > 1023)x = 1023;
if(x < 0)x = 0;
if (y < 450) {
// Left
digitalWrite(in5, 1);
digitalWrite(in6, 0);
Serial.println("Left");
}
else if (y > 550) {
// Right
digitalWrite(in5, 0);
digitalWrite(in6, 1);
Serial.println("Right");
}
else {
// Stop
digitalWrite(in5, 0);
digitalWrite(in6, 0);
Serial.println("Stop");
}
if(y > 1023)y = 1023;
if(y < 0)y = 0;
if (f < 450) {
// Flipper Up
digitalWrite(in7, 1);
digitalWrite(in8, 0);
Serial.println("Up");
}
else if (f > 550) {
// Flipper Down
digitalWrite(in7, 0);
digitalWrite(in8, 1);
Serial.println("Down");
}
else {
// Stop
digitalWrite(in7, 0);
digitalWrite(in8, 0);
Serial.println("Mid");
}
if(f > 1023)f = 1023;
if(f < 0)f = 0;
Serial.println(x);
Serial.println(y);
Serial.println(f);
delay(250);
}
Thank you for your time.
-
1Does it behave not the way you want? Because we don't know that. And if it does not behave like you want, describe exactly what you expect, and what you notice.Michel Keijzers– Michel Keijzers2019年08月27日 13:59:26 +00:00Commented Aug 27, 2019 at 13:59
-
So, I want it to take the values from the joysticks and send it to the receiver side. There the joystick values must be taken and l298n must behave accordingly. If the joystick is pulled upward then the motors should go forward and vice versa. Same with the x-axis, if the joystick is pulled on the left side the motor must rotate to the left-hand side.Blaziken– Blaziken2019年08月27日 15:05:07 +00:00Commented Aug 27, 2019 at 15:05
-
Ok ... and does your program behave like that? If yes, why ask the question? If not, describe what you see and what you expect.Michel Keijzers– Michel Keijzers2019年08月27日 15:06:19 +00:00Commented Aug 27, 2019 at 15:06
-
So the program does not behave like that.Blaziken– Blaziken2019年08月27日 15:07:42 +00:00Commented Aug 27, 2019 at 15:07
-
The joystick values do not get transmitted and on the receiver, the values are displayed as 0.Blaziken– Blaziken2019年08月27日 15:08:36 +00:00Commented Aug 27, 2019 at 15:08
1 Answer 1
I see (at least one ) problem.
First remove the * from:
int* Msg, mSg, msG;
Because it should store integers (numbers), not pointers to numbers.
Also, make the variables better. These almost identical names are quite confusing (name them x, y or z or a better name).
What you are doing is sending the 3 integers as integers (meaning that each integer is 2 bytes), so you send 6 bytes.
But in the receiver, you read characters, you have to replace this by:
- Changing
char
toint
for the 3 variables in the receiver side - Also here, change the names
- Remove the constants for the length (64)
Instead read the code as you write it:
int xAxis; radio.read(&xAxis, sizeof(int));
& means: address, the read function stores the value in xAxis (so you don't have to copy it afterwards). Also print out the value after the read so you can check it.
-
Thank you this program does work. But even if the joysticks are in the middle the value shown in the serial monitor is Forward, Left, and Up(the first if statements in each argument). then too, the transmission glitches.Blaziken– Blaziken2019年08月27日 15:51:30 +00:00Commented Aug 27, 2019 at 15:51
-
That depends on the values you get (did you check the values of x, y and zAxis?)Michel Keijzers– Michel Keijzers2019年08月27日 15:54:20 +00:00Commented Aug 27, 2019 at 15:54
-
The values are coming insanely differentBlaziken– Blaziken2019年08月27日 16:00:26 +00:00Commented Aug 27, 2019 at 16:00
-
When the joystick is pulled upward the values go like 129, 0, 0 then 0, 129, 0 and then 0, 0, 129. But when it is pulled backward then the values are 16382, 0, -13872 and so.Blaziken– Blaziken2019年08月27日 16:02:19 +00:00Commented Aug 27, 2019 at 16:02
-
Added if (x/y/f < 0)x/y/f = 0; and if (x/y/f > 1023)x/y/f = 1023;Blaziken– Blaziken2019年08月27日 16:10:10 +00:00Commented Aug 27, 2019 at 16:10
Explore related questions
See similar questions with these tags.