https://www.arduino.cc/en/Reference/Map
nel tuo caso sarebbe
Codice: Seleziona tutto
potValue2 = map( analogRead(potPin), 0, 1024, 0, 99);
Codice: Seleziona tutto
potValue2 = map( analogRead(potPin), 0, 1024, 0, 99);
Codice: Seleziona tutto
/* hey, this is a simple code to make your arduino read
the value of a potentiometer and display it in percentage form on
a 16 X 2 LCD screen. I am pretty new at this so sorry if this code
is terrible or if i have no idea what i'm talking about in the
comments.
the circuit(pasted from examples):
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
*/
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin = A0; //Potentiometer input pin
int potValue2 = 0; // final display variable
void setup() {
lcd.begin(16, 2); // lcd rows and columns
lcd.print("Potentiometer"); // title of sorts
}
void loop() {
// read then divide the input(max 1020 in this case) by 10
// divide by 1.02 to get percentage
potValue2=analogRead(potPin)/10.24;
// set cursor to second row, first column
lcd.setCursor(0, 1);
//display final percentage
lcd.print(potValue2);
//print the percent symbol at the end
lcd.print("%");
//wait 0.1 seconds
delay(100);
//wipe the extra characters
lcd.print(" ");
delay(1);
}
Codice: Seleziona tutto
/* hey, this is a simple code to make your arduino read
the value of a potentiometer and display it in percentage form on
a 16 X 2 LCD screen. I am pretty new at this so sorry if this code
is terrible or if i have no idea what i'm talking about in the
comments.
the circuit(pasted from examples):
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
*/
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin = A0; //Potentiometer input pin
int potValue2 = 0; // final display variable
void setup() {
lcd.begin(16, 2); // lcd rows and columns
lcd.print("Potentiometer"); // title of sorts
}
void loop() {
// read then divide the input(max 1020 in this case) by 10
// divide by 1.02 to get percentage
potValue2 = map( analogRead(potPin), 0, 1024, 0, 99);
// set cursor to second row, first column
lcd.setCursor(0, 1);
//display final percentage
lcd.print(potValue2);
//print the percent symbol at the end
lcd.print("%");
//wait 0.1 seconds
delay(100);
//wipe the extra characters
lcd.print(" ");
delay(1);
}
Codice: Seleziona tutto
potValue2 = map( analogRead(potPin), 0, 1024, 10, 20);
Codice: Seleziona tutto
float afr = map( analogRead(potPin), 0, 1024, 100, 200)/10;
Codice: Seleziona tutto
float potValue2 = map( analogRead(potPin), 0, 1024, 100, 200)/10;
Codice: Seleziona tutto
void setup() {
Serial.begin(57600);
}
float afr = 0;
char tempString[100];
char floatToStringBuffer[15];
void loop() {
for(int i=0; i<=1024; i++) {
afr = map( i, 0, 1024, 100, 200) / (float)10;
dtostrf(afr, 4, 2, floatToStringBuffer);
snprintf( tempString, 100, "i: %d -> afr: %s", i, floatToStringBuffer);
Serial.println(tempString);
}
delay(100);
}
Torna a “Elettronica generale”
Visitano il forum: Nessuno e 24 ospiti