Variable always equals zero in equation?

Hi,

I am novice at using Arduino and am probably reaching over my skill level with this project but would like to ask for some help.

I am attempting to send three potentiometer values and a button value from one Arduino to another using a nrf24 radio module. It seems to be working as I can read the transmitted potentiometer value and button value changes in the receiving Arduino's serial monitor. The only problem is that I can't seem to to do math with the received values. In function hammer() I try to multiply a received potentiometer value "hammerPOTval" by 9 to set the interval of a reoccurring event. This is then stored in hammerPOTtime.

hammerPOTtime = (hammerPOTval*9+1000)

It seems that " hammerPOTval " is always being treated as zero in the above equation. The serial.print of hammerPOTval will always read the correct received 0-1024 value though.

I think this is a data type issue but I can't seem to fix it. hammerPOTval set equal to hammerPOTval = data.hammerPOT which is a byte data type? I have been attempting to convert byte to long but have failed in pretty dramatic fashion.

Any help is greatly appreciated :slight_smile:

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#include <stdlib.h>


// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 (CE & CS)

RF24 radio(9,10);

// sets the role of this unit in hardware. Connect to GND to be the 'led' board receiver
// Leave open to be the 'remote' transmitter
const int role_pin = A4;


// Topology
//

// Single radio pipe address for the 2 nodes to communicate.
const uint64_t pipe = 0xE8E8F0F0E1LL;

//
// Role management
//
// Set up role.  This sketch uses the same software for all the nodes in this
// system.  Doing so greatly simplifies testing.  The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//

// The various roles supported by this sketch
typedef enum { role_remote = 1, role_led } role_e;

// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Remote", "LED Board"};

// The role of the current running sketch
role_e role;

//
// Payload
//


//
// Setup
//

void setup(void)
{

  // set up the role pin
  pinMode(role_pin, INPUT);
  digitalWrite(role_pin,HIGH);
  delay(20); // Just to get a solid reading on the role pin

  // read the address pin, establish our role
  if ( digitalRead(role_pin) )
    role = role_remote;
  else
    role = role_led;

  //
  // Print preamble
  //

  Serial.begin(115200);
  printf_begin();
  printf("\n\rRF24/examples/led_remote/\n\r");
  printf("ROLE: %s\n\r",role_friendly_name[role]);

  //
  // Setup and configure rf radio
  //

  radio.begin();

  //
  // Open pipes to other nodes for communication
  //

  // 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.

  if ( role == role_remote )
  {
    radio.openWritingPipe(pipe);
  }
  else
  {
    radio.openReadingPipe(1,pipe);
  }

  //
  // Start listening
  //

  if ( role == role_led )
    radio.startListening();

  //
  // Dump the configuration of the rf unit for debugging
  //

  radio.printDetails();



}

unsigned long previousMillis = 0;
int x = 0;
//const long interval 0; ///this is the interval at whihc the hammer will strike

    int rotatePOTval;
    int heightPOTval;
    unsigned long hammerPOTval;
    int MANval;
    

    int d3LED = 3;
    int d4LED = 4;
    int hammerpin = 8;
    int hammerPOTtime = 0;//set integer to hold value
    int d1 = 0;
    
