I'm trying to build a cumulative recorder. If you don't know what that is click here
Essentially where I am at is I have a stepper motor, a button and a limit switch set up and everytime I click the button the motor needs to move a few steps and then when the pen hits the limit switch it needs to reset and go back a number of steps.
I've been messing around with the code for days and been able to get it to do pretty much everything but that. Right now, I'm thinking maybe a do while loop might work best for what I've got set up, but it keeps giving me errors that make no sense. As soon as I delete the do while loop it loads fine. This is what I've got for code at the moment.
do {
if (reading_Button == HIGH) {
myStepper.step(Forward_Rev);
while (reading_Switch == HIGH); {
break;
myStepper.step(Backward_Rev);
return;
}
}
}
Any guidance would be appreciated wether its to completely scrap the path that I am on, or to correct the do while code.
Looking at your code, I think you are mistaken about what it is doing. Instead of messing around with the code for days, you need to spend a few days learning C.
The "errors that make no sense" is because your code doesn't make sense. If you format your code so that it is easy to match up the brackets you can see why it doesn't make sense on several levels:
do {
if (reading_Button == HIGH) {
myStepper.step(Forward_Rev);
while (reading_Switch == HIGH); {
break;
myStepper.step(Backward_Rev);
return;
}
}
}
Thank you for your time. I greatly appreciate your support even though this didn't accomplish what I wanted. I only post in places like this when I really need help, even though I know 99% of the replies will be people criticizing the fact that I don't fully understand what I am doing. I am a hobbyist who builds an Arduino project once every 2 years so yes, I don't fully understand what I am doing or how to code let alone mastering the syntax. I wish more people could understand that sort of thing.
Can you please post your complete code show we can see your basic variable and pin setup.
Have you been able to JUST get the stepper to rotate backwards and forwards before attempting to do any switch detecting?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.
do statement while (expression); is all one statement. You can't put the 'do' before an 'if' and the 'while' inside the 'if'. If the 'do/while' contains a block statement the '{' after the 'do' must match the '}' before the 'while'.
I hope this diagram makes sense to you. Im running an Arduino Uno hooked up to a 6 pin stepper motor (the program to make the diagram only had a 5 pin) with a ULN2003AN driver. A push button hooked up to pin 2 is supposed to make the driver step forward half a revolution everytime the button is pressed. Eventually what is hooked up to the motor will hit a limit switch (the 4 pole switch in the diagram) which is hooked up to pin 13 at which point it will put the motor into reverse and go back "x" number of steps and restart the process. I would also eventually like to add in a servo motor to operate with the push button as well, but I figured I'd work on one component at a time. I had the motor working on its own, I had the push button controlling the motor as it's supposed to but adding the limit switch in is where I had issues. I was able to get it to make it go in reverse, but only after it completed half a revolution which could cause mechanical failures. It should stop as soon as the switch is depressed and then reverse.
The code I've got is mostly copied and pasted from google with some changes made by me. As other people have very kindly made obvious, I don't fully understand what I am doing and I build Arduino projects very infrequently. So yes it is definitely a mess and probably all kinds of incorrect but here it is.
#include <Stepper.h>
int Limit_Switch = 13;
int Push_Button = 2;
const int Steps_Per_Rev = 200;
int Switch_State = 0;
int Button_State = 0;
const float Gear_Reduc = -.5;
const float Gear_Reduc_Rev = 4;
const float Forward_Rev = Steps_Per_Rev * Gear_Reduc;
const float Backward_Rev = Steps_Per_Rev * Gear_Reduc_Rev;
int lastButtonState = LOW; // the previous reading from the input pin
int lastSwitchState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
Stepper myStepper(Steps_Per_Rev, 8, 9, 10, 11);
void setup()
{
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(Limit_Switch, INPUT);
pinMode(Push_Button, INPUT);
}
void loop()
{
int reading = digitalRead(Push_Button);
// read the state of the switch into a local variable:
int reading_Button = digitalRead(Push_Button);
// read the state of the switch into a local variable:
int reading_Switch = digitalRead(Limit_Switch);
// If the switch changed, due to noise or pressing:
if (reading_Button != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != Button_State) {
Button_State = reading;
}
}
while (1)
{
if (reading_Button == HIGH)
{
myStepper.step(Forward_Rev);
do
{
;
}
while (reading_Switch != HIGH);
myStepper.step(Backward_Rev);
break;
}
}
//if(Limit_Switch == HIGH) {
//goto Forward;
//} else {
//goto Reverse;
//}
//Reverse:
//myStepper.step(Backward_Rev);
//Forward:
//if (Push_Button == HIGH) {
//myStepper.step(Forward_Rev);
//}
}
I appreciate you asking for more information and not being condescending like some others! Much easier to learn when people are actually being helpful
I missed this until just now. I replied to JCA34F already with this information below. Hopefully it is somewhat legible. I appreciate your support! Please let me know if you need more info or any clarification. The program I used to do the diagram didn't have all the components I used so the big rectangle is the ULN2003 driver.
Rather than defining your buttons as INPUT, it is simpler to define as INPUT_PULLUP. This means they are internally pulled HIGH... so then you then don't need the external pulldown resistors. You just need to connect that other side of the buttons to GND, and check for LOW.
2). Don't worry about button bounce for now... it is just confusing something quite simple. So you can get rid of all the assigning read results to local variables, and the timing stuff.
3). Remove all the other stuff you're not actually using, including the silly while and do loops... and that leaves the following very simple code. Let me know if there is something you don't understand.
#include <Stepper.h>
int Push_Button = 2;
int Limit_Switch = 13;
const int Steps_Per_Rev = 200;
const float Gear_Reduc = -0.5;
const float Gear_Reduc_Rev = 4;
const float Forward_Rev = Steps_Per_Rev * Gear_Reduc;
const float Backward_Rev = Steps_Per_Rev * Gear_Reduc_Rev;
Stepper myStepper(Steps_Per_Rev, 8, 9, 10, 11);
void setup()
{
myStepper.setSpeed(60);
pinMode(Limit_Switch, INPUT_PULLUP);
pinMode(Push_Button, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(Push_Button) == LOW) // Is the button being pressed?
myStepper.step(Forward_Rev); // Move the motor forward.
if (digitalRead(Limit_Switch) == LOW) // Is the limit switch pressed.
myStepper.step(Backward_Rev); // Move the motor backwards.
}
Remember to re-wire your switches before you try this out.