Hello ,
I am trying to read the potentiometer value, but the readings are not stable.
The board is arduino nano . I try with external power supply , same ...
in serial monitor i have some like this :
1023
1022
1023
1022
int analogInput = 0;
int value ;
int last;
void setup() {
pinMode(analogInput, INPUT);
Serial.begin(9600);
delay(1000);
}
void loop() {
value = analogRead(analogInput); // read the value at analog input
if(value!=last){
last=value;
Serial.println(value);
}
```
Check the 328P datasheet. Analogue readings can always vary by at least on bit.
Your topic has nothing to do with Installation and Troubleshooting (which is about the IDE) and has therefore been moved to a more suitable location on the forum.
Thank you ! I will try this ...
I think that we can`t talk about analog noice because the pot is next to the board 3 sm cables .
I try with capacitor before write in this forum , but nothing change ....
will try to add hysteresis in code .
And yes , code work now , but with unstable wast digit ...
Glad you found the 0-100% version helpful.
I also made a 0-255 version.
Leo..
// converts the position of a 10k lin(B) pot to a byte (0-255)
// pot connected to A0, 5volt and ground
int rawValue; // raw reading
int oldValue; // for deadband
byte Byte; // final byte
byte oldByte; // for printing
void setup() {
Serial.begin(9600);
}
void loop() {
// rawValue = analogRead(A0); // add dummy read, if needed
rawValue = analogRead(A0); // read pot
if (rawValue < (oldValue - 2) || rawValue > (oldValue + 2)) { // add some deadband
oldValue = rawValue; // update value
Byte = oldValue >> 2; // convert 10-bit to 8-bit
if (oldByte != Byte) { // only print if value changes
Serial.print("Byte is: ");
Serial.println(Byte);
oldByte = Byte; // update value
}
}
}
It also depends what you want to do with the 10-bit pot reading.
If you're sending it to an 8-bit PWM pin then you can afford loosing a few rogue bits.
Leo..
i want BIN value of the pot to be visualised on Digital pin 13 to pin 7 ( 7 bit )
Lets say if i have pot percentige 127 all pins from 13 to 7 to be = 1 ( high ) 127 is 1111111
If i have 0 this is 0000000 all pins off
If i have 10 -> 0001010
Just to have bin value on PORTD13 to PORTD7 or from D2 to D9 ... doesn`t matter .
// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground
int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;
void setup()
{
Serial.begin(115200); // set serial monitor to this baud rate, or change the value
DDRD=OUTPUT;
digitalWrite(PORTD,LOW);
}
void loop()
{
rawValue = analogRead(A0);
rawValue = analogRead(A0); // read input twice
rawValue = constrain(rawValue, 8, 1015); // ignore bad hop-on region of a pot by removing 8 values at both extremes
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) // add hysteresis
{
oldValue = rawValue;
potPercentage = map(oldValue, 8, 1008, 0, 127); // convert to percentage
if (oldPercentage != potPercentage) // Only print if %value changes
{
Serial.print("Pot percentage is: ");
Serial.print(potPercentage);
Serial.print("% BIN ");
oldPercentage = potPercentage;
for(int i=6; i>=0; i--)
{
bool m = bitRead(potPercentage, i);
Serial.print(m, BIN); //shows: 7 bits only
}
PORTD=(potPercentage,BIN);
Serial.println();
}
}
}
it was my mistake ... because varible m is a binary , no need -> Serial.print(m, BIN);
only Serial.print(m) work ...
I did it ... with one more varible , decrement for loop ...
you are right ... i must read cppreference: comma operator , thank you !