Ok I added a smoothing sketch to my code, and an RC low pass filter between my divider and analog pin.
Code:
#include <TM1637Display.h>
#include <EEPROM.h>
const int CLK = 2; //Set the CLK pin connection to the display
const int DIO = 3; //Set the DIO pin connection to the display
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
const int numReadings = 10;
int analog_value[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
float input_voltage = 0.0;
float temp = 0.0;
float r1 = 99900.0;
float r2 = 1980.0;
float setVoltage = 102.0;
int eeAddress = 0;
int Enable = 7;
int STEP = 8;
int dir = 9;
int relay1 = 4;
int relay2 = 5;
int relay3 = 6;
int mainSwitch = 13;
int Up = 10;
int Down = 11;
bool startDelay = false;
void setup() {
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
analog_value[thisReading] = 0;
}
display.setBrightness(0x0a); //set the diplay to maximum brightness
analogReference(EXTERNAL); // use AREF for reference voltage
pinMode(mainSwitch, INPUT);
pinMode(Up, INPUT);
pinMode(Down, INPUT);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(Enable, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(dir, OUTPUT);
digitalWrite(Enable, HIGH);
digitalWrite(STEP, LOW);
digitalWrite(dir, LOW);
EEPROM.get(eeAddress, setVoltage);
}
void loop() {
// subtract the last reading:
total = total - analog_value[readIndex];
// read from the sensor:
analog_value[readIndex] = analogRead(A0);
delay(10);
// add the reading to the total:
total = total + analog_value[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
temp = (average * 4.06) / 1024.0;
input_voltage = temp / (r2/(r1+r2));
Serial.print("v= ");
Serial.println(input_voltage);
display.showNumberDec(input_voltage);
if(digitalRead(Up) == LOW){
setVoltage++;
EEPROM.put(eeAddress, setVoltage);
display.showNumberDec(setVoltage);
delay(1000);
}
if(digitalRead(Down) == LOW){
setVoltage--;
EEPROM.put(eeAddress, setVoltage);
display.showNumberDec(setVoltage);
delay(1000);
}
if(digitalRead(mainSwitch) == LOW){
if(input_voltage >= 90 && input_voltage <=120){
Serial.println("ARC OK");
if(startDelay == false){
delay(1500);
startDelay = true;
}
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(Enable, LOW);
if(input_voltage <= setVoltage - 1){
digitalWrite(dir, HIGH);
Serial.println("Up");
for(int i=0;i<200;i++){
digitalWrite(STEP, HIGH);
delayMicroseconds(100);
digitalWrite(STEP, LOW);
delayMicroseconds(100);
}
}
else if(input_voltage >= setVoltage + 1){
digitalWrite(dir, LOW);
Serial.println("Down");
for(int i=0;i<200;i++){
digitalWrite(STEP, HIGH);
delayMicroseconds(100);
digitalWrite(STEP, LOW);
delayMicroseconds(100);
}
}
}
else{
Serial.println("Arc bad");
digitalWrite(Enable, HIGH);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
startDelay = false;
}
}
else{
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
startDelay = false;
}
}
I now seem to be getting a steady reading in the range I was expecting. I believe this was mainly as a result of the low pass filter.
The program seems to crash if I take more than 10 samples of the smoothing code. Any ideas? Plus it takes a second or two for the voltage to ramp up from zero to 102v with the smoothing code. Any way to make this happen faster?
z axis is going the wrong direction. That will be easily fixed :). May have to speed its movement up too.
The biggest problem I've seen though is that every other attempt the Arduino cnc controller crashes at plasma startup. I know this is a concern with plasma cutters, but it has never crashed on me before while cutting. Even with the long wire run from my plasma divider to thc controller, it worked fine and never crashed. The only difference now is the low pass filter. What issues would it cause?
Despite these problems, it was operating the z axis and trying to steady its height. I believe this will work if I can iron these faults out.
Thanks.