Hi All,
I have been running this sketch and just can't seem to ask google the right question to get the answer so I thought I would add it here and see if anyone could point me in the right direction.
I'm running a couple steppers and a relay at the press of a momentary button and then turning everything off until the press of the button to start it again. The problem I'm having is the sketch will run fine with the push of the button and do what I want but sometimes it will run 2 more times before stopping and wait for the button to start it again.
Any help in the right direction is appreciated.
#include <Wire.h>
#include <Stepper.h>
Stepper myStepper1=Stepper(200, 5, 6); //table slider pin 5 and 6
Stepper myStepper2=Stepper(200, 8, 9); //substrate rotate pin 8 and 9
const int gluemac = 11; // pin to actuate glue machine relay
const int button = 2; // pin button is on
int val = 0; // current button state
int old_val = 0;
int state = 0;
void setup()
{
pinMode(button, INPUT_PULLUP);
pinMode(gluemac, OUTPUT);
digitalWrite(gluemac, HIGH); //relay is active low
myStepper1.setSpeed(3000); // table slide
myStepper2.setSpeed(100); //substrate rotate
}
void loop()
{
val = digitalRead(button); //read input value and store it
if ((val == HIGH) && (old_val == LOW)){ //check if there was a transition
state = 1 - state;
delay(50); //button debounce
}
old_val = val; //store old value
if (state == 1) {
digitalWrite(button, HIGH);
} else {
digitalWrite(button, LOW); //turn button on
myStepper1.step(-6000); //slide table in
delay(25);
digitalWrite(gluemac, LOW); //turn glue machine on
myStepper2.step(1600); //rotate 1 time
digitalWrite(gluemac, HIGH); //turn glue off
myStepper1.step(6000); // slide table out
state = 1; //turn everything off for next button push
}
}
ribbonman:
The problem I'm having is the sketch will run fine with the push of the button and do what I want but sometimes it will run 2 more times before stopping and wait for the button to start it again.
"Sometimes" is a red flag to me. I'm guessing that you're getting an electrical transient that is crashing/resetting your Arduino board and that the transient is coming from one or both of the stepper motors.
If you can, try to power the steppers with a different power supply (not connected to the Arduino) and see if that helps.
Also, try putting a large (470 uF or larger) electrolytic capacitor across the Arduino 5 volt and ground pins (proper polarity of course) and see if this helps.
If you have an oscilloscope, look at your Arduino power rail and see if it's getting spikes or noise (also look for ground bounce).
J-M-L:
You are aware that your loop is executing the second part of the loop
if (state == 1) {
digitalWrite(button, HIGH);
} else {...
every time right?
why don't you do that only when you get the HIGH -> LOW transition?
(as a side note, 15ms is usually enough to debounce most buttons - at least the ones I'm playing with)
Thanks for the response J-M-L, I got the code from a book by Massimo Banzi and working with Arduino, it works for me but I'm a little fuzzy about it's logic but it does work, I tested it with an LED to make sure.
I realize it is running the else part of the loop but that is how I got it to work so I went with it, not sure how else to do it.
Krupski:
"Sometimes" is a red flag to me. I'm guessing that you're getting an electrical transient that is crashing/resetting your Arduino board and that the transient is coming from one or both of the stepper motors.
If you can, try to power the steppers with a different power supply (not connected to the Arduino) and see if that helps.
Also, try putting a large (470 uF or larger) electrolytic capacitor across the Arduino 5 volt and ground pins (proper polarity of course) and see if this helps.
If you have an oscilloscope, look at your Arduino power rail and see if it's getting spikes or noise (also look for ground bounce).
I'll bet your problem is electrical spikes.
Good luck.
Hi Krupski,
I do have that problem with the relay switching a high voltage AC load and will work to fix it but I'm not sure if that is the problem with the sketch running 2 extra times. I have removed all connections from the arduino except for the button and it still wants to run, I'm going to get a new momentary button and try that because the led in the lighted button is not working so I'm thinking maybe to much current has gone into it and messed up the rest of the button, the button seems to operate from the slightest of vibrations now.
I actually find surprising the idea of doing digitalWrite(button, HIGH); or digitalWrite(button, LOW); (button is configured in the setup() as INPUT_PULLUP and digitalWrite() enables (HIGH) or disables (LOW) the internal pullup on the input pin.
That feels weird.. how are things wired? do you just have the momentary button and ground connected on pin2? (Pin2 --> button --> GND) ? Why mess with the button ?
J-M-L:
I actually find surprising the idea of doing digitalWrite(button, HIGH); or digitalWrite(button, LOW); (button is configured in the setup() as INPUT_PULLUP and digitalWrite() enables (HIGH) or disables (LOW) the internal pullup on the input pin.
That feels weird.. how are things wired? do you just have the momentary button and ground connected on pin2? (Pin2 --> button --> GND) ? Why mess with the button ?
Are you sure you copied the code correctly?
I used the INPUT_PULLUP to use the onboard resistor for the button so I wouldn't have to add one to the button, without it the button doesn't work for me.
The button is connected with the positive side going to pin 2 and the negative going to the arduino GND, I have a lighted momentary button with the polarities marked.
The code is copy and pasted so that is what I'm using for now, my button is pretty flaky so I'm getting another to see if maybe my problem is hardware and not software related.
I used the INPUT_PULLUP to use the onboard resistor for the button so I wouldn't have to add one to the button, without it the button doesn't work for me.
The button is connected with the positive side going to pin 2 and the negative going to the arduino GND, I have a lighted momentary button with the polarities marked.
else {
digitalWrite(button, LOW); //turn button on
This line is turning off the INPUT_PULLUP and the button will be floating. The flaky button and the response to the slightest vibration are consistent with a floating condition.
if ((val == HIGH) && (old_val == LOW)){
This logic is designed to pick up the release of the button after having been pressed, and not the press of the button. Is that what you intended?
This line is turning off the INPUT_PULLUP and the button will be floating. The flaky button and the response to the slightest vibration are consistent with a floating condition.
if ((val == HIGH) && (old_val == LOW)){
This logic is designed to pick up the release of the button after having been pressed, and not the press of the button. Is that what you intended?
Hi cattledog,
As I'm going over my notes I see I used it without the INPUT_PULLUP at first and I saw a suggestion to use it so I tried it to see if there was a different result using it and I didn't see one and just never changed it back. The code is line for line as the original code I found except for the PULLUP.
The logic of the code is still obviously a little more than I understand by your comments about what is happening with the press of the button, what I wanted to happen is just have the momentary button turn everything on and run the code and then have everything stop to wait for a button push to start it up again.
I thought I understood what was going on but I guess not.
J-M-L:
I've the 3rd Edition of that book but don't see that code there
as I said in #5, this pullup thingy is weird... it would be good to double check how they recommended to wire things
book is 2nd edition
I looked at the book at the picture shows this
GND to 10K resistor to one side of switch with pin 2 attached to it
+5V to other side of switch
using onboard pin13 for led
const int ledPin = 13; // choose the pin for the LED
const int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // current button state
int old_val = 0;
int state = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if ((val == HIGH) && (old_val == LOW)){ //check if there was a transition
state = 1 - state;
delay(10); //button debounce
}
old_val = val; //store old value
if (state == 1) {
digitalWrite(ledPin, HIGH); // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON
}
}
The sketch you just posted looks correct, and the wiring is for a pull down to ground and an external 5v to one side of the switch. Pressed will be HIGH and not pressed will be LOW. The sketch uses digital write with the led output pin and not the input button pin.
cattledog:
The sketch you just posted looks correct, and the wiring is for a pull down to ground and an external 5v to one side of the switch. Pressed will be HIGH and not pressed will be LOW. The sketch uses digital write with the led output pin and not the input button pin.
Where do you want to go from here?
I understand what the schematic and the code are telling me, I think, and I understand that the switch is now acting like a rocker switch that turns the LED on and off with a push of the button.
I see this is really a hardware problem not a software problem, I just have to figure how to take it from the breadboard to an actual working button with a resistor on it, without the resistor(I thought incorrectly that I had an on board one) it won't work properly.
Thanks for the help cattledog.
ribbonman:
I see this is really a hardware problem not a software problem,
it's both!!! as I told you in #5 changing the pullup status of the button is fishy...
do you see the difference between what you posted earlier
old_val = val; //store old value
if (state == 1) {
digitalWrite([color=red]button[/color], HIGH);
} else {
digitalWrite([color=red]button[/color], LOW); //[color=red]turn button on[/color]
and what you posted now
old_val = val; //store old value
if (state == 1) {
digitalWrite([color=green]ledPin[/color], HIGH); // turn LED OFF
} else {
digitalWrite([color=green]ledPin[/color], LOW); // turn LED ON
}
how could have you copied and pasted??
looks like you did more than just changing the INPUT into INPUT_PULLUP...
I just have to figure how to take it from the breadboard to an actual working button with a resistor on it, without the resistor(I thought incorrectly that I had an on board one) it won't work properly.
You were correct in attempting to use INPUT_PULLUP with the button. There is no external resistor required, and it's safer without 5v running around the board. I think you were previously wired correctly for INPUT_PULLUP with one side of the switch to ground and the other side to the arduino input pin.
You only need to reverse the logic of your sketch.
const int ledPin = 13; // choose the pin for the LED
const int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // current button state
int old_val = 0;
int state = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT_PULLUP); // declare pushbutton with internal pullup resistor enabled
}
void loop(){
val = digitalRead(inPin); // read input value
if ((val == LOW) && (old_val == HIGH)){ //check if there was a transition
state = 1 - state;
delay(10); //button debounce
}
old_val = val; //store old value
if (state == 1) {
digitalWrite(ledPin, HIGH); // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON
}
}
J-M-L:
it's both!!! as I told you in #5 changing the pullup status of the button is fishy...
do you see the difference between what you posted earlier
old_val = val; //store old value
if (state == 1) {
digitalWrite(button, HIGH);
} else {
digitalWrite(button, LOW); //turn button on
and what you posted now
old_val = val; //store old value
if (state == 1) {
digitalWrite(ledPin, HIGH); // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON
}
how could have you copied and pasted??
looks like you did more than just changing the INPUT into INPUT_PULLUP...
The first code I have been working on for awhile so I don't doubt that it's not the same, I've tried so many things that I will start from the beginning from square one to get a working button and then add to that.
The second code I copy and pasted from the book so everyone could see where I was coming from and as you stated in pointing out where it all came off the rails for me. I was trying to be careful but when your trying a million things to see how they work sometimes you miss things.
I can't thank everyone enough for explaining what was going helping me understand the concept of the button, a little better anyhow.
Here is the code I ended up with after talking to everyone, I removed the else part of the code as I really didn't need it but let me know if I'm wrong.
#include <Wire.h>
#include <Stepper.h>
Stepper myStepper1=Stepper(200, 5, 6); //table slider pin 5 and 6
Stepper myStepper2=Stepper(200, 8, 9); //substrate rotate pin 8 and 9
const int gluemac = 11; // pin to actuate glue machine relay
const int button = 2; // pin button is on
int val = 0; // current button state
int old_val = 0;
int state = 0;
void setup()
{
pinMode(button, INPUT_PULLUP);
pinMode(gluemac, OUTPUT);
digitalWrite(gluemac, HIGH); //relay is active low
Serial.begin(9600);
myStepper1.setSpeed(3000); // table slide
myStepper2.setSpeed(500); //substrate rotate
}
void loop()
{
val = digitalRead(button); //read input value and store it
if ((val == LOW) && (old_val == HIGH)){ //check if there was a transition
state = 1 - state;
delay(15); //button debounce
}
old_val = val; //store old value
if (state == 1) {
myStepper1.step(6000); //slide table in
Serial.println("clockwise");
delay(25);
digitalWrite(gluemac, LOW); //turn glue machine on
Serial.println("relay_on");
myStepper2.step(1600); //rotate 1 time
Serial.println("rotate");
digitalWrite(gluemac, HIGH); //turn glue off
Serial.println("relayoff");
myStepper1.step(-6000); // slide table out
Serial.println("counterclockwise");
state = 0; //turn everything off
}
}