Can't get my merged sketch to work properly

Okay, I know I’m doing something wrong. Just can’t seem to pinpoint it.

What I am trying to do is this. Run a stepper motor that descends, pauses, and raises up again (I know how to control the length, and will adjust that later; I want it set where I have it for testing purposes,) and at the same time, have 4 LED’s chase each other forward and reverse.

I have compiled both sketches individually, and they work flawlessly. However, when I merge the two, I get this result. Motor begins right away as it should, lowers, pauses, raises, pauses, lowers again (Like it should as a loop) However, at the next raising, a single LED (Pin 7) turns on and stays on. Motor runs another cycle or two, and then everything stops.

Eventually here’s where I need to get to. A push button activates the Arduino sequence. Lights begin chasing as motor lowers object from above. Lights continuously chase, motor stops at bottom point. Lights then turn off as motor raises up to reset point, and program resets awaiting another push from initiation button. I’m a long way from that.

I don’t want to waste anyone’s time, but here are the three sketches…and if you could tell me how to do what I eventually want to do, that’d be awesome…But I am trying to learn it on my own. I’ve ordered the Arduino book off amazon, but it’s not here yet.

byte ledPin[] = {10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10}; // Create array for LED pins
int ledDelay = 200; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
for (int x=0; x<10; x++)
{
// set all pins to output
pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();
}
void loop() {
if ((millis() - changeTime) > ledDelay)
{
// if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}
void changeLED()
 {
for (int x=0; x<10; x++)
{
// turn off all LED's
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
// change direction if we reach the end
if (currentLED == 9) {direction = -1;}
if (currentLED == 6) {direction = 1;}
}

Motor Part

/*
 * MotorKnob
 *
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 *
 * http://www.arduino.cc/en/Reference/Stepper
 * This example code is in the public domain.
 */

// Pin 2 IN 1
// Pin 3 IN 2
// Pin 4 IN 3
// Pin 5 IN 4
// 5V to battery, ground to Gnd on Arduino



#include "Stepper.h";

// change this to the number of steps on your motor
#define STEPS 48


// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to


// Define Constants
// Steps per Rev
const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;

//Number of steps per geared rotation

// the previous reading from the analog input
int previous = 0;
int StepsRequired;
Stepper stepper(STEPS, 2, 4, 3, 5);


void setup() {
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(700);
}

void loop() {
  // get the sensor value
  int val = analogRead(0);

  // move a number of steps equal to the change in the
  // sensor reading
  stepper.step(10250);
  delay(2000);

  stepper.step(-10250);
  delay(10000);

  // remember the previous value of the sensor
  previous = val;
}

Both together and messed with, but never had any error codes

/*
 * MotorKnob
 *
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 *
 * http://www.arduino.cc/en/Reference/Stepper
 * This example code is in the public domain.
 */
// MOTOR
// Pin 2 IN 1
// Pin 3 IN 2
// Pin 4 IN 3
// Pin 5 IN 4
// 5V to battery, ground to Gnd on Arduino



#include "Stepper.h";

// change this to the number of steps on your motor
#define STEPS 48


// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to


// Define Constants
// Steps per Rev
const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;

//Number of steps per geared rotation

// the previous reading from the analog input
int previous = 0;
int StepsRequired;
Stepper stepper(STEPS, 2, 4, 3, 5);


//LIGHTS

byte ledPin[] = {11, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11}; // Create array for LED pins
int ledDelay = 200; // delay between changes
int direction = 11;
int currentLED = 6;
unsigned long changeTime;

void setup() 
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(700);


for (int x=06; x<12; x++)
{
// set all pins to output
pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();
}

void loop() {
  // get the sensor value
  int val = analogRead(0);

  // move a number of steps equal to the change in the
  // sensor reading
  stepper.step(10250);
  delay(2000);

  stepper.step(-10250);
  delay(10000);

  // remember the previous value of the sensor
  previous = val;
  
if ((millis() - changeTime) > ledDelay)
{
// if it has been ledDelay ms since last change 
changeLED();
changeTime = millis();
}
}

void changeLED()
 {
for (int x=6; x<11; x++)
{
 // turn off all LED's;
digitalWrite(ledPin[11, 10, 9, 8, 7, 6], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
// change direction if we reach the end
if (currentLED == 11) {direction = -1;}
if (currentLED == 6) {direction = 1;}
}

In a combined sketch any use of delay applies to both sketches. Therefore if it is important that the two sketches appear to do the functions at the same time you must write them as a state machine, like the blink without delay example.

You need to make your motor sketch work with millis() for timing instead of delay(). Then you have a chance of combining those versions and getting it working. Mixing long delay()s and millis() will rarely if ever do anything useful.

Steve

ok, getting me on the right path Thanks. I basically used the premade sketches so I didn’t realize they would contradict themselves.

You may assume that anything using "delay()" is just for showing pretty lights (and even then, in the crudest possible fashion) and not intended for serious use.

Paul__B:
You may assume that anything using "delay()" is just for showing pretty lights (and even then, in the crudest possible fashion) and not intended for serious use.

Not really true, I have a quite a few working projects that use delay() exclusively for timing.

However it is reasonable to assume that anything using delay() is probably doing a SINGLE clearly defined task. It's when you want multiple interacting tasks that delay() becomes a serious hindrance.

Steve