two xbee simple sketch

hello I am new to arduino

use:
Arduino + 1 + joystick xbee properly configured;
1 arduino with motor shield + one dc motor (forward / reverse) + xbee properly configured.

after several attempts I can not operate the receiver with the char “val” and “j”

////the sketch Arduino without xbee

int JoyStick_X = A0; // x

int j = A0;
int val = A0; // variable to store the value coming from the sensor
int Dir_A = 4;
int Speed_A = 5;

void setup ()
{
pinMode(Dir_A, OUTPUT);
pinMode(Speed_A, OUTPUT);
Serial.begin (9600); // 9600 bps
}
void loop() {

// Read location of joystick and calculate the distance and from center
val = analogRead(JoyStick_X); // read the value from the sensor
j = val - 517; // 517 is center positions - how far from center
j = abs(j); //absolute value

// put some bounds on j to keep PWM values useful
// below 100 the motor won’t move and PWM max is 255
if (j >= 510){
j = 510; //the most the PWM pin can do is 255
}
if (j <=200 && j>=10){
j=200; //below 100 PWM the motor makes a high pich sound and does not move
}
if (j <=10){
j=0; // below 10 the joystick is very close to center
}

//Run DC motor A based on analog input from joystick
if (val <= 500) {
digitalWrite(Dir_A, HIGH); // other direction
analogWrite(Speed_A, j/2); // PWM out (divide by 2 because max is 255)

}
if (val >= 550) {
digitalWrite(Dir_A, LOW); // other direction
analogWrite(Speed_A, j/2); // PWM out (divide by 2 because max is 255)

}

if (val <=520 && val >= 510) {
analogWrite(Speed_A, 0); // turn off if the joystick is in the center

}

{
// print values for debugging

Serial.print(val); // send numbers to PC so you can see what it going on
Serial.print(",");
Serial.println(j);

}
}

TX CODE

int JoyStick_X = A0; // x

int j = A0;
int val = A0; // variable to store the value coming from the sensor
int Dir_A = 4;
int Speed_A = 5;

void setup ()
{
pinMode(Dir_A, OUTPUT);
pinMode(Speed_A, OUTPUT);
Serial.begin (9600); // 9600 bps
}
void loop() {

// Read location of joystick and calculate the distance and from center
val = analogRead(JoyStick_X); // read the value from the sensor
j = val - 517; // 517 is center positions - how far from center
j = abs(j); //absolute value

{
// print values for debugging

Serial.print(val); // send numbers to PC so you can see what it going on
Serial.print(",");
Serial.println(j);

}
}

RX CODE

// Module KY023
// For more info see http://tkkrlab.nl/wiki/Arduino_KY-023_XY-axis_joystick_module

int j;
int val;
int Dir_A = 4;
int Speed_A = 5;

void setup ()
{
pinMode(Dir_A, OUTPUT);
pinMode(Speed_A, OUTPUT);
Serial.begin (9600); // 9600 bps
}

void loop() {

if (Serial.available() <= 550){
val = Serial.read();
j = Serial.read();

digitalWrite(Dir_A, HIGH); // other direction
analogWrite(Speed_A, j/2); // PWM out (divide by 2 because max is 255)
}

if (Serial.available() >= 550) {
val = Serial.read();
j = Serial.read();
digitalWrite(Dir_A, LOW); // other direction
analogWrite(Speed_A, j/2); // PWM out (divide by 2 because max is 255)
}

if (Serial.available() <=520 && val >= 510) {
val = Serial.read();
j = Serial.read();

analogWrite(Speed_A, 0); // turn off if the joystick is in the center
}

{
// print values for debugging

Serial.print(val); // send numbers to PC so you can see what it going on
Serial.print(",");
Serial.println(j);

}}

So, you have code that looks like crap. EVERY } goes on a line BY ITSELF. Use Tools + Auto Format before posting code, so it doesn’t look like you were drinking heavily while coding. Post code using code tags.

The code does something. You have not explained what it actually does.

You expect the code to do something. You have not explained what you expect it to do.

All that we can surmise is that what the code actually does is not what you expect.

if (Serial.available() <=520 && val >= 510) {

I can assure you that Serial.available() will ALWAYS be less than 520.

if (Serial.available() >= 550) {

I can assure you that Serial.available() will never be greater than (or equal) 550.

mini 4wd with ps2 joystick works with Arduino wire forward / reverse perfectly direction and pwd;
I try to put xbee … (xbee is configured correctly).
excuse for the code is the first post

joystick_easy_mod_rx_mod.ino (1.25 KB)

    if (Serial.available() >= 0)
    val = Serial.read();
    j = Serial.read();

It there is one byte available to read, it is NOT OK to read two bytes.

   if (j >= 510){

A byte will never contain a value of 510 or more.

   if (val <= 500) {

You can be certain that that will be true.

if (val >= 550) {

You can be certain that that will be never be true.

ok thanks,,,, I insert the 2 bytes!

I guessed it!

After I post the solution solved

post the solution??

TX CODE
int JoyStick_X = A0; // x

int val = A0; // TX variable to store the value coming from the sensor
int Dir_A = 4; // RX
int Speed_A = 5; //RX

void setup ()
{
pinMode(Dir_A, OUTPUT); //RX
pinMode(Speed_A, OUTPUT); //RX
Serial.begin (115200); // 9600 bps
}
void loop() {

val = analogRead(JoyStick_X); // read the value from the sensor

if (val <= 510) {
digitalWrite(Dir_A, HIGH); // other direction
analogWrite(Speed_A,50); // PWM out (divide by 2 because max is 255)
Serial.print(“C”);
}

if (val >= 530) {
digitalWrite(Dir_A, HIGH); // other direction
analogWrite(Speed_A,150); // PWM out (divide by 2 because max is 255)
Serial.print(“B”); }

if (val <=530 && val >= 510) {
analogWrite(Speed_A, 0); // turn off if the joystick is in the center
Serial.print(“G”);
}

Serial.print("");

}

RX CODE
#include <SoftwareSerial.h>

int Dir_A = 4; // RX
int Speed_A = 5; //RX

void setup()
{
pinMode(Dir_A, OUTPUT); //RX
pinMode(Speed_A, OUTPUT); //RX
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);

Serial.begin(115200);
}

void loop()
{

//tx char
char c = Serial.read();

if (c == ‘C’)

//FORWARD
{

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(Dir_A, HIGH); // other direction
analogWrite(Speed_A, 255); // PWM out (divide by 2 because max is 255)

}
if (c == ‘B’)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(Dir_A, LOW); // other direction
analogWrite(Speed_A, 150); // PWM out (divide by 2 because max is 255)
}
if (c == ‘G’)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);

analogWrite(Speed_A, 0); // PWM out (divide by 2 because max is 255)
}

}

  Serial.begin (115200); // 9600 bps

If you are going to have useless comments, they MUST be correct useless comments.

you like gossip?