void loop(void)
{



  
  unsigned long myTime;
  myTime = millis();
  
  if ( role == role_remote ) //*******************************************************HAND REMOTE****************************************************
  {

  int MAN = 3;
  pinMode(MAN,INPUT_PULLUP);
  
  struct MyData{
    int rotatePOT;
    int heightPOT;
    int hammerPOT;
    int MAN;
    };
    MyData data;
   
      data.rotatePOT = analogRead(A1);
      data.heightPOT = analogRead(A2);
      data.hammerPOT = analogRead(A3);
      data.MAN = digitalRead(3);      
  
    radio.write(&data, sizeof(MyData));
    
  }

 
  // ******************************************************************************HAND REMOTE****************************************************


  if ( role == role_led ) //*******************************************************RECEIVER****************************************************
  {
    int rotatePOTval;
    int heightPOTval;
    int hammerPOTval;
    int MANval;
    

    int d3LED = 3;
    int d4LED = 4;
    int hammerpin = 8;
    unsigned long hammerPOTtime = 0;//set long to hold value
    int d1 = 0;

    pinMode(d3LED,OUTPUT);
    pinMode(d4LED,OUTPUT);
    pinMode(hammerpin,OUTPUT);

    struct MyData{
      int rotatePOT;
      int heightPOT;
      int hammerPOT;
      int MAN;
    };
    MyData data;


      if ( radio.available() )
      {

      struct MyData{
      int rotatePOT;
      int heightPOT;
      int hammerPOT;
      int MAN;
      };
      MyData data;

          radio.read(&data, sizeof(MyData));

          Serial.println("rotate pot");
          Serial.println(data.rotatePOT);
          //Serial.println(rotatePOTval);
          Serial.println("height pot");
          Serial.println(data.heightPOT);
          Serial.println("hammer pot");
          Serial.println(data.hammerPOT);
          Serial.println("MAN");
          Serial.println(data.MAN);

          Serial.print("hammer time converted into long:  ");
          Serial.println(hammerPOTval);

          rotatePOTval = data.rotatePOT; // places read values into int varibles
          heightPOTval = data.heightPOT;
          hammerPOTval = data.hammerPOT;
          MANval = data.MAN;

          rotate(); //contains info on what to do with collected data 
          rise(); //contains info on what to do with collected data 
          hammer(); //contains info on what to do with collected data 
 
     }
    else{
      Serial.println("error, no radio");
      standby();
    }
    
    }
  
//*******************************************************RECEIVER**************************************************** 
  
}


//*******************************************************FUNCTIONS****************************************************

void standby(){

 // rotatePOTval = 0; 
 // heightPOTval = heightPOTval;
  //hammerPOTval = 0;
 // MANval = 1; //set to manual

}

void rotate(){
    //rotatePOTval  //value from 0 to 1023 (analog read 0-5v) transformed into value between 0-255 (analog write)
}

void rise(){
  
}

void hammer(){


  hammerPOTtime = (hammerPOTval*9+1000); //this is the interval at which the hammer will strike in ms (from 1hz [once a second) to 0.1 hz(once every 10 seconds))
  
  Serial.print("hammerPOTtime:");
  Serial.println(hammerPOTtime);
  
  long interval = hammerPOTtime; 
  Serial.print("interval:");
  Serial.println(interval);
  
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval){
    previousMillis = currentMillis;
    Serial.println("hammer on");
    digitalWrite(hammerpin,HIGH);
    delay(100); // gives time for hammer to strike
  }
  else{
    Serial.println("hammer off");
    digitalWrite(hammerpin,LOW);
  }
  
}

//*******************************************************FUNCTIONS****************************************************

You may review the data types from the following Table:

hammerPOTval is declare twice in your program, once at global scope
unsigned long hammerPOTval;
and again inside the test for the role

    int hammerPOTval;
hammerPOTtime = (hammerPOTval * 9 + 1000); //this is the interval at which the hammer will strike in ms (from 1hz [once a second) to 0.1 hz(once every 10 seconds))

When this line of code is executed, which of the 2 different hammerPOTval variables, and hence value, is being used ?

Good catch, I guess it should always be int since analog read is an int data type.

eg98:
Good catch, I guess it should always be int since analog read is an int data type.

Not just a matter of type. If you have a local variable of the same name as a global variable, the local one will be used. For that reason, I recommend that ALL global variable names start with a capital letter and all local variable names start with a lowercase letter. That makes it more clear which kind of variable you intended.

Hi everyone,

Want to update that the problem has been solved. It appears that the variable hammerPOTval was being reset to zero when the hammer() function was called.

The solution was to repeat struct, radio read, and hammerPOTval = data.hammerPOT; inside of the hammer() function.

eg98:
It appears that the variable hammerPOTval was being reset to zero when the hammer() function was called.

NO that was not the problem :wink:

The solution was to repeat struct, radio read, and hammerPOTval = data.hammerPOT; inside of the hammer() function.

Which makes this a stupid solution :wink:

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