Stepper & IR acting funny

Hello guys,

I've made several codes seperatly and now trying to combine them. The LCD I can manage, this works 'combined'. But now when I add the stepper to the IRremote/LCD code it's acting strange.

It seems as if my IR is recieving 'another button' while it's at actionstate0 executing a stepper command.
Could there be a conflit in my libraries or something?

Wiring etc is good. It works with the same pin locations when I run the seperate codes to test.

Code pasted below, feel free to have a look.
Work in progress so a bit messy perhaps...

Thank you
Maho

// Load libraries 
#include "IRremote.h"
#include <LiquidCrystal.h>
#include <Stepper.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);



// Integers used
const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
const int rolePerMinute = 17;   
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);// Adjustable range of 28BYJ-48 stepper is 0~17 rpm
int receiver = 6;
int ActionState = 99;
int MSeconds = 0;
unsigned long SECstartMillis;
unsigned long SECcurrentMillis;
unsigned long RFRSHstartMillis;
unsigned long RFRSHcurrentMillis;
int Seconds = 0;
int RFRSH = 0;
int StepperPos = 0;
int MenuState = 0;
int RandomNr = 0;
int Menu = 0;
int TarePos = 0;
int Setting = 0;
String Line1 = "Line 1";
String Line2 = "Line 2";

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

/*-----( Remote Button detection )-----*/
void translateIR()
{
  switch (results.value)
  {
    case 0xFFA25D: Serial.println("POWER"); break;
    
    case 0xFFE21D:  { //Funct.Stop Button
      actionSetup();
    }
    break;
    case 0xFF629D: { //Vol + Button
      actionVolUp();
    }
     break;
    case 0xFFA857: { //Vol - Button
     actionVolDown();
    }
    break;
    case 0xFF22DD: Serial.println("FAST BACK");    break;
    case 0xFF02FD: Serial.println("PAUSE");    break;
    case 0xFFC23D: Serial.println("FAST FORWARD");   break;
    case 0xFFE01F:  { //Arrow down
        actionDown();
      }
      break;
case 0xFF906F:  { //Arrow up
    actionUp();
  }
  break;
case 0xFF9867: Serial.println("EQ");    break;
case 0xFFB04F: Serial.println("ST/REPT");    break;
case 0xFF6897: { //0 button
    action0();
  }
  break;
case 0xFF30CF: { //1 Button
    action1();
  }
  break;
case 0xFF18E7: { //2 Button
    action2();
  }
  break;
case 0xFF7A85: //3 Button
  {
    action3();
  }
  break;
case 0xFF10EF: Serial.println("4");    break;
case 0xFF38C7: Serial.println("5");    break;
case 0xFF5AA5: Serial.println("6");    break;
case 0xFF42BD: Serial.println("7");    break;
case 0xFF4AB5: Serial.println("8");    break;
case 0xFF52AD: Serial.println("9");    break;
case 0xFFFFFFFF: Serial.println(" REPEAT"); break;
default:
  Serial.println(" other button   ");
}// End Case
delay(500); // Do not get immediate repeat
} //END translateIR

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn(); // Start the receiver
  SECstartMillis = millis();
}/*--(end setup)---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

  /* --- Timer start ----
    Increase the 'second' integer by 1 each 1000ms */
  SECcurrentMillis = millis();
  if (SECcurrentMillis - SECstartMillis >= 1000)
  {
    SECstartMillis = SECcurrentMillis;
    Seconds++;
  }
  
  RFRSHcurrentMillis = millis();
  if (RFRSHcurrentMillis - RFRSHstartMillis >= 50)
  {
    RFRSHstartMillis = RFRSHcurrentMillis;
    RFRSH++;
  }
  /* --- Timer end --- */
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  }
  // ### Button 0 -> Zero weight
  if ((ActionState == 0) && (Seconds >= 1))
  {
    Serial.println("Button 0 code executed every 1 second");
    StepperPos = 0;
    myStepper.step(stepsPerRevolution);
    //Servo1.write(StepperPos);
    Seconds = 0;
    //ActionState = 99;
  }
  // ### Button 1 -> Max weight
  if ( (ActionState == 1) && (Seconds >= 1))
  {
    Serial.println("Button 1 code executed every 1 second");
    StepperPos = 180;
    //Servo1.write(StepperPos);
    Seconds = 0;
  }
  /*#### - Button 2 action
    - Random weights
    #######*/
  if ( (ActionState == 2) && (Seconds >= 5))
  {
    Serial.println("Button 2 code executed every 5 seconds");
    RandomNr = random(10, 160);
    StepperPos = RandomNr;
    //Servo1.write(StepperPos);
    Seconds = 0;
  }
  /*#### - Button 3 action
    - Zero
    - Tare
    - Random weight
    - Wait
    - Empty scale
    - Repeat
    #######*/
  if ( (ActionState == 3) && (Seconds >= 2))
  {
    StepperPos = 0;
    //Servo1.write(StepperPos);
    Serial.println("Action 3 Tare + Random");
    ActionState = 33;
    Seconds = 0;
  }
  if ((ActionState == 33) && (Seconds >= 5))
  {
    Serial.println("Tare 15");
    StepperPos = 15;
    //Servo1.write(StepperPos);
    ActionState = 333;
    Seconds = 0;
  }
  if ((ActionState == 333) && (Seconds >= 8))
  {
    Serial.print("load on scale : ");
    RandomNr = random(120, 160);
    Serial.println(RandomNr);
    StepperPos = RandomNr;
    //Servo1.write(StepperPos);
    Seconds = 0;
    ActionState = 3333;
  }
  if ((ActionState == 3333) && (Seconds >= 14))
  {
    Seconds = 0;
    ActionState = 3;
    Serial.println("Empty Scale");
  }
  /* ## End of button 3
  ---- Setup mode menu ---- 
  */
