How to compare floats and ints

Hi all,

I have a function that takes a global variable named weight. weight needs to be assigned a name and a value based off of the weight itself. I need to compare floats and ints. For the sake of simplicity and testing i re write the value of weight in the function. No matter what I do the value is always --Null–.

Please let me know what I can do to fix my code.

Thank you

void Bins()
{
Serial.println("Start of the Bins");  
int LightSensorB = 0;
LightSensorB = digitalRead(3);
String bolt= "--Null--" ;
weight = 32.5;
int val = 0;
// once the weight has been calculated we need to identify the bolt and then say the absolute position

// I'll make this part once I know more about the weights I expect to see

if ((weight >= 30) && (weight<=35))
{
  val = 0;
  String bolt = "AL-0.500" ;
}

if ((weight >= 39) && (weight<=45))
{
  val = 14;
  String bolt = "AL-0.750" ;
}
if ((weight >= 46) && (weight<=52))
{
  val = 28;
  String bolt = "AL-1.000" ;
}
if ((weight >= 74) && (weight<=80))
{
  val = 42;
  String bolt = "AG-1.000" ;
}
if ((weight >= 83) && (weight<=85))
{
  val =56;
  String bolt = "AG-1.250" ;
}
if ((weight >= 86) && (weight<=90))
{
  val = 70;
  String bolt = "AJ-0.875" ;
}
if ((weight >= 91) && (weight<=95))
{
  val = 84;
  String bolt = "AJ-1.000" ;
}
if ((weight >=106 ) && (weight<=110))
{
  val = 98;
  String bolt = "AJ-1.250" ;
}
if ((weight >=140 ) && (weight<=150))
{
  val = 112;
  String bolt = "AJ-1.750" ;
}
if ((weight >=157 ) && (weight<=165))
{
  val = 126;
  String bolt = "AJ-2.000" ;
}
if ((weight >=209 ) && (weight<=220))
{
  val = 140;
  String bolt = "AJ-2.750" ;
}
if ((weight >=225 ) && (weight<=235))
{
  val = 154;
  String bolt = "AJ-3.000" ;
}
if ((weight >=240 ) && (weight<=265))
{
  val = 172;
  String bolt = "AG-4.500" ;
}

else
{
  val = 186;
  String bolt = "Uknown";
}
Serial.println("Bolt is");
Serial.println(bolt);

Also note the val = 186 always.

Post ALL of your code.

void Bins()
{
Serial.println("Start of the Bins"); 
int LightSensorB = 0;
LightSensorB = digitalRead(3);
String bolt= "--Null--" ;
weight = 32.5;
int val = 0;
// once the weight has been calculated we need to identify the bolt and then say the absolute position

// I'll make this part once I know more about the weights I expect to see

if ((weight >= 30) && (weight<=35))
{
  val = 0;
  bolt = "AL-0.500" ;
}

if ((weight >= 39) && (weight<=45))
{
  val = 14;
  bolt = "AL-0.750" ;
}
if ((weight >= 46) && (weight<=52))
{
  val = 28;
  bolt = "AL-1.000" ;
}
if ((weight >= 30) && (weight<=35))
{
  val = 0;
  String bolt = "AL-0.500" ;
}

A variable (String bolt) that is declared inside of a function is local to that function. When the function exits (is finished) the variable no longer exists.

String bolt= "--Null--" ;

Here a variable of the same name is declared globally. The local variable and the global variable are not the same. They are actually 2 different variables. The local version only exists within the function so when you print “bolt”, outside of the function, you are printing the global version that has the value “–NULL–”. Remove the String data type specifier from all of the if statements so that you are changing the global version.

Edit: I guess is should say “if block” instead of “function” to be more accurate.

jremington:
Post ALL of your code.

Enough was posted to see the problem

Okay so I updated my code so at the beginning of the program weight is set up as a global and it is 32.2

I have also taken out the strings. My output is now

Start of the Bins
Bolt is
Uknown
186

What can I do to better pass the global variable into the funtion?

// libraries needed for operation
//Scale imports
#include <hx711.h>
Hx711 scale(A2, A3);
//------------------
//Stepper imports
#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 8, 9, 10, 11);  //Pins for the motor driver going from 1 -> 4

//Global Variable Zone-------------
float weight = 32.2; // for the weight reading

int previous = 0; // for the stepper motor setup

//---------------------------------------------------------------------------------------------------------

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  // Pin Names are initialized here
  //Drum and CMD----------------------------------------------------------------------------------

  pinMode(13, OUTPUT); // Drum and CMD Transistor
  digitalWrite(13, HIGH);
  pinMode(12, OUTPUT); // Vibration Transistor
  digitalWrite(12, HIGH);

  
  //Meterwheel/COPA------------------------

  pinMode(4, OUTPUT); // Meter Transistor
  pinMode(7, OUTPUT); // Brush Transistor
  pinMode(5, OUTPUT); // Valve
  pinMode(2, INPUT);  // Light Sensor A

  digitalWrite(4, LOW);
  digitalWrite(7, LOW);
  digitalWrite(5, LOW);
  

  //WeighStation------------------------
  pinMode(6, OUTPUT); // Stopper Transistor
  pinMode(3, INPUT);  // Light Sensor B

  digitalWrite(6, LOW);
  

  //BINS------------------------Note the pins are initalized in the stepper command in the above section

  stepper.setSpeed(30);
  

  
}
//--------------------------------------------------------------
// MAIN
void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
//MeterCopa();
//delay(200);
//WeighStation();
//delay(200);
Bins();
delay(200);
}
//------------------------------------------------------------------
//Meter wheel and copaslip ( Verrified on 4/10 at 5:46 pm)
void MeterCopa() // this section of the code is responsible for priming the bolts in the meter wheel and doing the COPA, but will not send the bolt forward
{
Serial.println("MeterCopa Start");
int LightSensorA = 0;
LightSensorA = digitalRead(2);
Serial.println("Priming the Bolt");
while(LightSensorA == HIGH) // this while loop will prime the bolt
{
  digitalWrite(4,HIGH);
  LightSensorA = digitalRead(2);
}

digitalWrite(4,LOW); // Bolt is primed no need to move the wheel anymore just need to run the CMD proccess
Serial.println("CopaSlipTime");
delay(200);
digitalWrite(5,HIGH);  // This will move the cylinder up
digitalWrite(7,HIGH);  // This will turn the mini motors on
delay(2000);  // This delay will have to change with time when the machine is made

digitalWrite(5,LOW);  // Release air P will bring cylinder down
digitalWrite(7,LOW);  // Turn off the COPA motors

Serial.println("END of MeterCopa");

}
//---------------------------------------------------------------------
//Weighstation
void WeighStation()
{
Serial.println("WeighStation Start");
float tarr = 0;
float measure = 0;
int LightSensorA = 0;
int LightSensorB = 0;

LightSensorA = digitalRead(2);
LightSensorB = digitalRead(3);

//Before anything starts we need to stop any incoming bolts
digitalWrite(6,LOW);
Serial.println("Tarr the Scale");
delay(500);
//First calibrate the scale
tarr = scale.getGram();
// Now send the next bolt through
Serial.print(tarr,1);
Serial.println("g");
Serial.println("Send the Bolt");
while(LightSensorA == LOW) // this small while loop will send the bolt away from the meter wheel
{
   digitalWrite(4,HIGH);
   LightSensorA = digitalRead(2);
}
// may need to add a small delay here
digitalWrite(4,LOW); // stop the meter wheel once the bolt is away
Serial.println("Bolt Sent");
// Right now the bolt is in transit and we need to wait for it to arrive
while(LightSensorB == HIGH) // need to wait till sensor says bolt is present
{
Serial.println("Waiting on Bolt"); 
delay(100);
LightSensorB = digitalRead(3);
}
delay(100);
// this while loop will end when the light sensor is LOW ( sees a bolt)
measure = scale.getGram();

weight = tarr - measure;
weight = abs(weight);
Serial.print("bolt weight in g ");

Serial.println(weight);

// the bolt has now been weighed and is now ready to leave.
Serial.println("WeighStation End");
}
//-----------------------------------------------------------------------------------------
//Bins
void Bins()
{
Serial.println("Start of the Bins");  
int LightSensorB = 0;
LightSensorB = digitalRead(3);
String bolt= "--Null--" ;
int val = 0;
// once the weight has been calculated we need to identify the bolt and then say the absolute position

// I'll make this part once I know more about the weights I expect to see

if ((weight >= 30) && (weight<=35))
{
  val = 0;
  bolt = "AL-0.500" ;
}

if ((weight >= 39) && (weight<=45))
{
  val = 14;
  bolt = "AL-0.750" ;
}
if ((weight >= 46) && (weight<=52))
{
  val = 28;
  bolt = "AL-1.000" ;
}
if ((weight >= 74) && (weight<=80))
{
  val = 42;
  bolt = "AG-1.000" ;
}
if ((weight >= 83) && (weight<=85))
{
  val =56;
  bolt = "AG-1.250" ;
}
if ((weight >= 86) && (weight<=90))
{
  val = 70;
  bolt = "AJ-0.875" ;
}
if ((weight >= 91) && (weight<=95))
{
  val = 84;
  bolt = "AJ-1.000" ;
}
if ((weight >=106 ) && (weight<=110))
{
  val = 98;
  bolt = "AJ-1.250" ;
}
if ((weight >=140 ) && (weight<=150))
{
  val = 112;
  bolt = "AJ-1.750" ;
}
if ((weight >=157 ) && (weight<=165))
{
  val = 126;
  bolt = "AJ-2.000" ;
}
if ((weight >=209 ) && (weight<=220))
{
  val = 140;
  bolt = "AJ-2.750" ;
}
if ((weight >=225 ) && (weight<=235))
{
  val = 154;
  bolt = "AJ-3.000" ;
}
if ((weight >=240 ) && (weight<=265))
{
  val = 172;
  bolt = "AG-4.500" ;
}

else
{
  val = 186;
  bolt = "Uknown";
}
Serial.println("Bolt is");
Serial.println(bolt);
Serial.println(val);

Serial.println("Stepper Motor Move now");
// now that the bolt and absolute position have been identifiied we need to move to that value ( lets call it val and will range 0 to 200)
stepper.step(val - previous); // go to the correct place
previous = val; // remember where you now are

Serial.println("Stepper in position");
// now we are in position and are ready to send the bolt away from the weigh station
digitalWrite(6,LOW);
Serial.println("Bolt in transit");

while(LightSensorB == LOW)
{
  digitalWrite(6,HIGH);
}
delay(100); // extra timer to make sure the bolt got away
digitalWrite(6,LOW); // stop bolts again

Serial.println("End of Bins");
}
int LightSensorA = 0;
LightSensorA = digitalRead(2);

