Hi Paul,
Thanks for the quick response. I've tried to clean up the code a bit and make it more clear.
PaulS:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(SSPin, INPUT);
You are not using the internal pullup resistors. So, how ARE the switches wired? Why aren't you using simpler wiring and the internal pullup resistors?
I am using 10k Ohm resistors on the bread board and all switches and buttons function properly as inputs
currentState = digitalRead(SSPin);
if (currentState == HIGH && lastState == LOW) {
I find it a lot easier to follow code what there is a pattern to the variable names.
Tried to make this more consistent
currSSState = digitalRead(SSPin);
if(currSSState != lastSSState)
if (test == 0) {
}
If test is 0, do nothing. Otherwise, do nothing. So, why bother testing?
You are correct this didn't do anything. I took it out.
What is test supposed to imply? Wouldn't running be a better name? Why is test an int, when it only holds 0 or 1? Better would be a boolean holding true or false.
Whatever the name it is only testing and holding the state if the SSbutton has been pressed. It keeps track properly of the on/off state. I think the place I'm having problems is in the functions at the bottom. That was why I tried using the "ActUpState" to track and start the actuator moving the first time through the loop. Your last comment concerned my use of this "ActUpState"...
digitalWrite(ActUpState, HIGH);
digitalWrite(ActDwnState, LOW);
This is where I quit trying to understand the code. Using State in the name of a variable that holds a pin number does not make sense.
...and I took it out.
So basically the first time I go through the "ActuatorUp() function" I would like to use the "OR" condition so the code to knows that both "ActUp" and "ActDwn" are set to "HIGH", i.e., actuator not moving, and then continue forward through the if statement and start them moving down. Next time through the "ActuatorUp()" function, "ActUp" will be "LOW" since it's moving up, then the limit switch will tell it to reverse. Thanks again for any advise on this.
-Stephen
Here is my latest iteration after your comments:
const int limit1 = 2; // the number of the actuator up? limit switch pin
const int limit2 = 6; // the number of the actuator down? limit switch pin
const int ActUp = 11; // the number of the pin controlling actuator up movement
const int ActDwn = 8; // the number of the pin controlling actuator down movement
const int SSbutton = 3; // Start/Stop (SS) button Pin
int limit1State; // the current reading from the input pin
int lastlimit1State = LOW; // the previous reading from the input pin
int limit2State; // the current reading from the input pin
int lastlimit2State = LOW;
int testSS = 0; // used for determining if SS button is pressed
boolean currentSSState = LOW; //strorage for current SS button state
boolean lastSSState = LOW; //storage for last button state
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long debounceDelay1 = 10; // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 10;
void setup() {
Serial.begin(9600);
pinMode(limit1, INPUT); //using 10k Ohm resistors on my bread board
pinMode(limit2, INPUT);
pinMode(ActUp, OUTPUT);
pinMode(ActDwn, OUTPUT);
pinMode(SSbutton, INPUT);
}
void loop() {
currentSSState = digitalRead(SSbutton);
if (currentSSState == HIGH && lastSSState == LOW) {
delay(1);
if (testSS == 0) {
testSS = 1;
Serial.print(testSS);
}
else {
testSS = 0;
Serial.print(testSS);
}
}
lastSSState = currentSSState;
if (testSS == 0) {
digitalWrite(ActUp, HIGH); // these 2 lines stop all movement when SSbutton is pressed
digitalWrite(ActDwn, HIGH);
}
if (testSS == 1) {
ActuatorUp(); // functions defined below to move determine when limit switch is pressed then change direction
ActuatorDown();
}
}
/////// Actuator Going Up then Change Direction and Go Down Once Limit Swith is pressed //////////////////
void ActuatorUp() {
int reading1 = digitalRead(limit1);
// Serial.print(ActUp);
if (reading1 != lastlimit1State) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
if (reading1 != limit1State) {
limit1State = reading1;
if (limit1State == HIGH || ActUp == HIGH && ActDwn == HIGH ) { //trying to use OR statement here
digitalWrite(ActUp, HIGH);
digitalWrite(ActDwn, LOW);
}
}
}
lastlimit1State = reading1;
}
/////// Actuator is Going Down then Change Direction and Go Up Once Limit Swith is pressed //////////////////
void ActuatorDown() {
int reading2 = digitalRead(limit2);
if (reading2 != lastlimit2State) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if (reading2 != limit2State) {
limit2State = reading2;
if (limit2State == HIGH) {
digitalWrite(ActDwn, HIGH);
digitalWrite(ActUp, LOW);
} else {
}
}
}
lastlimit2State = reading2;
}