Stepper motor and lights at the same time

Hi

I`m having difficulities with running two functions at the same time. I have tried to figure it out, but the code is just doing the first function and then the secound one.

The motor should run, and the light should be on at the same time, and they should start at the same time.
Thanks in advance!

int menubutton = 7;
int menu_item =0;
int button = 8;

long lightsInterval = 1000;  
long motorInterval = lightsInterval * 3 / 4; 
long motorCounter = 0;  
long lightsCounter = 0;

int motorState = LOW; 
int lightsState = LOW; 

void motorpina();        //THE TWO FUNCTIONS
void lightspina();      //THE TWO FUNCTIONS


void setup()
{
 Serial.begin(9600);
  
//FOR buttons
  pinMode(button, INPUT);
  pinMode(menubutton, INPUT);

  lcd.begin(16, 2);
  lcd.print("Welcome to");
  lcd.setCursor(0, 1);
  lcd.print("the DRINKMIXER!");
}

void loop()
{

  if (digitalRead(menubutton) == HIGH)
  {


    if (menu_item >= 3)
      menu_item = 0;
    else
      menu_item++;
      
    lcd.setCursor(0, 1);

    switch (menu_item)
    {

      case 0:                                 //Meny PinaColada
        {
          lcd.begin(16, 2);
          lcd.print("Drink");
          lcd.setCursor(0, 1);
          lcd.print("Pina Colada");

        }
        break;
    }
  }


  if (menu_item == 0 && digitalRead(button) == HIGH)        //Making Pina COlada
  {
      lcd.begin(16, 2);
      lcd.print("Making");
      lcd.setCursor(0, 1);
      lcd.print("Pina Colada");

      motorpina();    //THIS IS THE FUNCTIONS THAT SHOULD RUN AT THE SAME TIME
      lightspina();   //THIS IS THE FUNCTIONS THAT SHOULD RUN AT THE SAME TIME

      lcd.clear();
      lcd.begin(16, 2);
      lcd.print("Your Pina Colada");
      lcd.setCursor(0, 1);
      lcd.print("is ready");
      

  }
}




void motorpina() 
{
 if(millis() - motorCounter > motorInterval) // if it has been more than 750 ms since the blueState has changed, change it. 
 {
   motorCounter = millis();  
   Serial.print("motorCounter = ");
   Serial.println(motorCounter);
   if(motorState == LOW)
   {
      myStepper.step(700);
      delay(500);
      myservo.write(180);
      delay(2000);
      myservo.write(0);
      delay(1000);
      myStepper.step(-1100);
      delay(500);
      myservo.write(180); 
      delay(2000);
      myservo.write(0);
       delay(1000);
   }
     else
   {
     motorState = LOW;   
   }
   
 }
}



void lightspina() 
{
int g =0;
  
 if(millis() - lightsCounter > lightsInterval)
 {
   lightsCounter = millis();
   Serial.print("lightsCounter = ");
   Serial.println(lightsCounter);
   if(lightsState == LOW)
   {
 // fade from blue to violet
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 

  // fade from violet to red
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 

 // fade from blue to violet
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
    
   }
   else
   {
     lightsState = LOW;
   }
 }
}

You could use multitasking library or follow this link

This is adafruit example. You could use this.

https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing

that is only run more than one task at a time. that is also based on timer running back side to run the more tasks.

@arduinofan90, after 32 posts you should know to use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

Have a look at the demo Several Things at a Time

…R

Sorry, I thought I did that. I`ll try looking at the links you left in your post.

arduinofan90: Sorry, I thought I did that.

It is still not too late to modify your Original Post and use the code button.

...R

Hi arduinofan90!

Arduino has several timers. You could use one of them to run motor in background. In this case you have to use interrupt handler function to generate stepper motor pulses.
In this example you have to calculate timer Ticks for desired motor speed.

e.g.
16MHz - CPU clock, 8 - timer prescaler, 200 - motor does 200 full steps per turn, 2 - two pulses per step (high and low)
16000000 / 8 / 200 / 2 = 5000.
Set timer 5000 makes motor run at 1 tern per second. 500 - 10 turns per second ( the less interval between pulses the faster motor turns).

bool MotorPulse = false;
int MotorSpeed = 0;

void initTimer()
{
  cli();                         //disable interrupts
  TCCR1A = 0;  
  TCCR1B = 0;            // timer stopped
  TCNT1  = 0;
  OCR1A = 0;
  TCCR1B |= (1 << WGM12);       // set CTC mode to compare with OCR1A
  TCCR1B |= (1 << CS11);           // choose prescaler
  TIMSK1 |= (1 << OCIE1A);        // enable TIMER1 interrupt
  sei();                        // enable interrupts
}

void SetTimer(unsigned int Ticks) 
{
  cli();
  TCNT1  = 0;
  if (Ticks==0) 
     TIMSK1 &= ~(1 << OCIE1A); // disable interrupt
  else 
     TIMSK1 |= (1 << OCIE1A);    // enable interrupt
  OCR1A = Ticks;
  sei();
}

ISR(TIMER1_COMPA_vect)
{
  if(MotorSpeed==0) digitalWrite(pinStep, LOW);
  else {
    MotorPulse=!MotorPulse;
    digitalWrite(pinStep, MotorPulse ? HIGH : LOW);
  }
}

...

initTimer();
unsigned int  Ticks = some formulae based on MotorSpeed;
SetTimer(Ticks); // now motor started turning in background
...