Combining two working sketches

Discussion in 'Programmer's Corner' started by cmjb13, Oct 17, 2016.

  1. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    I have 2 separate sketches

    The first sketch has a push button that allows me to toggle the Arudino LED light on and off

    Code (Text):
    1.  
    2. //Button test
    3.  
    4. int button = 3; //button pin, touch to ground as button
    5. int press = 0;
    6. boolean toggle = true;
    7.  
    8. void setup()
    9. {
    10.   pinMode(13, OUTPUT); //LED on pin 13
    11.   pinMode(button, INPUT); //arduino monitor pin state
    12.   digitalWrite(5, HIGH); //enable pullups to make pin 5 high
    13. }
    14.  
    15. void loop()
    16. {
    17.   press = digitalRead(button);
    18.   if (press == LOW)
    19.   {
    20.     if(toggle)
    21.     {
    22.       digitalWrite(13, HIGH);   // set the LED on
    23.       toggle = !toggle;
    24.     }
    25.     else
    26.     {
    27.       digitalWrite(13, LOW);    // set the LED off
    28.       toggle = !toggle;
    29.     }
    30.   }
    31.   delay(300);  //delay for debounce
    32. }
    33.  
    Second code allows for a stepper motor to spin

    Code (Text):
    1.  
    2. /*
    3.  Stepper Motor Control - one revolution
    4.  
    5.  This program drives a unipolar or bipolar stepper motor.
    6.  The motor is attached to digital pins 8 - 11 of the Arduino.
    7.  
    8.  The motor should revolve one revolution in one direction, then
    9.  one revolution in the other direction.
    10.  
    11.  
    12.  Created 11 Mar. 2007
    13.  Modified 30 Nov. 2009
    14.  by Tom Igoe
    15.  
    16.  */
    17.  
    18. #include <Stepper.h>
    19. int x = 0;
    20. const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    21. // for your motor
    22.  
    23. // initialize the stepper library on pins 8 through 11:
    24. Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
    25.  
    26. void setup() {
    27.   // set the speed at 60 rpm:
    28.   myStepper.setSpeed(63);
    29.   // initialize the serial port:
    30.   Serial.begin(9600);
    31. }
    32.  
    33. void loop() {
    34.  if (x <= 10)
    35.  { // step one revolution in one direction:
    36.   Serial.println("clockwise");
    37.   myStepper.step(stepsPerRevolution);
    38.   // delay(500);
    39.  x++;
    40.  }
    41.  
    42.   // step one revolution in the other direction:
    43.   // Serial.println("counterclockwise");
    44.  // myStepper.step(-stepsPerRevolution);
    45.  // delay(500);
    46. }
    47.  
    I've been trying to combine codes in order to allow the push button to be pushed, the stepper motor spins/runs through the program and then requires the push button to be pushed again for the process to repeat. I've tried different methods of combining the code with no luck so far. It may be very simple as I'm just not seeing it. Push button diagram attached.

    Thank you in advance for any help.
     
  2. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    Getting very close. Have a semi combined working sketch, with a hiccup...

    If I have button state = LOW, I have to press and hold the button in order for it to keep spinning and release it for the spinning to stop.
    If I have button state = HIGH, the motor spins right away, pressing the button stops the spinning, but resumes, unless I hold the button down.
    What am I missing in the sketch to where I can press the button once (release/not hold), and it will spin X number times?


    Code (Text):
    1.  
    2. #include <Stepper.h>
    3. int x = 0;
    4. //define our variables
    5. const int buttonPin = 3;  // the number of the pushbutton pin
    6. const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    7. int buttonState = 0;  // variable for reading the pushbutton status
    8. Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins
    9.  
    10. void setup() { //this code will only run once
    11.   pinMode(3, INPUT); // initialize the pushbutton pin as an input:
    12.   myStepper.setSpeed(60); //set stepper speed
    13.   Serial.begin(4800);  // initialize the SLOW serial port
    14. } //end of setup function
    15.  
    16. void loop() {
    17.   buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
    18.   if (buttonState == LOW) { //if button is pressed
    19.       steppermotorfunction();// run the stepper motor function:
    20.   }
    21.   else
    22.   {
    23.   delay(300);//do nothing waiting for button press
    24.   }
    25. } //end of loop function
    26.  
    27. void steppermotorfunction() {
    28.   //This code runs as soon as the button is pressed
    29.    if (x <=20)
    30.     Serial.println("clockwise"); //print that we are running clockwise
    31.     Serial.print("Turn #");
    32.     myStepper.step(stepsPerRevolution);
    33.     //delay(500);
    34. } //end of steppermotorfunction
    35.  
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    You need to step back and use a "top-down" design approach, a basic concept used in Structured Programming.

    Every program for an embedded application will have a simple and straight forward structure consisting of two parts - (1) Initialization and (2) control loop.
    Code (Text):
    1. void main()
    2. {
    3.    Initialization();  // as per your setup
    4.    while(1) // endless loop
    5.     {
    6.       // main loop
    7.       loop();  // as per your loop
    8.     }
    9.  
    10. }
    Initialization is the same as what you have called setup( ).
    Within set up you will initialize a variable such as MotorEnable.
    Code (Text):
    1. void setup()
    2. {
    3.    // your code here
    4.   MotorEnable = 0;
    5. }
    Using a loop( ) function is ok. However, in the interest of execution efficiency you can eliminate one function call/return by unrolling the loop( ) code directly in the while(1){ } code.
    Code (Text):
    1. while(1)
    2. {
    3.   while(MotorEnable)
    4.       StepMotor();
    5. }
    MotorEnable should be toggled in a button sense interrupt routine. This would be set up to respond on a rising or falling edge but not both, depending on how your button is wired, i.e. active-low or active-high.

    Since your code is locked out during the StepMotor( ) function, you do not have to use interrupts.
    Code (Text):
    1. while(1)
    2. {
    3.   ButtonSense();
    4.   while(MotorEnable)
    5.       StepMotor();
    6. }
    Within ButtonSense( ), toggle MotorEnable, i.e. you have to code for button change, not simply button status.
     
  4. dannyf

    Well-Known Member

    Sep 13, 2015
    1,767
    357
    Change the setup in the first sketch to setup1 and loop to loop1, ..
    And your new setup with contain setup1 and setup2, and your new loop will contain loop1 and loop2.

    Done.
     
  5. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    Wouldn't this be the key line to change as no matter what other code I have, this number is fixed?
     
Loading...