/*
Forum: https://forum.arduino.cc/t/unexpected-activation-after-long-months-run/1385981
Wokwi: https://wokwi.com/projects/432557652667267073
*/
// constants won't change. They're used here to set pin numbers:
int RPWM = 10; //connect Arduino pin 10 to IBT-2 pin RPWM
int LPWM = 11; //connect Arduino pin 11 to IBT-2 pin LPWM
int buttonPin = 2; // the number of the pushbutton pin
// variables will change:
byte Speed = 0; // Intialize Varaible for the speed of the motor (0-255);
byte Move = 0; // Intialize Varaible for the speed of the motor (0-255);
int buttonState = 0; // variable for reading the pushbutton status
int direction = 1;
void setup() {
// setup code runs once:
pinMode(10, OUTPUT); // Configure pin 10 as an Output
pinMode(11, OUTPUT); // Configure pin 11 as an Output
pinMode(buttonPin, INPUT_PULLUP); // Configure button
}
void loop() {
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// Check For Button Push
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Move = 1; //set actuator to move state
}
else { //if no button is pushed, remain stationary
Move = 0;
}
// Extend Actuator ------------------------------------------------------
if (Move == 1 && direction == 1) {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 100; fadeValue <= 255; fadeValue += 5) {// sets the value (range from 0 to 255):
analogWrite(10, fadeValue);
analogWrite(11, 0);
// wait for 85 milliseconds to see the dimming effect
delay(85);
}
Move = 0; //Actuator reaches end of extension
// Stop Actuator
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
direction = 2; // Set to retract
}
else { //if no button is pushed, remain stationary
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
Move = 0;
}
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
// Check For Button Push
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Move = 1;
}
else { //if no button is pushed, remain stationary
Move = 0;
}
// Retract Actuator ----------------------------------------------------------------------
if (Move == 1 && direction == 2) {
// fade out from max to min in increments of 5 points:
for (int fadeValue = 50; fadeValue <= 255; fadeValue += 5) {// sets the value (range from 0 to 255):
// sets the value (range from 0 to 255):
analogWrite(10, 0);
analogWrite(11, fadeValue);
// wait for 84 milliseconds to see the dimming effect
delay(84);
}
Move = 0;
// Stop Actuator
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
direction = 1; // Set back to extend
}
else { //if no button is pushed, remain stationary
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
Move = 0;
}
}