Good afternoon-
I'm attempting to do some math in the Arduino language (converting a distance measurement into a percentage). The problem I'm having is that board appears to be rounding and losing my decimals (which means when I convert to a percentage it either chooses zero or 100 with no intermediate values.)
Here's the relevant code:
long longDistance=long(distance);
long longMaxDistance=long(maxDistance);
long ratio=(longDistance/longMaxDistance);
long percent=(ratio*100);
int percentage=(percent);
(Note that I'm converting to an integer for display purposes, but I HOPE I'm doing that after the number becomes a percentage between 0 and 100. I added the first two lines in an attempt to force my numbers to be longs so I don't have an int messing me up.
I added a debugging section:
Serial.print("distance=");
Serial.print(distance);
Serial.print(" maxDistance=");
Serial.print(maxDistance);
Serial.print(" ratio=");
Serial.print(ratio);
Serial.print(" percent=");
Serial.print(percent);
Serial.print(" percentage=");
Serial.println(percentage);
and the results I get look like this:
distance=12 maxDistance=17 ratio=0 percent=0 percentage=0
Now, I'm not a math whiz, but my calculator tells me that 12/17 is not 0- it should be 0.705, which should get me a percent value of 70.5 and then it should truncate to a percentage of either 70 or 71. Obviously my program is dropping the decimals despite my best efforts to avoid that.
I'm attaching my whole program (which gets the distance from an ultrasonic sensor and passes it to an NRF2401 for remote display, and which also includes some error checking that I'm still fiddling with, and which is commented out) just in case something somewhere else is what is messing me up. THANK YOU for your help!
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(7,8); // Activate Radio
byte addresses[][6] = {"1Node","2Node"};
int count = 0;
const int trigPin = 4;
const int echoPin = 5;
long maxDistance=16;
long oldDistance=0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
Serial.begin(9600);
maxDistance=Measurement(trigPin, echoPin);
}
void loop() {
radio.stopListening();
long distance = Measurement(trigPin,echoPin);
//if (distance>2000){
// distance=oldDistance;
//}
//if (distance>maxDistance){
// maxDistance=distance;
//}
long longDistance=long(distance);
long longMaxDistance=long(maxDistance);
long ratio=(longDistance/longMaxDistance);
long percent=(ratio*100);
int percentage=(percent);
radio.write(&percentage, sizeof(percentage));
Serial.print("Message sent: ");
Serial.print("distance=");
Serial.print(distance);
Serial.print(" maxDistance=");
Serial.print(maxDistance);
Serial.print(" ratio=");
Serial.print(ratio);
Serial.print(" percent=");
Serial.print(percent);
Serial.print(" percentage=");
Serial.println(percentage);
oldDistance=distance;
delay(250);
}
// Pass pins that ultrasonic detector is connected to, returns distance in CM
long Measurement (int localTrigPin, int localEchoPin)
{
// Clears the trigPin
digitalWrite(localTrigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(localTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(localTrigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
long duration = pulseIn(localEchoPin, HIGH);
// Calculating the distance in cm
long cm= duration*0.034/2;
return cm;
}