Combining two working sketches

Thread Starter

cmjb13

Joined Mar 18, 2015
35
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:
//Button test

int button = 3; //button pin, touch to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(300);  //delay for debounce
}
Second code allows for a stepper motor to spin

Code:
/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.


 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>
int x = 0;
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(63);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
 if (x <= 10)
 { // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  // delay(500);
 x++;
 }
 
  // step one revolution in the other direction:
  // Serial.println("counterclockwise");
 // myStepper.step(-stepsPerRevolution);
 // delay(500);
}
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.
 

Attachments

Thread Starter

cmjb13

Joined Mar 18, 2015
35
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:
#include <Stepper.h>
int x = 0;
//define our variables
const int buttonPin = 3;  // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int buttonState = 0;  // variable for reading the pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins

void setup() { //this code will only run once
  pinMode(3, INPUT); // initialize the pushbutton pin as an input:
  myStepper.setSpeed(60); //set stepper speed
  Serial.begin(4800);  // initialize the SLOW serial port
} //end of setup function

void loop() {
  buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
  if (buttonState == LOW) { //if button is pressed
      steppermotorfunction();// run the stepper motor function:
  }
  else
  {
  delay(300);//do nothing waiting for button press
  }
} //end of loop function

void steppermotorfunction() {
  //This code runs as soon as the button is pressed
   if (x <=20)
    Serial.println("clockwise"); //print that we are running clockwise
    Serial.print("Turn #");
    myStepper.step(stepsPerRevolution);
    //delay(500);
} //end of steppermotorfunction
 

MrChips

Joined Oct 2, 2009
30,802
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:
void main()
{
   Initialization();  // as per your setup
   while(1) // endless loop
    {
      // main loop
      loop();  // as per your loop
    }

}
Initialization is the same as what you have called setup( ).
Within set up you will initialize a variable such as MotorEnable.
Code:
void setup()
{
   // your code here
  MotorEnable = 0;
}
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:
while(1)
{
  while(MotorEnable)
      StepMotor();
}
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:
while(1)
{
  ButtonSense();
  while(MotorEnable)
      StepMotor();
}
Within ButtonSense( ), toggle MotorEnable, i.e. you have to code for button change, not simply button status.
 

dannyf

Joined Sep 13, 2015
2,197
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.
 

Thread Starter

cmjb13

Joined Mar 18, 2015
35
Wouldn't this be the key line to change as no matter what other code I have, this number is fixed?
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
 
Top