if ((ActionState == 20) && (Seconds >= 1) && (Menu == 0))
{
Seconds = 0;
Serial.println("Menu state at 0");
Serial.print("Setting state at :");
Serial.println(Setting);
}
}
/* --(end main loop )-- */

void action0()
{
  lcd.setCursor(0, 0);
  lcd.print("Set scale Zero");
  lcd.setCursor(0, 1);
  lcd.print("Setting zero...");
  Serial.println("Action 0 activated");
  ActionState = 0;
}

void action1()
{
  Serial.println("Action 1 activated");
  ActionState = 1;
}
void action2()
{
  Serial.println("Action 2 activated");
  ActionState = 2;
}
void action3()
{
  Serial.println("Action 3 activated");
  ActionState = 3;
}
void actionUp()
{
  Serial.println("Button Up");
  Menu++;
}
void actionDown()
{
  Serial.println("Down");
  Menu++;
}
void actionSetup()
{
  ActionState = 20;
  Line1 = "Setup Mode";
  Serial.println(Line1);
}
void actionVolDown()
{
Setting--;
}
void actionVolUp()
{
Setting++;
}

Edit : I'm powering the stepper seperatly using the tiny power board. Ground is shared.
Edit2 : When I run just the stepper code, it works as stated above.

What I notice when I run the 'combined' code :
The mini power board flashes very gently even when powered off. Yet the Arduino has no 5V+ connection going anywhere.

Also, the C&D led's are constant on the stepper board but A&B flash and go off. Instead of keeping on and turning the stepper...

So, no one got a clue directly?

Forum is rather active, seems my thread fell to page two rather quick. I'm giving up for today.

Maho

I find your code difficult to read. Please use the Auto-format tool to indent it consistently and please add a little white-space between sections

An explanation of how the code is intended to work would also be a big help. Comments like " // ### Button 1 -> Max weight" don't mean anything to me.

Also, I don't understand this and I don't have your hardware so I can't try your program

It seems as if my IR is recieving 'another button' while it's at actionstate0 executing a stepper command.
Could there be a conflit in my libraries or something?

What exactly are you seeing that makes you think that?

...R

Hello,

So the big picture is a 'remote control' button is detected and code is executed. I added a few comments and spaces as requested. The goal is to make a control to simulate a scale.

For now, I want to be able to set the stepper to go from 0% to 100% and in between. 0% would be 0 steps, 100% would be for example 5000 steps. So if I want to load my scale to 50% it would go to 2500 steps later on. The stepper is connected to a potentio meter, which is then connected to a weighing terminal.

