Hello,
I am working with the 4051 analog expander IC. I made all my connections but i got one problem. Each time i move the circuits on the table or turn off the circuit then on, the value of the potentiometer at 0 angle is different. The value is never 0 there is always a number. So remap it with map() to get 0-255 range but each time it changes i have to remap it again manually.
for example I plug the arduino in my laptop and check the start value let's say it is 150, then I unplug the arduino then plug and the value is 200. but when I plug and turn the pot everything works fine and i get a value on the display.
So what I am looking for is to let this be done automatically in the sketch by defining the value at 0 pot angle at the start to be the minimum value and the max reached to be the maximum value. It is to note that i am not turning the potentiometer all the way because it is my application that i should not turn it all the way. So I need your help make this work please.
In my sketch below, I tried a method, but seems not working and I thought also of making an array in which I add the values from the pot to it then should the minimum and maximum
It is to note that values of analog pin is never 0 with the 4051, I changed many and all worked this way thinking that i have a defected IC.
I am using a potentiometer like this
here is is my sketch :
// Define the 4051 analog expander pins
//-------------------------------------------------
int totalChannels = 8;
int addressA = A2;
int addressB = A3;
int addressC = A4;
int AnalogIn_z = A1;
int A = 0; //Address pin A
int B = 0; //Address pin B
int C = 0; //Address pin C
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0;
int clutchValue = 0;
int clutchAverage;
int clutch_min;
int clutch_max;
int clutch_current_min;
int clutch_current_max;
int clutch_previous_min = clutch_min;
int clutch_previous_max = clutch_max;
void setup() {
Serial.begin(9600);
// define arduino pins used to connect to the 4051 analog expander
pinMode(addressA, OUTPUT);
pinMode(addressB, OUTPUT);
pinMode(addressC, OUTPUT);
pinMode(AnalogIn_z, INPUT);
}
void loop() {
//Select each pin and read value
for(int i=0; i<totalChannels; i++){
A = bitRead(i,0); //Take first bit from binary value of i channel.
B = bitRead(i,1); //Take second bit from binary value of i channel.
C = bitRead(i,2); //Take third bit from value of i channel.
//Write address to mux
digitalWrite(addressA, A);
digitalWrite(addressB, B);
digitalWrite(addressC, C);
//Read and print value
if(i == 4) {
clutchAverage = smooth(AnalogIn_z);
clutch_current_min = clutchAverage;
if(clutch_min < clutchAverage){
if(clutch_min < clutch_previous_min){
clutch_previous_min = clutch_min;
clutch_min = clutchAverage;
}
if(clutch_max < clutchAverage){
clutch_previous_max = clutch_max;
clutch_max = clutchAverage;
}
} else if(clutch_min > clutchAverage){
clutch_previous_min = clutch_min;
clutch_min = clutchAverage;
} else if(clutch_previous_min > clutch_min && clutch_min > clutchAverage){
clutch_previous_min = clutch_min;
clutch_min = clutchAverage;
if(clutch_previous_max > clutch_max && clutch_max > clutchAverage){
clutch_previous_max = clutch_max;
clutch_max = clutchAverage;
}
}
Serial.print("Clutch Value: ");
Serial.println(clutchAverage);
Serial.print("Clutch min: ");
Serial.println(clutch_min);
Serial.print("Clutch max: ");
Serial.println(clutch_max);
Serial.print("Clutch previous min: ");
Serial.println(clutch_previous_min);
Serial.print("Clutch previous max: ");
Serial.println(clutch_previous_max);
clutchAverage = map(clutchAverage, clutch_min, clutch_max, 0, 255);
Serial.print("Clutch remaped: ");
Serial.println(clutchAverage);
delay(500);
//Serial.println(clutchAverage);
}
}
}
// Smoothing analog input function
int smooth(int potPin){
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(potPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
return average;
}