Utterly pointless

int LightSensorA = digitalRead(2);

What can I do to better pass the global variable into the funtion?

Which variable?
Which function?

Thanks for the suggestion on the light sensor, however right now i am more concerned with the weight value being read properly

if ((weight >=240 ) && (weight<=265))
{
  val = 172;
  bolt = "AG-4.500" ;
}

else
{
 val = 186;
  bolt = "Uknown";
}

The else there is only associated with the last if. The last if is false so the else executes and makes val= 186 and bolt = “AG-4.500”.

To fix it make every if statement after the first one else if.

if ((weight >= 30) && (weight<=35))
{
  val = 0;
  bolt = "AL-0.500" ;
}

else if ((weight >= 39) && (weight<=45))
{
  val = 14;
  bolt = "AL-0.750" ;
}
else if ((weight >= 46) && (weight<=52))
{
  val = 28;
  bolt = "AL-1.000" ;
}

Ans so on. You should end up with one if, several else if and one else.

AWESOME,

thank you so much everyone for all the help. Changing the all the if to else if helped make it work!

(does anyone know how i mark this resolved)?

final code for anyone wondering

// libraries needed for operation
//Scale imports
#include <hx711.h>
Hx711 scale(A2, A3);
//------------------
//Stepper imports
#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 8, 9, 10, 11);  //Pins for the motor driver going from 1 -> 4

