Go Down

Topic: Stepper & IR acting funny (Read 27 times) previous topic - next topic

Mahoke

Today at 08:10 pm Last Edit: Today at 08:35 pm by Mahoke
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


Code: [Select]
// 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...

Mahoke

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

Go Up