I have the following program that is suppose to move a linear actuator to various positions based on the momentary push of one of two buttons, one for extend and on for retract.
When I download the sketch seems to ignore the whole 'updown' area of the sketch and the actuator keeps moving in and out. If I push the button the 'swcnt' changes but it has no impact
Here's what the Serial monitor shows and the sketch
#define IN_PIN 4 // the Arduino pin connected to the IN1 pin L298N
#define OUT_PIN 5 // the Arduino pin connected to the IN2 pin L298N
#define POTENTIOMETER_PIN A0 // the Arduino pin connected to the potentiometer of the actuator
int swcnt = 0;
#define button1Pin A1 // the number of the pushbutton1 pin
#define 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 TargetPosition[7] = {72, 97, 122, 147, 172, 197, 222};
#define TOLERANCE 3
void setup() {
Serial.begin(9600);
pinMode(IN_PIN, OUTPUT);
pinMode(OUT_PIN, OUTPUT);
pinMode (button1Pin, INPUT_PULLUP);
pinMode (button2Pin, INPUT_PULLUP);
}
void updown() {
Serial.println(swcnt);
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == LOW && button2State == HIGH) {
swcnt++;
if (swcnt > 6 ) {
(swcnt = 6 );
Serial.println(swcnt);
}}
if (button1State == HIGH && button2State == LOW) {
swcnt--;
Serial.println("re ");
if (swcnt < 0) {
(swcnt = 0 );
Serial.println(swcnt);
}}}
void loop() {
updown();
delay(3500);
int stroke_pos = analogRead(POTENTIOMETER_PIN);
//int stroke_pos = potentiometer_value;
Serial.print("The stroke's position = ");
Serial.print(stroke_pos);
Serial.println(" ");
Serial.print("The targets position = ");
Serial.print(TargetPosition[swcnt]);
Serial.println(" ");
if (stroke_pos < (TargetPosition[swcnt] - TOLERANCE))
ACTUATOR_extend();
else if (stroke_pos > (TargetPosition[swcnt] + TOLERANCE))
ACTUATOR_retract();
else
ACTUATOR_stop();
}
void ACTUATOR_extend() {
digitalWrite(IN_PIN, LOW);
digitalWrite(OUT_PIN, HIGH);
}
void ACTUATOR_retract() {
digitalWrite(IN_PIN, HIGH);
digitalWrite(OUT_PIN, LOW);
}
void ACTUATOR_stop() {
digitalWrite(IN_PIN, HIGH);
digitalWrite(OUT_PIN, HIGH);
}
