Problem float number

Hello everyone I have a problem to ask this forum, I have this math :

void loop() {
  analog_val = analogRead(analog_pin);         
   
  // Convert cm

  int analog_val_cm = analog_val/0.78;


  misura=float(analog_val_cm)/651;  

// 651 is maximun number read from maxbotix sensor

  Serial.print("misura 1: "); 
  Serial.print(misura);
  Serial.print("\n"); 

  delay(50);

but the result is 0:02 instead of 0:00 and do not understand why
can someone help me?

The types of some of your variables are undefined. Therefore, we can't answer your question.

You could, by adding more Serial.print() statements. Perhaps some of your assumptions are incorrect.

i get all code

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

#include <OSCBoards.h>
#include <OSCBundle.h>
#include <OSCData.h>
#include <OSCMatch.h>
#include <OSCMessage.h>
#include <SLIPEncodedSerial.h>
#include <SLIPEncodedUSBSerial.h>

#include <SPI.h>


EthernetUDP Udp;

//the Arduino's IP
IPAddress ip(192, 168, 0, 102);
//destination IP
IPAddress outIp(192, 168, 0, 101);
const unsigned int outPort = 8000;

byte mac[] = {  
  0x90, 0xA2, 0xDA, 0x0D, 0xDA, 0xB7 }; // you can find this written on the board of some Arduino Ethernets or shields


//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int analog_pin = A0;  // Sensore di segnale analogico
int analog_val = 0;         // valore del segnale analogico

float misura;
float misura2;


void setup() {

  Ethernet.begin(mac,ip);
  Udp.begin(8001);
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
}

void loop() {
  analog_val = analogRead(analog_pin);            
  // Convertire il valore in centimetri
  int analog_val_cm = analog_val/0.78;
  //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 147 uS per Inch.


  misura=float(analog_val_cm)/651; 
  misura2=misura/100;
  
  
  Serial.print("misura 1: "); 
  Serial.print(misura);
  Serial.print("\n"); 
  Serial.print("misura 2"); 
  Serial.print(misura2); 
  Serial.print("\n"); 

  delay(50);
  
 

OSCBundle bndl;

  bndl.add("/activeclip/link1/values").add(misura);
  bndl.add("/activeclip/link2/values").add(misura2);

  Udp.beginPacket(outIp, outPort);
  bndl.send(Udp); // send the bytes to the SLIP stream
  Udp.endPacket(); // mark the end of the OSC Packet
  bndl.empty(); // free space occupied by message

  delay(200);
}

thx

  analog_val = analogRead(analog_pin);

So, what value did you read?

Why do you assume that misura should be 0?

the value is the distance from the ultrasonic sensor, with what sending a given osc that controls a slide that needs to go from 0.01 to 1.
when the value of ultrasound read is 651 ( cm ) I need both the slider to 0

the value is the distance from the ultrasonic sensor

What value is? What is the value read from the sensor? 0? 1023? Something in between?

as performance is no issue (as you use delay) make all var's float and keep the math in float.

float analog_val;
float misura;
...
void loop() {
  analog_val = analogRead(analog_pin);         
   
  // Convert cm

  float analog_val_cm = analog_val/0.78;


  misura = analog_val_cm/651;  

// 651 is maximun number read from maxbotix sensor

  Serial.print("misura 1: "); 
  Serial.print(misura , 3);   // print with 3 decimal as misura is between 0.000 and 1.000
  Serial.print("\n"); 

  delay(50);

i found a problem "analog_val_cm" the low number don't 0, but 14 ...

 Serial.print("misura 1: "); 
  Serial.print(misura);
  Serial.print("\n"); 
  Serial.print("analog val :"); 
  Serial.print(analog_val_cm); 
  Serial.print("\n");

Result

misura 1: 0.02
analog val :14

14/651 = 0.02

i put this

if ( analog_val_cm == 14 ) {
   
   analog_val_cm = 0;
    
  }

and go ! Thanks

Better do

if ( analog_val_cm  < 15 )
  {
    analog_val_cm = 0;
  }

Please pay some attention to the style of the code layout, makes it much easier to read / debug / etc.

sorry

No need to apologize, it was meant as a tip to improve your coding style.
To be honest we have seen far far worse on the forum.

In your code, you had "int analog_val_cm = analog_val/0.78;"

I should point out that it might work better if you declared 'analog_val_cm' as 'float' rather than 'int', and used a math library function (such as 'floor()') to truncate or round it, as needed. Or did you want better precision? This might explain your problem. The integer conversion generally truncates, not rounds.

try 'float analog_val_cm = analog_val / 0.78;" to keep it as a float,
or "int analog_val_cm = (int)floor(0.5 + analog_val / 0.78);" to round it before making it an integer