elp, please.
Old computer user but Arduino newbie.
I've been developing a sketch to control a dc motor to drive a carriage back and forth along a track, between limit switches.
The setup part starts with a button push, activates a relay, waits 5 seconds, starts the motor in a specic direction towards the limit switch then proceeds to the loop setion.
In the loop te first part generates a pair of random numbers which go to control the speed of the motor in each direction, changing on each repetion of the loop and reversing the motor each time a limit swith is activated. So far so good. the attached sketch does all of this. Where I am not succeeding is trying to define the number of times the back and forth motion happens. I've tried "for" loops and "while" loops without success. This is where I will be grateful for any suggestions or guidance. If possible, I would also like to reset at this point so the whole process can be repeated by pressing the start button.
For information, the sketch is based on a tutorial in the arduino forum and I've corrected a few errors to get to this stage. It uses the ezbutton library and I believe it was created by the author of the library. I'm using a UNO R4 Wifi and a motor shield. I spent several days trying to the debugger working, so I could see what was happening and so put the "for" or "while" in the right place. That is an exercise in frustration.
#include <ezButton.h>
#define DIRECTION_CCW -1
#define DIRECTION_CW 1
#define ENA_PIN 3 // The Arduino pin connected to the EN1 pin L298N
#define IN1_PIN 12 // The Arduino pin connected to the IN1 pin L298N
//#define IN2_PIN 9 // The Arduino pin connected to the IN2 pin L298N
//define relay pin here
ezButton limitSwitch_1(4); // create ezButton object that attach to pin 4
ezButton limitSwitch_2(5); // create ezButton object that attach to pin 5
ezButton button(7); // create ezButton object that attach to pin 7;
int direction = DIRECTION_CW;
int prev_direction = DIRECTION_CW;
void setup() {
Serial.begin(9600);
limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds
button.setDebounceTime(50); // set debounce time to 50 milliseconds
int seed = analogRead(0);
randomSeed(seed);
while (!button.isPressed())
button.loop(); // MUST call the loop() function first
// initialize digital pins as outputs.
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
//pinMode(IN2_PIN, OUTPUT);
//insert relay command here
delay(5000);
analogWrite(ENA_PIN, 250); // max speed
digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise
//digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise
}
void loop() {
int var;
var = 0;
while (var <5) {
//for ( int i = 0; i < 4; i++) {
int Sp1 = (random(5,9)*25); // random speed 1
int Sp2 = (random(5,9)*25); // random speed 2
limitSwitch_1.loop(); // MUST call the loop() function first
limitSwitch_2.loop(); // MUST call the loop() function first
if (limitSwitch_1.isPressed()) {
direction *= -1; // change direction
Serial.println(F("The limit switch 1: TOUCHED"));
}
if (limitSwitch_2.isPressed()) {
direction *= -1; // change direction
Serial.println(F("The limit switch 2: TOUCHED"));
}
if (prev_direction != direction) {
Serial.print(F("The direction -> "));
if (direction == DIRECTION_CW) {
Serial.println(F("CLOCKWISE"));
digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise
analogWrite(ENA_PIN, Sp1);
//digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise
} else {
Serial.println(F("ANTI-CLOCKWISE"));
digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise
analogWrite(ENA_PIN, Sp2);
//digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise
}
prev_direction = direction;
}
//}
var++;
}
}