johnrobholmes
10 MW
I am refining the automation on one of my production machines, a resistance brazer. I have a big SSR coming to replace the contact switch since it is being turned off under load, and I imagine the switch won't last very long. While upgrading that part I figured it useful to integrate all functions into an arduino controlled sequence. The basic code I have hacked together is as follows:
I think all cases are covered, and I threw in some extra IF statements to cover irregular states. I think a few more statements are needed to ensure the high() and low() functions operate properly, as there may be a problem where the footpedal is pressed and the loop kicks back into high() mode as soon as it releases. The pedal input is momentary on and I want to control everything with one shot input. Ideally we would want {Hlimit HIGH && pedal HIGH} to execute and wait for a second pedal HIGH input before executing the low() {else} portion of code, to prevent a long pedal press from running through the entire high() to low() {else} sequence without pause. I'm trying to figure out how to force each function to loop until a specific state is reached, I think the 'while' function could be the trick.
This is my first code for a functional machine, other than making a simple strobe for our lathe and setting up PLC years ago in college.
What do you guys think?
Code:
int footswitchPin = 1; // footswitch connected to digital pin 1
int HlimitPin = 2; // high limit switch NO connected to pin 2
int LlimitPin = 3; // low limit switch NO connected to pin 3
int motorPin = 4; // motor control output connected to pin 4
int powerPin = 5; // transformer control output connected to pin 5
int resetPin = 6; // reset button connected to pin 6
int motorvar = 0; // variable for storage of motor state
//digitalRead shortcuts
int footswitch = digitalRead(footswitchPin);
int Hlimit = digitalRead(HlimitPin);
int Llimit = digitalRead(LlimitPin);
int reset = digitalRead(resetPin);
int power = digitalRead(powerPin);
// set variable functions
void setup()
{
pinMode(footswitchPin, INPUT); // sets footswitchPin as input
pinMode(HlimitPin, INPUT); // sets HlimitPin as input
pinMode(LlimitPin, INPUT); // sets LlimitPin as input
pinMode(resetPin, INPUT); // sets reset as input
pinMode(motorPin, OUTPUT); // sets motorPin as output
pinMode(powerPin, OUTPUT); // sets powerPin as output
}
// home position function
int high()
{
if (Hlimit != HIGH)
{
digitalWrite (motorPin, HIGH);
}
else
{
digitalWrite (motorPin, LOW);
}
}
// low position function
int low()
{
if (Llimit != HIGH)
{
digitalWrite (motorPin, HIGH);
}
else
{
digitalWrite (motorPin, LOW);
digitalWrite (powerPin, HIGH);
delay (250);
digitalWrite (powerPin, LOW);
delay (100);
high ();
}
}
//main loop
void loop()
{
// footswitch OFF
if (reset == HIGH)
{
digitalWrite (powerPin, LOW);
delay (250);
high();
}
if (power == LOW && footswitch == LOW && Llimit == LOW && Hlimit == LOW)
{
high();
}
if (footswitch == LOW && Hlimit == HIGH)
{
digitalWrite (motorPin, LOW);
digitalWrite (powerPin, LOW);
}
if (footswitch == LOW && Llimit == HIGH)
{
digitalWrite (powerPin, LOW);
digitalWrite (motorPin, LOW);
}
//footswitch ON
if (footswitch == HIGH && Hlimit == HIGH)
{
low();
}
if (footswitch == HIGH && Llimit == HIGH)
{
low();
}
}
I think all cases are covered, and I threw in some extra IF statements to cover irregular states. I think a few more statements are needed to ensure the high() and low() functions operate properly, as there may be a problem where the footpedal is pressed and the loop kicks back into high() mode as soon as it releases. The pedal input is momentary on and I want to control everything with one shot input. Ideally we would want {Hlimit HIGH && pedal HIGH} to execute and wait for a second pedal HIGH input before executing the low() {else} portion of code, to prevent a long pedal press from running through the entire high() to low() {else} sequence without pause. I'm trying to figure out how to force each function to loop until a specific state is reached, I think the 'while' function could be the trick.
This is my first code for a functional machine, other than making a simple strobe for our lathe and setting up PLC years ago in college.
What do you guys think?