Combining script problem

Hi All,

I have two scripts working fine:

  • The first is a rotary encoder linked to a stepper motor, turning the encoder, the motor follows.
  • The second is a push button which when pressed and released allows three outputs to sequence in a particular pattern.

The home project is to control a variable capacitor remotely via a motor, the concept is course tune the capacitor using the stepper on full speed, fine tuning will be done by pressing the button (rotary encode switch), thus switching the outputs (HIGH), these will be linked to the Micro-Stepper inputs of the stepper driver, reducing steps from Full, to Half, Quarter, Eighth and Sixteenth.

I’m a novice and have tried to copy into the script each other, but I have never managed to make it compile and I’m stuck.

The code for the motor control is:

// Stepper motor speed control using rotary encoder and A4988 driver

#include <AccelStepper.h>

int motorSpeed = 11000; //maximum steps per second (about 5rps / at 8 microsteps)
int motorAccel = 3600; //steps/second/second to accelerate

const unsigned char ttable[7][4] = {
{0x0, 0x2, 0x4, 0x0}, {0x3, 0x0, 0x1, 0x10},
{0x3, 0x2, 0x0, 0x0}, {0x3, 0x2, 0x1, 0x0},
{0x6, 0x0, 0x4, 0x0}, {0x6, 0x5, 0x0, 0x20},
{0x6, 0x5, 0x4, 0x0},

#define DT 2
#define CLK 3
#define DIR_CCW 0x10
#define DIR_CW 0x20
int motorStepPin = 8; //digital pin 3
int motorDirPin = 9; //digital pin 2
const int Enable_PIN = 10;
AccelStepper stepper(1, motorStepPin, motorDirPin);
volatile unsigned char state = 0;

void setup(){

  pinMode(Enable_PIN, OUTPUT);
  digitalWrite(Enable_PIN, LOW);
  stepper.moveTo(8000); //move 32000 steps (should be 10 rev)
  pinMode(DT, INPUT);
  pinMode(CLK, INPUT);

void loop(){
 int counter;
 unsigned char result;
  /* Reset the counter */
 counter = 0;

    /* Read the status of the dial */
    unsigned char pinstate = (digitalRead(CLK) << 1) | digitalRead(DT);
    state = ttable[state & 0xf][pinstate];
    if(result==DIR_CCW) counter++;
        if(result==DIR_CW) counter--;
 // put some magnification  hera about 10 times

The code for the pushbutton control of outputs to control steps:

// // [Project 7] - RGB LED Control With Button
// Sketch used to control the step size of a stepper motor via a A4988 by applying a HIGH to Pins MS1, MS2 & MS3
// MS1 = L, MS2 = L, MS3 = L - Full Steps
// MS1 = H, MS2 = L, MS3 = L - Half Steps
// MS1 = L, MS2 = H, MS3 = L - Quarter Steps
// MS1 = H, MS2 = H, MS3 = L - Eighth Steps
// MS1 = H, MS2 = H, MS3 = H - Sixteenth Steps
// RED test LED Pin 11 = MS1 on A4988
// GREEN test LED Pin 12= MS2 on A4988
// YELLOW test LED Pin 13= MS3 on A4988

const int MS3=13;
const int MS2=12;
const int MS1=11;
const int button=7;
boolean beforeCase=LOW;
boolean nowCase=LOW;
int ledMode=0;

void setup()

{ pinMode(MS3,OUTPUT);
pinMode(button,INPUT); }

boolean buttonControl(boolean now)
{ boolean presentBtn = digitalRead(button); 
{ delay(5); presentBtn=digitalRead(button);
} return presentBtn;


void rgbSet(int colour)

{ if(colour==1)
{ digitalWrite(MS1,LOW); 
else if(colour==2) 
{ digitalWrite(MS1,HIGH);
else if(colour==3) 
{ digitalWrite(MS1,LOW);
else if(colour==4) 
{ digitalWrite(MS1,HIGH);
else if(colour==5) 
{ digitalWrite(MS1,HIGH);

void loop() 
{ nowCase=buttonControl(beforeCase);
if(beforeCase==HIGH && nowCase==LOW)
{ ledMode++; } beforeCase=nowCase;
if(ledMode==6) ledMode=1; rgbSet(ledMode);

I have tried copying everything before ‘void setup()’ on one script to the other and that’s ok, it’s when I copy the script beneath the ‘void setup()’ that I have my first problem.

Can anyone help me out please.



The first sketch has a "while(1)", you have to remove that. If you want the variables "counter" and "result" to keep their value, make them global variables or make them static.

  static int sun = 2;
  int moon;


In this example, the sun start at 2, and is incremented every time the loop() runs. The sun is static and keeps its value. The moon has no initial value, but it usually zero. Every time the loop() runs again, the moon is set to zero.

The second sketch uses delay(5). You can either replace that with a millis() software timing, or using a library like Bounce2 :

Could you give it a try and show your combined sketch ?

Every time the loop() runs again, the moon is set to zero

sp. "Every time the loop() runs again, the moon assumes some random value"

Thanks guys for your helpful replies, I have a next to zero knowledge base on this, so my reply back to forum may take me a while.