Conversion

Hi I am trying to convert below values from serial readings into 0% ~ 100%.
Minimum value : 30%
Maximum value : 100%
I want 30% ~ 100% to be converted into 0% ~ 100% so that 30%(Minimum value) to become 0% and so on until 100%.
This is the serial readings,

100.00
100.00
100.00
100.00
89.00
89.00
89.00
89.00
89.00
81.00
81.00
81.00
81.00
73.00
73.00
73.00
73.00
73.00
73.00
63.00
63.00
63.00
54.00
53.00
53.00
53.00
53.00
42.00
42.00
42.00
33.00
30.00
30.00
30.00
30.00

And, this is the code I used.

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
    lcd.begin(20, 4);
    lcd.print("Position SensorDetector(%)");
   Serial.begin(9600);
}

void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);    
   // turn the ledPin on
   int positionSensorReading = analogRead(sensorPin);
   float positionSensorVolts = positionSensorReading * 5.0 / 1023.0;
   float positionSensor = ( ( positionSensorVolts-0.5 ) / ( 4.5 - 0.5 )) * 100.0 ;

  positionSensor = ceil(positionSensor);   // <<<<< round to the following integer

   digitalWrite(ledPin, HIGH);  
   // stop the program for <sensorValue> milliseconds:
   delay(sensorValue);          
   // turn the ledPin off:        
   digitalWrite(ledPin, LOW);   
   // stop the program for for <sensorValue> milliseconds:
   delay(sensorValue);    
lcd.setCursor(0, 4 );
lcd.print("   ");
lcd.setCursor(0, 4 );

lcd.print(positionSensor);
lcd.println ("% ");
Serial.println(positionSensor);   
}

Please teach me how to modify above code to have 0% ~ 100% readings.
Thank you.

"map()"?

Subtract 30 from every value and multiply the result by 100/70

...R

be sure to do the math in float ...

. . . And for bonus marks, be sure to not do the arithmetic in floating point :wink:

I'm very sorry, guys.
I am very new with programing.
Could you teach me in more detail?

Did you read reply #2?

this is the formula to adapt.

float positionSensor = ( ( positionSensorVolts-0.5 ) / ( 4.5 - 0.5 )) * 100.0 ;

AWOL:
Did you read reply #2?

Yes, but I'm still having trouble applying it to the code.

robtillaart:
this is the formula to adapt.

float positionSensor = ( ( positionSensorVolts-0.5 ) / ( 4.5 - 0.5 )) * 100.0 ;

I'm sorry, still having trouble.

I'm sorry, still having trouble.

You are not going to get any more help until you explain what you've done, and what "trouble" you are still having.

Hi Paul,
I tried to modify this code by changing 4.5 to 5.0 and 0.5 to 0.0 but I couldn't get the result I wanted.

float positionSensor = ( ( positionSensorVolts-0.5 ) / ( 4.5 - 0.5 )) * 100.0 ;

4.5 is 100%. It works but I am completely lost on how to establish 0% instead of having 30% as the minimum value.

but I couldn't get the result I wanted.

You haven't defined what results you get, based on what input. You haven't defined what results you expect. You haven't posted all of your code. The ball is STILL in your court. Over there. In the corner. It's the big orange thing.

The essence of your loop, removed from all non relevant stuf

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
   int r = analogRead(sensorPin);     // r = 0 .. 1023
   float v = r * 5.0 / 1023.0;            // v = 0.0 ..  5.0

   float p = ( v - 0.5 ) / (4.5-0.5) * 100.0 ;       // p = -12.5 .. 112.5    // NOT 0 .. 100 %

   p = ceil(p);   // <<<<< round to the following integer
  Serial.println(p);   
}

this code prints 30 .. 100

so p is in practice minimal 30 and max 100,

a p of 30 equals to a voltage v == 1.7 ==> analogRead of 347
a p of 100 equals to a voltage v == 4.5 ==> analogRead of 921

so the voltage moves apparently between 1.7 and 4.5 V = 2.8V dynamic
(do the math in reverse...)

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
   int r = analogRead(sensorPin);     // r = 0 .. 1023
   float v = r * 5.0 / 1023.0;                 // v = 0.0 ..  5.0
  int p = (v - 1.7)/(4.5 - 1.7) * 100;  // check 1.7 -> 0 and 4.5 -> 100
  Serial.println(p);
}

now the same with only int math

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
   long r = analogRead(sensorPin);
  int p = (r - 347) * 100L / (921 - 347);  // L is for long enforces 32 bit math
  Serial.println(p);
}

give it a try

Thank you so much, robtillaart
I tried and I am having some compiling problem.
I am having trouble incorporating your code with mine.

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
    lcd.begin(20, 4);
    lcd.print("Position SensorDetector(%)");
   Serial.begin(9600);
}

void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);    
   // turn the ledPin on
   int positionSensorReading = analogRead(sensorPin);
   float positionSensorVolts = positionSensorReading * 5.0 / 1023.0;
   float positionSensor = ( ( positionSensorVolts-0.5 ) / ( 4.5 - 0.5 )) * 100.0 ;

  positionSensor = ceil(positionSensor);   // <<<<< round to the following integer

   digitalWrite(ledPin, HIGH);  
   // stop the program for <sensorValue> milliseconds:
   delay(sensorValue);          
   // turn the ledPin off:        
   digitalWrite(ledPin, LOW);   
   // stop the program for for <sensorValue> milliseconds:
   delay(sensorValue);    
lcd.setCursor(0, 4 );
lcd.print("   ");
lcd.setCursor(0, 4 );

lcd.print(positionSensor);
lcd.println ("% ");
Serial.println(positionSensor);   
}

I tried to modify the code as below.

void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
    lcd.begin(20, 4);
    lcd.print("Position SensorDetector(%)");
   Serial.begin(9600);
}

void loop() {
    int r = analogRead(sensorPin);     // r = 0 .. 1023
   float v = r * 5.0 / 1023.0;            // v = 0.0 ..  5.0

   float p = ( v - 0.5 ) / (4.5-0.5) * 100.0 ;       // p = -12.5 .. 112.5    // NOT 0 .. 100 %

   p = ceil(p);   // <<<<< round to the following integer
  Serial.println(p);

This error message is appeared.

sketch_jul06a.ino: In function 'void loop()':
sketch_jul06a:33: error: 'lcd' was not declared in this scope
sketch_jul06a:37: error: 'positionSensor' was not declared in this scope

you must declare the variables used.

disclaimer - I did not test my code