I am new to the Arduino community and trying to start up a project to read the direction of the analog input. The output should indicate the direction being 1 or 0.
To keep the code lean I wrote some functions to manage this.
The function Filter in the example should average the input signal, taken as an input the raw voltage of the sensor.
By executing this function 2 time by a delay, the idea is to compare result 1 with result 2 and use this as
an input in the next function called Direction.
For a reason I don't understand it is not generate an output from the Filter function ( when printing to the console it just reads 0 where the pot meter reads 0.45V.
Having no function and all in Void Loop() this works ( by creating 2 times the filtered signal an comparing ).
But this looks too messy.
attached the code
Can someone tell me my error in the logic?
Very much appreciated!
it looks like your reading the input once and using the same value in Filtering() and the subsequent comparison to determine a direction. I can't see how the results of the comparison isn't always the same answer.
i think you want each iteration of loop () to make a measurement and compare it to the measurement from the previous iteration of loop ().
const int inputPin = A0;
void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
}
void loop()
{
float grip = analogRead(inputPin) * ( 4.5 / 1024.0 );
float pre_sample_grip = Filtering(grip);
delay(20);
float post_sample_grip = Filtering(grip);
int Direction = Grip_Direction(pre_sample_grip, post_sample_grip, grip);
}
float Filtering(float grip)
{
const int numReadings = 50;
int grip_value[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
float filtered_grip;
for (int thisReading = 0; thisReading < numReadings; thisReading++)
{
grip_value[thisReading] = 0;
}
total = total - grip_value[readIndex]; // subtract the last reading:
grip_value[readIndex] = grip; //analogRead(inputPin); // read from the sensor:
total = total + grip_value[readIndex]; // add the reading to the total:
readIndex = readIndex + 1; // advance to the next position in the array:
if (readIndex >= numReadings) // if we're at the end of the array...
{
readIndex = 0; // ...wrap around to the beginning:
}
filtered_grip = total / numReadings; // calculate the average:
return filtered_grip;
}
int Grip_Direction(float pre_sample_grip, float post_sampling_grip, float grip)
{
int Direction;
if (pre_sample_grip > post_sampling_grip || grip <= 0.55) // set conditions
{
Direction = 0;
}
else
{
Direction = 1;
}
return Direction;
}
@rs46 - please read the advice in the post linked to and follow it in future
The filtering, is just to make the reading stable.
So it is not switching too quick from one direction to the other.
Checking on my scope it does the job for what I am using it for.
For sure it is not the nicest one I agree
rs46:
The filtering, is just to make the reading stable.
So it is not switching too quick from one direction to the other.
Checking on my scope it does the job for what I am using it for.
For sure it is not the nicest one I agree
I can't see that it does anything at all, apart from waste processor cycles.