//Global Variable Zone-------------
float weight = 0; // for the weight reading

int previous = 0; // for the stepper motor setup

//---------------------------------------------------------------------------------------------------------

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  // Pin Names are initialized here
  //Drum and CMD----------------------------------------------------------------------------------

  pinMode(13, OUTPUT); // Drum and CMD Transistor
  digitalWrite(13, HIGH);
  pinMode(12, OUTPUT); // Vibration Transistor
  digitalWrite(12, HIGH);

  
  //Meterwheel/COPA------------------------

  pinMode(4, OUTPUT); // Meter Transistor
  pinMode(7, OUTPUT); // Brush Transistor
  pinMode(5, OUTPUT); // Valve
  pinMode(2, INPUT);  // Light Sensor A

  digitalWrite(4, LOW);
  digitalWrite(7, LOW);
  digitalWrite(5, LOW);
  

  //WeighStation------------------------
  pinMode(6, OUTPUT); // Stopper Transistor
  pinMode(3, INPUT);  // Light Sensor B

  digitalWrite(6, LOW);
  

  //BINS------------------------Note the pins are initalized in the stepper command in the above section

  stepper.setSpeed(30);
  

  
}
//--------------------------------------------------------------
// MAIN
void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
//MeterCopa();
//delay(200);
//WeighStation();
//delay(200);
Bins();
delay(200);
}
//------------------------------------------------------------------
//Meter wheel and copaslip ( Verrified on 4/10 at 5:46 pm)
void MeterCopa() // this section of the code is responsible for priming the bolts in the meter wheel and doing the COPA, but will not send the bolt forward
{
Serial.println("MeterCopa Start");
int LightSensorA = 0;
LightSensorA = digitalRead(2);
Serial.println("Priming the Bolt");
while(LightSensorA == HIGH) // this while loop will prime the bolt
{
  digitalWrite(4,HIGH);
  LightSensorA = digitalRead(2);
}

digitalWrite(4,LOW); // Bolt is primed no need to move the wheel anymore just need to run the CMD proccess
Serial.println("CopaSlipTime");
delay(200);
digitalWrite(5,HIGH);  // This will move the cylinder up
digitalWrite(7,HIGH);  // This will turn the mini motors on
delay(2000);  // This delay will have to change with time when the machine is made

digitalWrite(5,LOW);  // Release air P will bring cylinder down
digitalWrite(7,LOW);  // Turn off the COPA motors

Serial.println("END of MeterCopa");

}
//---------------------------------------------------------------------
//Weighstation
void WeighStation()
{
Serial.println("WeighStation Start");
float tarr = 0;
float measure = 0;
int LightSensorA = 0;
int LightSensorB = 0;

LightSensorA = digitalRead(2);
LightSensorB = digitalRead(3);

//Before anything starts we need to stop any incoming bolts
digitalWrite(6,LOW);
Serial.println("Tarr the Scale");
delay(500);
//First calibrate the scale
tarr = scale.getGram();
// Now send the next bolt through
Serial.print(tarr,1);
Serial.println("g");
Serial.println("Send the Bolt");
while(LightSensorA == LOW) // this small while loop will send the bolt away from the meter wheel
{
   digitalWrite(4,HIGH);
   LightSensorA = digitalRead(2);
}
// may need to add a small delay here
digitalWrite(4,LOW); // stop the meter wheel once the bolt is away
Serial.println("Bolt Sent");
// Right now the bolt is in transit and we need to wait for it to arrive
while(LightSensorB == HIGH) // need to wait till sensor says bolt is present
{
Serial.println("Waiting on Bolt"); 
delay(100);
LightSensorB = digitalRead(3);
}
delay(100);
// this while loop will end when the light sensor is LOW ( sees a bolt)
measure = scale.getGram();

weight = tarr - measure;
weight = abs(weight);
Serial.print("bolt weight in g ");

Serial.println(weight);

// the bolt has now been weighed and is now ready to leave.
Serial.println("WeighStation End");
}
//-----------------------------------------------------------------------------------------
//Bins
void Bins()
{
Serial.println("Start of the Bins"); 

int LightSensorB = 0;
LightSensorB = digitalRead(3);
String bolt= "--Null--" ;
int val = 0;
// once the weight has been calculated we need to identify the bolt and then say the absolute position

// I'll make this part once I know more about the weights I expect to see

if ((weight >= 30.00) && (weight<=35.000))
{
  val = 0;
  bolt = "AL-0.500" ;
}

else if ((weight >= 39) && (weight<=45))
{
  val = 14;
  bolt = "AL-0.750" ;
}
else if ((weight >= 46) && (weight<=52))
{
  val = 28;
  bolt = "AL-1.000" ;
}
else if ((weight >= 74) && (weight<=80))
{
  val = 42;
  bolt = "AG-1.000" ;
}
else if ((weight >= 83) && (weight<=85))
{
  val =56;
  bolt = "AG-1.250" ;
}
else if ((weight >= 86) && (weight<=90))
{
  val = 70;
  bolt = "AJ-0.875" ;
}
else if ((weight >= 91) && (weight<=95))
{
  val = 84;
  bolt = "AJ-1.000" ;
}
else if ((weight >=106 ) && (weight<=110))
{
  val = 98;
  bolt = "AJ-1.250" ;
}
else if ((weight >=140 ) && (weight<=150))
{
  val = 112;
  bolt = "AJ-1.750" ;
}
else if ((weight >=157 ) && (weight<=165))
{
  val = 126;
  bolt = "AJ-2.000" ;
}
else if ((weight >=209 ) && (weight<=220))
{
  val = 140;
  bolt = "AJ-2.750" ;
}
else if ((weight >=225 ) && (weight<=235))
{
  val = 154;
  bolt = "AJ-3.000" ;
}
else if ((weight >=240 ) && (weight<=265))
{
  val = 172;
  bolt = "AG-4.500" ;
}

else
{
  val = 186;
  bolt = "Uknown";
}
Serial.println("Bolt is");
Serial.println(bolt);


Serial.println("Stepper Motor Move now");
// now that the bolt and absolute position have been identifiied we need to move to that value ( lets call it val and will range 0 to 200)
stepper.step(val - previous); // go to the correct place
previous = val; // remember where you now are

Serial.println("Stepper in position");
// now we are in position and are ready to send the bolt away from the weigh station
digitalWrite(6,LOW);
Serial.println("Bolt in transit");

while(LightSensorB == LOW)
{
  digitalWrite(6,HIGH);
}
delay(100); // extra timer to make sure the bolt got away
digitalWrite(6,LOW); // stop bolts again

Serial.println("End of Bins");
}

I think that you can edit the title to include solved.