I'm working on a project that moves a linear actuator to specific positions based on one of two buttons are pressed. I use 2 arrays to determine positions based on the count of each switch. Somehow I have my if statements out of sync or there is a bug in my logic.
const int relay1Pin = 4; // the number of the Relay1 pin
const int relay2Pin = 5 ; // the number of the Relay2 pin
const int sensorPin = A0; // select the input pin for the LA
void setup() {
int sensorValue = 0; // variable to store the value coming from the sensor
int swcnt = -1;
int CurrentPosition = 0;
const int button1Pin = A1; // the number of the pushbutton1 pin
const int button2Pin = A2; // the number of the pushbutton2 pin
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int EXgoalPosition[7] = {72, 97, 122, 147, 172, 197, 222};
int REgoalPosition[7] = {78, 103, 128, 153, 178, 203, 228};
void setup() {
Serial.begin(9600);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
CurrentPosition = analogRead(sensorPin);
digitalWrite (button2Pin, LOW);
digitalWrite(button1Pin, LOW);
}
void loop() {
delay(5000);
Serial.print(swcnt);
CurrentPosition = analogRead(sensorPin);
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH && button2State == LOW) {
swcnt = ++swcnt;
Serial.print(button1State);
Serial.print (CurrentPosition);
Serial.print(" ");
Serial.print(EXgoalPosition[swcnt]);
Serial.print(" ");
Serial.print(REgoalPosition[swcnt]);
Serial.print(" ");
Serial.println("ex ");
Serial.println(" ");
if (swcnt > 6 ) {
(swcnt = 6 );
}
if (EXgoalPosition[swcnt] > CurrentPosition) {
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
Serial.println("Extending");
}
if (EXgoalPosition[swcnt] < CurrentPosition) {
digitalWrite(relay2Pin, LOW);
}
} else; {
if (button1State == LOW && button2State == HIGH) {
swcnt = --swcnt;
Serial.println("re ");
if (swcnt < 0) {
(swcnt = 0 );
}
if (REgoalPosition[swcnt] < CurrentPosition) {
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Retracting");
}
if (REgoalPosition[swcnt] > CurrentPosition) {
digitalWrite(relay1Pin, LOW);
}
}
}
button1State = LOW;
button2State = LOW;
}
}
are wrong. You don't need the assignment with the pre-increment/decrement operator since it does it for you automatically. It is also much more common to see the post-inrement/decrement operator (++ after the variable)
I made all the suggested changes, it compiles but somehow ever without pushing a button it runs the **BOLD * code
const int relay1Pin = 4; // the number of the Realy1 pin
const int relay2Pin = 5 ; // the number of the Relay2 pin
const int sensorPin = A0; // select the input pin for the potentiometervoid
setup() {
// put your setup code here, to run once:
int sensorValue = 0; // variable to store the value coming from the sensor
int brkswState = 0;
int swcnt = -1;
int CurrentPosition = 0;
const int button1Pin = A1; // the number of the pushbutton1 pin
const int button2Pin = A2; // the number of the pushbutton2 pin
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int EXgoalPosition[7] = {72, 97, 122, 147, 172, 197, 222};
int REgoalPosition[7] = {78, 103, 128, 153, 178, 203, 228};
void setup() {
//start serial connection
Serial.begin(9600);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
CurrentPosition = analogRead(sensorPin);
digitalWrite (button2Pin, LOW);
digitalWrite(button1Pin, LOW);
}
void loop() {
delay(5000);
Serial.print(swcnt);
CurrentPosition = analogRead(sensorPin);
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH && button2State == LOW) {
swcnt++;
Serial.print(button1State);
Serial.print (CurrentPosition);
Serial.print(" ");
Serial.print(EXgoalPosition[swcnt]);
Serial.print(" ");
Serial.print(REgoalPosition[swcnt]);
Serial.print(" ");
Serial.println("ex ");
Serial.println(" ");
if (swcnt > 6 ) {
(swcnt = 6 );
}
if (EXgoalPosition[swcnt] > CurrentPosition) {
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
Serial.println("Extending");
}
if (EXgoalPosition[swcnt] < CurrentPosition) {
digitalWrite(relay2Pin, LOW);
}
} else {
if (button1State == LOW && button2State == HIGH) {
swcnt--;
Serial.println("re ");
if (swcnt < 0) {
(swcnt = 0 );
}
if (REgoalPosition[swcnt] < CurrentPosition) {
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Retracting");
}
if (REgoalPosition[swcnt] > CurrentPosition) {
digitalWrite(relay1Pin, LOW);
}
}
}
button1State = LOW;
button2State = LOW;
}
How are your buttons wired up? They need external pullup or pulldown resistors to work properly. Or, you can change your code to use the internal pullups. But it depends on how they are wired.
const int relay1Pin = 4; // the number of the Realy1 pin
const int relay2Pin = 5 ; // the number of the Relay2 pin
const int sensorPin = A0; // select the input pin for the potentiometervoid
setup() {
There are the first four lines of your sketch. It will not compile; the problem is line four, and the solution is to delete it:
const int relay1Pin = 4; // the number of the Realy1 pin
const int relay2Pin = 5 ; // the number of the Relay2 pin
const int sensorPin = A0; // select the input pin for the potentiometervoid
Please draw a diagram showing what your words do not. Are you using a pull up or a pull down resistor?
You explicitly turn off the internal pull-up, can we assume you knew that's what you were doing when you
Then they are most likely "floating" when not pressed which means you will get a HIGH or LOW reading randomly.
The best method is to wire one pin of the button to ground and the other pin to your arduino pin. You then declare it as INPUT_PULLUP in setup(). This means the button will read HIGH when not pressed and LOW when pressed.
Yes, especially if you aren't using any pull-up or pull-down resistors. Which don't seem to be in your drawing, so.
I don't understand why the bad code compiles for you, it is an error of a fatal kind. Without knowing better, I'd have to guess something is very odd, or you aren't being careful as you tinker and can't report reliably your observations and what code you are verifying or compiling.
Go to the IDE preferences and dial up the warnings and verbosity as much as you can, then compile with line four present as you claimed was no problem.
As I said, it should have been. That it wasn't may mean you have something unexplained happening to you. This should be found and fixed, or your work will be harder.
Programming is hard enough when the tools are working properly and you can trust them.