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,
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.
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);
}
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