Help with Do While

Actually... they don't really know what to use.

So encouraging them down this path with poor examples, when it is completely unneccesary as in is this case, is not a great way for them to learn.

Teaching/Learning pedagogy is truly a difficult task when one faces 4x50 undergraduate pupils and observes that each individual bears unique way of receiving lesson/lecture. Finally, the tutor devises his own way of delivery what he thinks simplest and will cover most of the slow learners. AE says that a wise man starts from the simple one and then gradually moves to the complex one.

OP (probably) has thought a process like this: keep waiting/checking until reading_Button/reading_Switch is activated which prompted him to think about do-while from his own guess as @anon57585045 has correctly conceived (post #5) and not from C Language perspective. From linguistic point of view if-else is a difficult structure that is not immediately taken for practice for the language development of the impaired children. Here in this thread, it is observed that the OP has reached intuitively to the do-while structure instead of if-else.

True, and the linguistic do-while doesn't have the strong implication of testing the condition at the end, it may be ambiguous, e.g. "I will do the dishes while it is raining".

True indeed! In case of do-while, the control statement always executes the sub-ordinate clause at least for once before testing the conditional expression. So, the preference should be on while-do which checks the condition first for true and then performs the task. For example: "while it has been raining, stay inside."

Hey Tom,

I cant find an exact spec sheet for this motor, but its a 23KM-K346-G1V that I pulled from an old industrial office type printer.

As for the A0 and the resistor, as I mentioned, I copied and pasted a large part of the code and stole some of the wiring from other google sources. The resistor is not the 4k7 listed, its a 10kohm.

For the loop in the set up, are you referring to having a defined functional void? I'm not sure I am 100% following.

Hey!

Thanks for the help! The code you have here runs almost nearly as it should although I had to change the digital read in the loop to HIGH instead of LOW. The motor moves forward when I press the button and runs in reverse when I hit the switch. What it's not doing is interrupting the forward revolution when the limit switch is depressed and going to the reverse revolution which is where I was getting hung up. I had the entire system to this point previously which is why I though of trying the do/while, goto etc.

Thanks again for your assistance.

Did you do this bit?

A minor change is required to the if statement to achieve this...

  if (digitalRead(Push_Button)  == LOW && // Is the button being pressed AND
      digitalRead(Limit_Switch) == HIGH)  // the limit switch is NOT pressed.
    myStepper.step(Forward_Rev);          // Move the motor forward.
1 Like

@bobbybonez2

I estimate that you invested 30 hours in this project to come as far as you are now.

My estimation is If you would have taken 20 hours to learn the programming basics
the developping time for your code would go down to 3-5 hours = faster finishing your project.

best regards Stefan

This is working as it should! Thank you very much. For reference this is the code I've got. Now on to making the servo work with the push button!

#include <Stepper.h>
//#include <Servo.h>


int Push_Button  = 2;
//int servoPin = 3; 
int Limit_Switch = 13;

// Create a servo object 
//Servo Servo1;

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()
{
  // We need to attach the servo to the used pin number 
  //pinMode(servoPin, OUTPUT); 
  myStepper.setSpeed(60);

  pinMode(Limit_Switch, INPUT_PULLUP);
  pinMode(Push_Button,  INPUT_PULLUP);
}

void loop()
{
  // Make servo go to 0 degrees 
  //Servo1.write(0);
  if (digitalRead(Push_Button)  == HIGH)    // Is the button being pressed
      myStepper.step(Forward_Rev);          // Move the motor forward.
    //Make servo go to 90 degrees 
    //Servo1.write(90); 
    //delay(1000);
    //Servo1.write(0);

  if (digitalRead(Push_Button) == LOW &&// Is the limit switch pressed.
      digitalRead(Limit_Switch) == HIGH)
      myStepper.step(Backward_Rev);         // Move the motor backwards.
}

Glad it's working... but your code doesn't look right ... if you are using INPUT_PULLUP then you should be checking for active when LOW. How have you got the button/switch connected? They should be one side to the pin, one side to GND, and you don't need the external resistors anymore.

Does this look better? I've gotten rid of both of the resisters for the switch and button and its working as it should. Although I think I've got a couple of bad servos. Even with it set to go to specific angels, it just spins around in circles and doesn't do what its supposed to do. Also the delay is messing with the ability for the limit switch to run when it's supposed to.

#include <Stepper.h>
#include <Servo.h>


int Push_Button  = 2;
int Limit_Switch = 13;

// Create a servo object 
Servo MyServo;

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);
  MyServo.attach(3); // We need to attach the servo to the used pin number 
  

  pinMode(Limit_Switch, INPUT_PULLUP);
  pinMode(Push_Button,  INPUT_PULLUP);
}

void loop()
{
  //MyServo.write(0);
  
  if (digitalRead(Push_Button)  == LOW)    // Is the button being pressed
      myStepper.step(Forward_Rev);          // Move the motor forward.
      
  //if (digitalRead(Push_Button) == LOW)
      //Make servo go to 90 degrees 
      //MyServo.write(90); 
      //delay(3000);
      //MyServo.write(0);
      
  if (digitalRead(Push_Button) == HIGH &&// Is the limit switch pressed.
      digitalRead(Limit_Switch) == LOW)
      myStepper.step(Backward_Rev);         // Move the motor backwards.
      
  
}

Better. I think your logic is still not quite right... if the limit switch is pressed then I thought you didn't want to bother reading the push button. Look at the logic in post #48 vs your latest version.

Yeah I don't know. If i reverse the low and high like had suggested, the button and switch work in reverse of how they should. Anyway everything is working as it should wit the exception of the servo now :person_shrugging:

Thanks again!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.