I'm a newbie, and had the help of a friend with some Uno experience, but still having problems getting this to work. Need help please. Trying to run the motor forward (+110) until it hits UpSw (limit switch). Then want the motor to stop (0), delay 7 seconds, and run in reverse (-110) until it hits DnSw (limit switch). Then stop (0), delay 7 seconds, and run forward (+110), and continuing the loop. I have tried a number of sketches, and nothing seems to get the switches to engage in stopping and/or reversing the motor. Thank you for any help you can lend me. Here's the last sketch I wrote:
#include <SyRenSimplified.h>
SyRenSimplified SR;
const int Motor = 1;
const int UpSw = 2;
const int DnSw = 3;
int MotorState = 0;
int UpSwState;
int lastUpSwState = LOW;
int DnSwState;
int lastDnSwState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
SyRenTXPinSerial.begin(9600);
pinMode(Motor, OUTPUT);
pinMode(UpSw, INPUT);
pinMode(DnSw, INPUT);
SR.motor(1, 0);
}
void loop() {
SR.motor(1, 110);
digitalRead(UpSw);
if (UpSwState == HIGH) {
SR.motor(1, 0);
delay(7000);
SR.motor(1, -110);
}
digitalRead(DnSw);
if (DnSwState == HIGH) {
SR.motor(1, 0);
delay(7000);
}
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
You need to assign a value to UpSwState and DnSwState. Change:
digitalRead(UpSw);
to
UpSwState = digitalRead(UpSw);
and
digitalRead(DnSw);
to
DnSwState = digitalRead(DnSw);
How do you have your limit switches wired? If you don't have a pull down resistor then the pins will be floating so digitalRead() will give random results when the switch isn't triggered. Better would be to connect the switch between the digital pin and ground and then do:
pinMode(UpSw, INPUT_PULLUP);
Then the pin will be HIGH until the switch is hit, when it will be LOW. So you would need to change
if (UpSwState == HIGH) {
to
if (UpSwState == LOW) {
When you post code or error/warning messages please use code tags(</> button on the toolbar) and always use Tools > Auto Format on your code. This will make it easier to read and spot bugs.
Thanks for the tips. I've got the switches wired as normally open. 5V coming from the Uno, and when tripped to be passing the 5V to pins 2&3 respectively.
cdhc6slyc:
I've got the switches wired as normally open. 5V coming from the Uno, and when tripped to be passing the 5V to pins 2&3 respectively.
Since you don't have pull-down resistors your pins 2 and 3 are floating which means they can give random HIGH and LOW readings when the switch is not closed. You want to set the pin to a known state when the switch is open. With your current wiring of the switch being connected between the Arduino pin and 5V, this would be accomplished by connecting a pull-down resistor(somewhere around 10KOhm) between the pin and GND. This means when the switch is open the reading will always be LOW but when the switch is closed the resistor will be overcome and the reading will be HIGH. Much more convenient is to use the Arduino's internal pull-up resistors which are activated by pinmode(2, INPUT_PULLUP) but since they are pull-up(connected to 5V), instead of pull-down resistors this means you would need to change your wiring so that the switch is connected between the Arduino pin and GND. This means that switch open is HIGH and switch closed is LOW.
Pert, this is getting very close. Per your suggestion I've added a 10K Ohm resistor between each pin 2&3 & GND, and have rewritten the code (below). When I run this new sketch, the motor goes +110 until the UpSw is tripped at which time the motor goes 0. That is great, but now the problem is that as long as UpSw stays tripped, the motor stays at 0 (I want the motor to go -110 after a 7 second delay). And if I then release UpSw, the motor continues at +110 rather than going -110. I've also separately tested DnSw. Tripping it also sends the motor to 0, and has the same further effect as the UpSw. Any other suggestions? The latest sketch:
#include <SyRenSimplified.h>
SyRenSimplified SR;
const int Motor = 1;
const int UpSw = 2;
const int DnSw = 3;
int MotorState = 0;
int UpSwState;
int lastUpSwState = LOW;
int DnSwState;
int lastDnSwState = LOW;
void setup() {
SyRenTXPinSerial.begin(9600);
pinMode(Motor, OUTPUT);
pinMode(UpSw,INPUT);
pinMode(DnSw,INPUT);
SR.motor(1, 0);
}
void loop() {
SR.motor(1, 110);
UpSwState = digitalRead(UpSw);
if (UpSwState == HIGH) {
delay(25);
SR.motor(1, 0);
delay(7000);
SR.motor(1, -110);
}
DnSwState = digitalRead(DnSw);
if (DnSwState == HIGH) {
delay(25);
SR.motor(1, 0);
delay(7000);
}
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
So it only gets a chance to run at -110 for a couple of microseconds which isn't enough time for the motor to even react. So if you want it to change direction after each switch trip you could do something like:
if (UpSwState == HIGH) {
delay(25);
SR.motor(1, 0);
delay(7000);
direction = -1;
}
See if you can figure out how to integrate that system into the rest of your code.
When I run this new sketch, the motor goes +110 until the UpSw is tripped at which time the motor goes 0. That is great, but now the problem is that as long as UpSw stays tripped, the motor stays at 0 (I want the motor to go -110 after a 7 second delay).
What you need to do is detect the switch going HIGH, as in changing state, not being HIGH.
That way each time you read the switch state, compare it with the previous time you read the switch state, if it has gone form LOW to HIGH, then set a flag HIGH, to stop the motor, if on the next time you look at the switch and it is still HIGH or gone LOW, set the flag LOW.
The flag going to HIGH will then start you stop timer etc etc.