I have been adding to my code information from different examples and from internet.
I don't understand all what they do... especially about the code of the median filter.
So if any has any comments how it could be shorter (or better), I would really appreciate it.
The program reads from two potentiometers.
After the median filter and if the value has changed more than four then it send the data thru serial .
From serial it reads "1" or "0" to control the stepper motor, and it send the value back thru serial.
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
const int pressureIn1 = A0; // Analog input pin that the potentiometer is attached to
const int pressureOut1 = 5; // Analog output pin that the LED is attached to
const int pressureIn2 = A1; // Analog input pin that the potentiometer is attached to
const int pressureOut2 = 6; // Analog output pin that the LED is attached to
unsigned long time = 0;
int dPosition = 0;
int medianSize1 = 5; //quantity of values to find the median (sample size). Needs to be an odd number
int rangeValue1[] = {
0, 0, 0, 0, 0};
int medianSize2 = 5; //quantity of values to find the median (sample size). Needs to be an odd number
int rangeValue2[] = {
0, 0, 0, 0, 0};
int ledPin = 13;
int value1 = 0; // value read from the pot
int outpoutValue1 = 0; // value output to the PWM (analog out)
int value2 = 0; // value read from the pot
int outputValue2 = 0; // value output to the PWM (analog out)
int compareData1= 0;
int compareData2 = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int midle1 = medianSize1/2;
int midle2 = medianSize2/2;
value1 = rangeValue1[midle1];
value2 = rangeValue2[midle2];
outpoutValue1 = map(value1, 0, 1023, 0, 255);
outputValue2 = map(value2, 0, 1023, 0, 255);
analogWrite(pressureOut1, outpoutValue1);
analogWrite(pressureOut2, outputValue2);
delay(10);
{
int val = Serial.read() - '0';
if (val == 1)
{
Serial.print("po=");
dPosition += 1;
Serial.println(dPosition);
myStepper.step(stepsPerRevolution);
}
else if (val == 0)
{
Serial.print("po=");
dPosition -= 1;
Serial.println(dPosition);
myStepper.step(-stepsPerRevolution);
}
}
{
for(int i = 0; i < medianSize1; i++)
{
rangeValue1[i] = analogRead(pressureIn1);
rangeValue2[i] = analogRead(pressureIn2);
delay(1); //wait between analog samples
}
isort(rangeValue1, medianSize1);
isort(rangeValue2, medianSize2);
}
if (value1<compareData1-4 || value1>compareData1+4 || value2<compareData2-4 || value2>compareData2+4)
{
time = millis();
Serial.print("fP=" );
Serial.print(value1);
Serial.print("\n");
Serial.print("sP=" );
Serial.print(value2);
Serial.print("\n");
Serial.print("Ti=" );
Serial.print(time);
Serial.print("\n");
}
// delay(10);
compareData1= value1;
compareData2 = value2;
}
void isort(int *a, int n)
{
for (int i = 1; i < n; ++i)
{
int j = a[i];
int k;
for (k = i - 1; (k >= 0) && (j < a[k]); k--)
{
a[k + 1] = a[k];
}
a[k + 1] = j;
}
}
void printArray(int *a, int n)
{
for (int i = 0; i < n; i++)
{
Serial.print(a[i], DEC);
Serial.print(' ');
}
Serial.println();
}
Moderator edit: I guess you could make it shorter, but better would be to post it properly using CODE TAGS