Combining two codes

Hi

I have a arduino nano and I try to get two codes to run on it independently.

The first code looks like this:

// constants won't change. They're used here to
// set pin numbers:
const byte stepUpPin = 5;     // the number of the step pin
const byte stepDownPin = 4;     // the number of the step pin
const byte stepOut = 2;
const byte directionOut = 3;
const byte ratePin = 0; // A0 - analog 0 pin on Arduino to control the stepping dealy (i.e. RPMs)
const byte enablePin = 6; // turn EasyDriver off when not turning (saves power but ONLY use when there's no need to hold position)

// Variables will change:
byte lastStepUpButtonState = HIGH;   // the previous reading from the step UP pin
byte lastStepDownButtonState = HIGH;   // the previous reading from the step DOWN pin
byte directionState=HIGH;             // the current direction
byte stepUpState=HIGH;             // the current state of UP button
byte stepDownState=HIGH;             // the current state of DOWN button
int stepsPassed = 0; //how many steps?
int stepsPassedMax = 8; // trying to calculate steps per revolution (remember the microstepping settings 1/8th is default)
// most small and micro steppers, especially those that came from a CD- or DVD-R/RW drives
// have 20 steps per revolution. So 160 mircosteps should make the motor spin 360 degrees once.

long lastStepUpDebounceTime = 0;  // the last time the output pin was toggled
long lastStepDownDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time in ms; increase if the output flickers

long stepDelayBase = 1; // 1ms base rate. Multiply by the reading from the RATE potentiometer for actual step delay
long stepDelay; //
long lastStepTime = 0; // last time the step signal was sent

void setup() {
  pinMode(stepUpPin, INPUT);
  pinMode(stepDownPin, INPUT);
  pinMode(ratePin, INPUT);
  pinMode(stepOut, OUTPUT);
  pinMode(directionOut, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop() {

// read the state of the switch into a local variable:
int readingStepUp = digitalRead(stepUpPin);
int readingStepDown = digitalRead(stepDownPin);
stepDelay = analogRead(ratePin) * stepDelayBase/100;
if(stepDelay < 1) stepDelay = 1; // reality check - a pot can read 0 and then it would mean infinite RMP - not possible

if(readingStepUp == LOW || readingStepDown == LOW) { // only read buttons if either one of them is LOW

  // If the switch changed, due to noise or pressing:
  if (readingStepUp != lastStepUpButtonState) {
    // reset the debouncing timer
    lastStepUpDebounceTime = millis();
    lastStepUpButtonState = readingStepUp;
  }

  if ((millis() - lastStepUpDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    lastStepUpButtonState = readingStepUp;
    lastStepUpDebounceTime = millis();
    stepUpState = readingStepUp;  }

  // If the switch changed, due to noise or pressing:
  if (readingStepDown != lastStepDownButtonState) {
    // reset the debouncing timer
    lastStepDownDebounceTime = millis();
    lastStepDownButtonState = readingStepDown;
  }

  if ((millis() - lastStepDownDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    lastStepDownButtonState = readingStepDown;
    lastStepDownDebounceTime = millis();
    stepDownState = readingStepDown;
  }

}else{
  stepUpState = HIGH;
  stepDownState = HIGH;

} // end of if block that reads button states

if(stepsPassed == 0 ) { // if the previous command has completed, make the direction decision
    if(stepUpState == LOW || stepDownState == LOW) {
      if(stepUpState == LOW && stepDownState == LOW) { directionState = LOW; } // why are you holding both UP and DOWN buttons?
      if(stepUpState == LOW && stepDownState == HIGH) { directionState = HIGH;} // go up
      if(stepUpState == HIGH && stepDownState == LOW) { directionState = LOW;} // go down
      stepsPassed = stepsPassedMax+1;
    }
  }

if(stepsPassed > 0 ) // send step signals now
{
  digitalWrite(enablePin, LOW); // wake up
  digitalWrite(directionOut, directionState);
  if((millis() - lastStepTime) > stepDelay) // delay expired, send another step
    {
      digitalWrite(stepOut, HIGH);
      stepsPassed--;
      lastStepTime = millis();
    }else
    {
      // time for one step not yet expired, hold the STEP signal low
      digitalWrite(stepOut, LOW);
    }
}else{
  digitalWrite(enablePin, HIGH); // go to sleep

}
}

… and the other one looks like this:

#include <Tone.h>

/* Timer reload value, globally available */
unsigned int tcnt2;

/* Toggle HIGH or LOW digital write */
int toggle1 = 0;
int toggle2 = 0;

/* Keep track of when each note needs to be switched */
int count1 = 0;
int count2 = 0;

/* Frequency Output Pins */
#define FREQ1 9
#define FREQ2 10

//Setup Function will run once at initialization
void setup()
{

/* First disable the timer overflow interrupt*/
TIMSK2 &= ~(1<<TOIE2);

/* Configure timer2 in normal mode (no PWM) */
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);

/* Select clock source: internal I/O clock */
ASSR &= ~(1<<AS2);

/* Disable Compare Match A interrupt (only overflow) */
TIMSK2 &= ~(1<<OCIE2A);

/* Configure the prescaler to CPU clock divided by 128 */
TCCR2B |= (1<<CS22)  | (1<<CS20); // Set bits
TCCR2B &= ~(1<<CS21);             // Clear bit

/* We need to calculate a proper value to load the counter.
* The following loads the value 248 into the Timer 2 counter
* The math behind this is:
* (Desired period) = 64us.
* (CPU frequency) / (prescaler value) = 125000 Hz -> 8us.
* (desired period) / 8us = 8.
* MAX(uint8) - 8 = 248;
*/
/* Save value globally for later reload in ISR */
tcnt2 = 248;

/* Finally load and enable the timer */
TCNT2 = tcnt2;
TIMSK2 |= (1<<TOIE2);

//Configure I/O Pin Directions
pinMode(FREQ1,    OUTPUT);
pinMode(FREQ2,    OUTPUT);

}

/* Install the Interrupt Service Routine (ISR) for Timer2.  */
ISR(TIMER2_OVF_vect) {
/* Reload the timer */
TCNT2 = tcnt2;

count1++; count2++;

if (count1 == 16)
{
digitalWrite(FREQ1, toggle1 == 0 ? HIGH : LOW);
toggle1 = ~toggle1;
count1 = 0;
}
if (count2 == 14)
{
digitalWrite(FREQ2, toggle2 == 0 ? HIGH : LOW);
toggle2 = ~toggle2;
count2 = 0;
}
}

void loop()
{
}

Is it possible to have these two codes to run on the same nano simultaneously and independent from eachother? I’m not very good at programming.

Thanks.

You can only have one setup() and loop().

Why can't you run them in the same loop()?

Weedpharma

I Think I understand what you mean. If I understand correctly, the codes don't need to be placed right after each other or have to be intact? Can I place everything that is in void setup() {} in one code in the same void setup() {} as the other code?

I removed the last void loop() {} and tacked all code under void setup() {} together

One of the codes work now and I guess the other one should also work, just that I can't test it right now as I need the stepper motor that the code Controls. I don't have it right now so have to test it tomorrow.

Thanks for feedback on this.

This demo shows a method of merging programs.

...R

This walks you through what you need to do. http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html