Code not returning the results expected

Hey there,

I’m trying to write a program for a handheld unit to generate 4 values based on joystick positions and get 1 value from the user based on a preset then send the data to a receiver using the nRF24l01 units.

Everything about the transmitter code seems to work except for actually transmitting the values. Though it could be transmitting and my receiver could be errored.

TX CODE

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

//Device
int confirmled = 4;
int sendled = 5;
int busyled = 6;
int idleled = 7;
int gobutton = 8;
int xpot = 0;
int ypot = 1;
int xjoy = 2;
int yjoy = 3;
int recspup = 2;
int recspdn = 3;

//NRF24
const int pinCE = 9;
const int pinCSN = 10;
RF24 COM(pinCE, pinCSN);
const byte wAddress[10] = {'H', 'H', 'c', 'n', 't', 'r', 'l', 'r', '2', '1',};
const byte rAddress[10] = {'H', 'H', 'c', 'n', 't', 'r', 'l', 'r', '1', '2',};

//Variables
int xpotval;
int ypotval;
int xjoyval;
int yjoyval;
//int senslev;
int recspval = 30;
int action;
int i;
int stat;

void setup() {
  lcd.init();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Initializing");
  COM.begin();  //Start the nRF24 module
  COM.openWritingPipe(wAddress);
  lcd.print(".");
  pinMode(confirmled, OUTPUT);
  pinMode(sendled, OUTPUT);
  pinMode(busyled, OUTPUT);
  pinMode(idleled, OUTPUT);
  pinMode(gobutton, INPUT_PULLUP);
  pinMode(recspup, INPUT);
  pinMode(recspdn, INPUT);
  attachInterrupt(digitalPinToInterrupt(recspup), increaseval, FALLING);
  attachInterrupt(digitalPinToInterrupt(recspdn), decreaseval, FALLING);
  lcd.print(".");
  delay(750);
  lcd.print(".");
  lcd.setCursor(0, 1);
  lcd.print("Complete!");
  delay(1500);
}

void loop() {
  xpotval = analogRead(xpot);
  xpotval = map ( xpotval, 0, 1024, 0, 100);
  ypotval = analogRead(ypot);
  ypotval = map ( ypotval, 0, 1024, 0, 100);
  xjoyval = analogRead(xjoy);
  xjoyval = map ( xjoyval, 0, 1024, 0, 100);
  yjoyval = analogRead(yjoy);
  yjoyval = map ( yjoyval, 0, 1024, 0, 100);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("X: ");
  lcd.print(xpotval);
  lcd.print("      ");
  lcd.print("Y: ");
  lcd.print(ypotval);
  lcd.setCursor(0, 1);
  lcd.print("P:");
  lcd.print(xjoyval);
  lcd.print(" ");
  lcd.print("T:");
  lcd.print(yjoyval);
  lcd.print(" ");
  lcd.print("RS:");
  lcd.print(recspval);

  action = digitalRead(gobutton);
  if (action == LOW) {
    digitalWrite(confirmled, HIGH);
    digitalWrite(sendled, LOW);
    digitalWrite(idleled, LOW);
    i = 5;
    delay(500);
    while (i > 0) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Upload Data?");
      lcd.setCursor(0, 1);
      lcd.print(i);
      action = digitalRead(gobutton);
      if (action == LOW) {
        senddata(xpotval, ypotval, xjoyval, yjoyval, recspval);
      }
      i = --i;
      delay(1000);
    }
    digitalWrite(confirmled, LOW);
  }
  delay(100);
}

void senddata (int x, int y, int p, int t, int s) {
  digitalWrite(sendled, HIGH);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Sending Data");
  delay(250);
  lcd.setCursor(0, 1);
  lcd.print(x);
  delay(250);
  lcd.print(",");
  lcd.print(y);
  delay(250);
  lcd.print(",");
  lcd.print(p);
  delay(250);
  lcd.print(",");
  lcd.print(t);
  delay(250);
  lcd.print(",");
  lcd.print(s);
  delay(250);
  float dataToSend[5];
  dataToSend[0] = x;
  dataToSend[1] = y;
  dataToSend[2] = p;
  dataToSend[3] = t;
  dataToSend[4] = s;
  COM.write(&dataToSend, sizeof(dataToSend));
  COM.openReadingPipe(1, rAddress);
  COM.startListening();
  delay(1000);
  digitalWrite(sendled, LOW);
  if (COM.available()) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(x);
    lcd.print(",");
    lcd.print(y);
    lcd.print(",");
    lcd.print(p);
    lcd.print(",");
    lcd.print(t);
    lcd.print(",");
    lcd.print(s);
    char dataToReceive[1];
    dataToReceive[0] = 'u';
    while (dataToReceive[0] != 'b') {
      COM.read(&dataToReceive, sizeof(dataToReceive));
      stat = dataToReceive[0];
      lcd.setCursor(0, 1);
      lcd.print(stat);
    }
    if (stat == 'b') {
      while (stat != 'i') {
        digitalWrite(busyled, HIGH);
        COM.read(&dataToReceive, sizeof(dataToReceive));
        stat = dataToReceive[0];
        lcd.setCursor(0, 1);
        lcd.print(stat);
      }
      if (stat == 'i') {
        digitalWrite(busyled, LOW);
        digitalWrite(idleled, HIGH);
        lcd.setCursor(0, 1);
        lcd.print(stat);
      }
    }
  }
}


void increaseval() {
  recspval = recspval + 1;
}

void decreaseval() {
  recspval = recspval - 1;
}

RX Code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//NRF24
const int pinCE = 9;
const int pinCSN = 10;
RF24 COM(pinCE, pinCSN);
const byte wAddress[10] = {'H', 'H', 'c', 'n', 't', 'r', 'l', 'r', '2', '1',};
const byte rAddress[10] = {'H', 'H', 'c', 'n', 't', 'r', 'l', 'r', '1', '2',};

int x;
int oldx;
int y;
int oldy;
int p;
int oldp;
int t;
int oldt;
int s;

void setup() {
  Serial.begin(9600);
  COM.begin();  //Start the nRF24 module
  COM.openReadingPipe(1, rAddress);
  COM.startListening();
}

void loop() {
  int dataToReceive[5];
  COM.read(&dataToReceive, sizeof(dataToReceive));
  dataToReceive[0] = x;
  dataToReceive[1] = y;
  dataToReceive[2] = p;
  dataToReceive[3] = t;
  dataToReceive[4] = s;
  if (x != oldx || y != oldy || p != oldp || t != oldt) {
    Serial.print("x:");
    Serial.println(x);
    Serial.print("y:");
    Serial.println(y);
    Serial.print("p:");
    Serial.println(p);
    Serial.print("t:");
    Serial.println(t);
    Serial.print("s:");
    Serial.println(s);
    COM.stopListening();
    COM.openWritingPipe(wAddress);
    char dataToSend[1];
    dataToSend[0] = "b";
    COM.write(&dataToSend, sizeof(dataToSend));
    Serial.println("sent back busy signal");
    delay(s);
    dataToSend[0] = "i";
    COM.write(&dataToSend, sizeof(dataToSend));
    Serial.println("sent back idle signal");
  }
  Serial.println("Nothing new");
}

Thanks for any help in advance!

In the Tx code

  float dataToSend[5];

However, in the Rx code

  int dataToReceive[5];

The data types must match and be appropriate type for the data being sent

      i = --i;

ERROR: This statement produces undefined results! Use one of these four valid statements to decrement 'i':

   i = i - 1;
  i -= 1;
  --i;
  i--;

DO NOT PRE- OR POST-DECREMENT (or increment) the variable to which you are assigning a value!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.