As you can see on the video I've made, the remote reciever and the stepper motor print is blinking when the code is supposed to be executed and something beeps shortly. No idea whats going on...
Video : video 1550676315 - YouTube

Same hardware works if I run a stepper motor 'example sweep' code.

Thank for your efforts.
Maho

// Load libraries
#include "IRremote.h"
#include <LiquidCrystal.h>
#include <Stepper.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// Integers used
const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
const int rolePerMinute = 17;
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);// Adjustable range of 28BYJ-48 stepper is 0~17 rpm
int receiver = 6;
int ActionState = 99;
int MSeconds = 0;
unsigned long SECstartMillis;
unsigned long SECcurrentMillis;
unsigned long RFRSHstartMillis;
unsigned long RFRSHcurrentMillis;
int Seconds = 0;
int RFRSH = 0;
int StepperPos = 0;
int MenuState = 0;
int RandomNr = 0;
int Menu = 0;
int TarePos = 0;
int Setting = 0;
String Line1 = "Line 1";
String Line2 = "Line 2";

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'



/*-----( Remote Button detection )-----*/
void translateIR()
{
  switch (results.value)
  {
    case 0xFFA25D: Serial.println("POWER"); break;

    case 0xFFE21D:  { //Funct.Stop Button
        actionSetup();
      }
      break;
    case 0xFF629D: { //Vol + Button
        actionVolUp();
      }
      break;
    case 0xFFA857: { //Vol - Button
        actionVolDown();
      }
      break;
    case 0xFF22DD: Serial.println("FAST BACK");    break;
    case 0xFF02FD: Serial.println("PAUSE");    break;
    case 0xFFC23D: Serial.println("FAST FORWARD");   break;
    case 0xFFE01F:  { //Arrow down
        actionDown();
      }
      break;
    case 0xFF906F:  { //Arrow up
        actionUp();
      }
      break;
    case 0xFF9867: Serial.println("EQ");    break;
    case 0xFFB04F: Serial.println("ST/REPT");    break;
    case 0xFF6897: { //0 button
        action0();
      }
      break;
    case 0xFF30CF: { //1 Button
        action1();
      }
      break;
    case 0xFF18E7: { //2 Button
        action2();
      }
      break;
    case 0xFF7A85: //3 Button
      {
        action3();
      }
      break;
    case 0xFF10EF: Serial.println("4");    break;
    case 0xFF38C7: Serial.println("5");    break;
    case 0xFF5AA5: Serial.println("6");    break;
    case 0xFF42BD: Serial.println("7");    break;
    case 0xFF4AB5: Serial.println("8");    break;
    case 0xFF52AD: Serial.println("9");    break;
    case 0xFFFFFFFF: Serial.println(" REPEAT"); break;
    default:
      Serial.println(" other button   ");
  }// End Case
  delay(500); // Do not get immediate repeat
}

//END translateIR
// End of detecting what remote button was pressed and activate its actions, actions can be find below





void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn(); // Start the receiver
  SECstartMillis = millis();
}/*--(end setup)---*/





