if/data help

i know im doing something wrong with the data sending but not sure how to set it up and also in my if statments at first it was sending the 111

if ( digitalRead(SW1) == HIGH)
{
data[0] = 111;
Serial.println(“Forward”);
}

then after adding
if ( digitalRead(SW1) == HIGH)
{
data[0] = 111;
Serial.println(“Forward”);
}
if ( digitalRead(SW2) == HIGH)
{
data[0] = 122;
Serial.println(“Backwards”);
}

it only sends 122

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);

uint8_t data[2] ; //buffer to store received data
const uint8_t buffer_size = sizeof(data);
const int tx_led=2;// transmission indicator
const int transmit_pin=6;
 int transmit_enable;
 int SW1=7;//forward
 int SW2=3;//backward
 int SW3=4;//left
 int SW4=5;//right
 // Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  
  Serial.begin(57600);
  printf_begin();
  printf("\n\rRF24/examples/led_remote/\n\r");

  radio.begin();
  radio.openWritingPipe(pipe);
  // Dump the configuration of the rf unit for debugging
   radio.printDetails();
  // Set up buttons
   pinMode(SW1, INPUT_PULLUP);
   pinMode(SW2,INPUT_PULLUP);
   pinMode(SW3, INPUT_PULLUP);
   pinMode(SW4,INPUT_PULLUP);
   pinMode(transmit_enable,INPUT);
  pinMode(tx_led,OUTPUT);
  digitalWrite(tx_led,LOW);

}

//
// Loop
//

void loop(void)
{

  //Direction
if ( digitalRead(SW1) == HIGH)
  {
    data[0] = 111;
  Serial.println("Forward");
  }
  if ( digitalRead(SW2) == HIGH)
  {
    data[0] = 122;
  Serial.println("Backwards");
  }
 /*Steering
  if ( digitalRead(SW3) == HIGH)
  {
    data[1] = 200;
  Serial.println("Left");
  }
  if ( digitalRead(SW4) == HIGH)
  {
    data[1] = 222;
  Serial.println("Right");
  }*/
  else
  {
  data[0] = 10;
  }
   if((data[0]>153 || data[0] <=149) ||  (data[1]>=92 || data[1]<88))
  {
    //printf("Now sending...");
    bool ok = radio.write( data, buffer_size );
    //if (ok)
    printf("ok\n\r");
    // else
    printf("failed\n\r");
    // delay(15);
    digitalWrite(tx_led,HIGH); //turn led on after transmission
  }
  else digitalWrite(tx_led,LOW);//keep led off when no transmission
}

and it registers both buttons being HIGH but wont send the first data it its pressed

If the SW2 pin is HIGH, then it overwrites what the first if statement put there. That's what you told it to do. data[0] cannot possibly have two different values at the same time. The code runs from top to bottom. If both pins are HIGH, it sets data[0] to 111 and then immediately changes it to 122 in the next block.

I beleave I should use something like this

Int data[] = {111,122};

And call it by data[0] or data[1] ????

merkzilla:
I beleave I should use something like this

Int data = {111,122};

And call it by data[0] or data[1] ???

Sure. That way, you can send instructions to move forwards and backwards at the same time.

Good luck implementing that on the receiving end.

merkzilla: I beleave I should use something like this

Int data[] = {111,122};

And call it by data[0] or data[1] ????

Are you kidding us? Why would setting element zero do anything to element 1?

Please go read some c++ tutorials.

merkzilla:
and it registers both buttons being HIGH but wont send the first data it its pressed

You have not explained what you want to happen.

Your code suggests that you want to send one value if one switch is pressed and a different value if the other switch is pressed.
However if you want to send both values then you also need something to send when a switch is not pressed - perhaps send “<0,0>” when no switch is pressed and “<111,0>” when only the first switch is pressed, etc.

Have a look at serial input basics which shows how to receive data - and, obviously, you have to send data in a matching style.

…R

so this code for sending works on the micro

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9, 10);

uint8_t data[2] ; //buffer to store received data
const uint8_t buffer_size = sizeof(data);
const int tx_led = 2; // transmission indicator
const int transmit_pin = 6;
int transmit_enable;
int SW1 = 7; //forward
int SW2 = 3; //backward
int SW3 = 4; //left
int SW4 = 5; //right
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{

  Serial.begin(57600);
  printf_begin();
  printf("\n\rRF24/examples/led_remote/\n\r");

  radio.begin();
  radio.openWritingPipe(pipe);
  // Dump the configuration of the rf unit for debugging
  radio.printDetails();
  // Set up buttons
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  pinMode(transmit_enable, INPUT);
  pinMode(tx_led, OUTPUT);
  digitalWrite(tx_led, LOW);

}

//
// Loop
//

