Morning All
I am building some stepper motor dials, they will be controlled by an analog input, I have them working to the point where the needle will move in relation to the potentiometer but it has some chatter, I have used a a little bit of code to only read once every 500 milliseconds which has helped
if (!(millis() % 500)) {
but it does have some side effects,
what id like to do is update the needle position if the analog input changes by a value of 10 either way but i'm struggling to write this,
the dials will show temperature and fuel levels so don't need the rapid response
below is the code i have that works with the delay,
and below that is what i'm struggling to put together,
[code]
//02/01/23, adding reset button for dials
#include <SwitecX25.h>
// standard X25.168 range 315 degrees at 1/3 degree steps
#define STEPS1 (230)
#define STEPS2 (945)
// For motors connected to digital pins 5, 6, 7, 8 9, 10, 11, 12,.
SwitecX25 motor1(STEPS1, 9, 10, 11, 12);
SwitecX25 motor2(STEPS2, 5, 6, 7, 8);
#define BatteryLED 2
#define HiBeamLED 3
#define IndiacatorLED 4
#define LED_backlight 5
#define ResetButton A4 //reset buton on pin A4
int sensorValue1 = A0;
int sensorValue2 = A1;
void WarningLightTest(){
digitalWrite(BatteryLED, HIGH);
digitalWrite(HiBeamLED, HIGH);
digitalWrite(IndiacatorLED, HIGH);
delay(2000);
digitalWrite(BatteryLED, LOW);
digitalWrite(HiBeamLED, LOW);
digitalWrite(IndiacatorLED, LOW);
}
void setup() {
Serial.begin(9600);
motor1.zero(); // run the motor against the stops
motor2.zero(); // run the motor against the stops
pinMode(BatteryLED, OUTPUT);
pinMode(HiBeamLED, OUTPUT);
pinMode(IndiacatorLED, OUTPUT);
pinMode(LED_backlight, OUTPUT);
digitalWrite(LED_backlight, HIGH);
WarningLightTest();
}
void loop() {
int stepPosition1;
int stepPosition2;
// get the sensor value every 5 seconds
if (!(millis() % 400)) { //creates a delay for the sensor read,
stepPosition1 = (map( analogRead(sensorValue1), 0, 1023, 0, 230 ) );
}
motor1.update();
motor1.setPosition(stepPosition1);
if (!(millis() % 400)) {
stepPosition2 = (map( analogRead(sensorValue2), 0, 1023, 0, 945 ) );
}
motor2.update();
motor2.setPosition(stepPosition2);
delay (3); //delay to dampen the inertia of the needle.
}
}
[/code]
[code]
int sensorValue1 = A0;
int valueX =0;
void setup() {
Serial.begin(9600);
}
void loop() {
int valueA;
int valueY;
valueA = (map( analogRead(sensorValue1), 0, 1023, 0, 230 ) );
if (valueA > (valueX + 10)){
valueX = valueA;
}
Serial.print("Value A ");
Serial.println(valueA);
//Serial.print("Value Y ");
// Serial.println(valueY);
Serial.print("Value X ");
Serial.println(valueX);
delay (500);
}
[/code]