void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
// Timers to be used instead of delay();
// This is not all used just yet, I'm playing around still. Seconds is used though
  /* --- Timer start ----
    Increase the 'second' integer by 1 each 1000ms */
  SECcurrentMillis = millis();
  if (SECcurrentMillis - SECstartMillis >= 1000)
  {
    SECstartMillis = SECcurrentMillis;
    Seconds++;
  }

  RFRSHcurrentMillis = millis();
  if (RFRSHcurrentMillis - RFRSHstartMillis >= 50)
  {
    RFRSHstartMillis = RFRSHcurrentMillis;
    RFRSH++;
  }
  /* --- Timer end --- */


  // IS a button pressed on the remote? If so, activate the action
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  }



  // ### Button 0 -> Zero weight
  if ((ActionState == 0) && (Seconds >= 1))
  {
    Serial.println("Button 0 code executed every 1 second");
    StepperPos = 0;
    myStepper.step(stepsPerRevolution);
    //Servo1.write(StepperPos);
    Seconds = 0;
    delay(2000);
    //ActionState = 99;
  }



  // ### Button 1 -> Max weight
  if ( (ActionState == 1) && (Seconds >= 1))
  {
    Serial.println("Button 1 code executed every 1 second");
    StepperPos = 180;
    //Servo1.write(StepperPos);
    Seconds = 0;
  }



  /*#### - Button 2 action
    - Random weights
    #######*/
  if ( (ActionState == 2) && (Seconds >= 5))
  {
    Serial.println("Button 2 code executed every 5 seconds");
    RandomNr = random(10, 160);
    StepperPos = RandomNr;
    //Servo1.write(StepperPos);
    Seconds = 0;
  }



  /*#### - Button 3 action
    - Zero
    - Tare
    - Random weight
    - Wait
    - Empty scale
    - Repeat
    #######*/
  if ( (ActionState == 3) && (Seconds >= 2))
  {
    StepperPos = 0;
    //Servo1.write(StepperPos);
    Serial.println("Action 3 Tare + Random");
    ActionState = 33;
    Seconds = 0;
  }
  if ((ActionState == 33) && (Seconds >= 5))
  {
    Serial.println("Tare 15");
    StepperPos = 15;
    //Servo1.write(StepperPos);
    ActionState = 333;
    Seconds = 0;
  }
  if ((ActionState == 333) && (Seconds >= 8))
  {
    Serial.print("load on scale : ");
    RandomNr = random(120, 160);
    Serial.println(RandomNr);
    StepperPos = RandomNr;
    //Servo1.write(StepperPos);
    Seconds = 0;
    ActionState = 3333;
  }
  if ((ActionState == 3333) && (Seconds >= 14))
  {
    Seconds = 0;
    ActionState = 3;
    Serial.println("Empty Scale");
  }
  /* ## End of button 3
    ---- Setup mode menu ----
  */
  if ((ActionState == 20) && (Seconds >= 1) && (Menu == 0))
  {
    Seconds = 0;
    Serial.println("Menu state at 0");
    Serial.print("Setting state at :");
    Serial.println(Setting);
  }
}
/* --(end main loop )-- */




//actions executed by the remote detection above

void action0()
{
  lcd.setCursor(0, 0);
  lcd.print("Set scale Zero");
  lcd.setCursor(0, 1);
  lcd.print("Setting zero...");
  Serial.println("Action 0 activated");
  ActionState = 0;
}

void action1()
{
  Serial.println("Action 1 activated");
  ActionState = 1;
}
void action2()
{
  Serial.println("Action 2 activated");
  ActionState = 2;
}
void action3()
{
  Serial.println("Action 3 activated");
  ActionState = 3;
}
void actionUp()
{
  Serial.println("Button Up");
  Menu++;
}
void actionDown()
{
  Serial.println("Down");
  Menu++;
}
void actionSetup()
{
  ActionState = 20;
  Line1 = "Setup Mode";
  Serial.println(Line1);
}
void actionVolDown()
{
  Setting--;
}
void actionVolUp()
{
  Setting++;
}

You have still not described the system in a way that means anything to me. Your video is commendably short, but, again I cannot understand it.

As far as I can see you have only one line in your program (line 153) where the stepper motor is intended to move and that line is within an IF statement that is only true when ActionState == 0) && (Seconds >= 1. However I have no idea whether you are creating the conditions for that to be true.

A couple of other things ...

The standard Stepper library uses blocking code so it will do all the steps before allowing the Arduino to do anything else.

The same is true of the delay() function

Taking those together I would not expect your program to be responsive to incoming IR data.

I suggest you make a copy of your program and strip out everything except the minimum needed to receive an IR command that causes the motor to move. That will make debugging very much easier.

...R

Hello,

I've found a bug. This part "myStepper.setSpeed(rolePerMinute);" was not in the setup void.

Its all working now. Still rather strange how this was affecting the whole thing in the way it did.

Been noticing you're a friendly helpfull person, so I did upload a longer video (and it's darker now) that shows what was going on. The remote reciever keeps flickering like someone would push a button.

If you're really interested I'd try and explain the whole concept I'm building.

Thanks for the support, appreciate it.
Maho

Longer video -> longervid - YouTube