void loop(void)
{

  //Direction
  if ( digitalRead(SW1) == HIGH)
  {
    data[0] = 111;
    Serial.println("Forward");
  }
 else if ( digitalRead(SW2) == HIGH)
  {
    data[0] = 122;
    Serial.println("Backwards");
  }
   else
  {
    data[0] = 10;
  }
  //Steering
  if ( digitalRead(SW3) == HIGH)
   {
     data[1] = 200;
   Serial.println("Left");
   }
   else if ( digitalRead(SW4) == HIGH)
   {
     data[1] = 222;
   Serial.println("Right");
   }
  else
  {
    data[1] = 10;
  }
 if((data[0]>153 || data[0] <=149) ||  (data[1]>=200 || data[1]<250))
  {
    //printf("Now sending...");
    bool ok = radio.write( data, buffer_size );
    //if (ok)
    printf("ok\n\r");
    // else
    printf("failed\n\r");
    // delay(15);
    digitalWrite(tx_led,HIGH); //turn led on after transmission
  }
  else digitalWrite(tx_led,LOW);//keep led off when no transmission
}

now it seems on my receaving side it prints all data but only follows through with the left and right turn

#include <MotorDriver.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
// Hardware configuration
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(48, 49);
// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;
//
uint8_t received_data[2];
uint8_t num_received_data = sizeof(received_data);
const int rx_led = 42;

void setup(void)
{
  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(200, MOTORA);

  pinMode(rx_led, OUTPUT);
  digitalWrite(rx_led, LOW);
  delay(3000); //wait until the esc starts in case of Arduino got power first
  Serial.begin(57600);
  printf_begin();
  printf("\n\rRF24/Reciever/\n\r");
  radio.begin(); //Begin operation of the chip.
  // This simple sketch opens a single pipes for these two nodes to communicate
  // back and forth.  One listens on it, the other talks to it.
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  //
  radio.printDetails();
}


void loop(void)
{
  delay(100);
  // if there is data ready
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( received_data, num_received_data );
    }
    //direction
    if (received_data[0] == 111)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
    }
    else if (received_data[0] == 122)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
    }
     else if (received_data[0] == 10)
    {
      digitalWrite(rx_led, LOW);
      motordriver.stop();
    }
    Serial.println(received_data[0]);
    //steering
     if (received_data[1] == 200)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goLeft();
    }
   else if (received_data[1] == 222)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goRight();
    }
   else if (received_data[1] == 10)
    {
      digitalWrite(rx_led, LOW);
      motordriver.stop();
    }
    Serial.println(received_data[0]);
    Serial.println(received_data[1]);
  }
}

What does the receiver print that it received?

It's printing the values that I send 111,122,200,222,10 when I push the buttons on the transmitter (well 10 is when nothing is pushed) 111 is forward 122 revers 200 left 222 right and 10 is for all stop. after adding the left and right to the receiver code it seems to only do those functions. But yet it still receives all values I send

merkzilla: so this code for sending works on the micro

Can you just explain what you are trying to do so we don't have to read through your code to figure it out?

...R

Yes… I have a Arduino micro I’m using as a controller with 4 push buttons currently forward,backward,left, and right. When I push a button it sends a value to the Arduino mega that will be the remote car.the values are data[o] 111 and 122 also data[1] 200 and 222 in serial monitor all values are being sent and received but the corresponding function only works for left right unless I remove those then the reverse and forward work. I did observe that the receive led is very dim and I hear the motor trying to work but doesn’t when unless again I take out the left right functions then it works fine

im assuming my error is with in this section of code on the recever

void loop(void)
{
  delay(100);
  // if there is data ready
  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( received_data, num_received_data );
    }
    //direction
    if (received_data[0] == 111)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
    }
    else if (received_data[0] == 122)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
    }
    else if (received_data[0] == 10)
    {
      digitalWrite(rx_led, LOW);
      motordriver.stop();
    }
    else if (received_data[1] == 200)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goLeft();
    }
    else if (received_data[1] == 222)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goRight();
    }
    else if (received_data[1] == 10)
    {
      digitalWrite(rx_led, LOW);
      motordriver.stop();
    }
    Serial.println(received_data[0]);
    Serial.println(received_data[1]);
  }
}

The error is in your understanding of how arrays work.

A little less hack and post, and a little more reading of C++ tutorials and you'd be able to do this.

Could you point me to a good resource for this? I understand my project might be a little too ambitious for a beginner but this project is the reason I'm doing i got into Arduino and I appreciate all the help I'm trying to complete this fairly quickly In between work and my kids

This seems like an obvious first step

merkzilla: Yes... I have a Arduino micro I'm using as a controller with 4 push buttons currently forward,backward,left, and right. When I push a button it sends a value to the Arduino mega that will be the remote car.the values are data[o] 111 and 122 also data[1] 200 and 222

That is better, but it is still not completely clear.

If you only need to send one value at any one time why are you thinking of having data[ 0 ] and data[ 1 ]

Did you look at the examples in serial input basics ?

...R

well 10 is when nothing is pushed

Knock THAT crap off. Nothing is pushed most of the time, so you are spamming the receiver. You should ONLY send data when there is MEANINGFUL data to send. 10 is NOT meaningful data.

I would like to steer while moving so I thought I might need two sets of data for that...? I have everything working although it's in the same set of data so I can only do one function at a time