Issues with IR Remote control speed with Stepper and A4988 Driver

Hello all,

New to programming and Arduino so please bear with me. I have been working on a project to get a motorized lazy Susan with speed controlled by an IR remote, it also has an LCD display that will display the ambient temp, humidity and a third temperature of a liquid with a separate waterproof temp sensor. I have gotten all of the other components to work together by piecing code parts from different projects and doing edits and a lot of trial and error.

The part that is tripping me up is how to add in the code to get the IR remote to not only control the direction of the stepper motor but also speed it up. I have been able to get it to work independently of all of the other components very basic turn one direction then the other way but cant get it to work with everything else and I have not been able to find clear code for the A4988 driver and a speed controlled by IR remote.

This is as far as I’ve gotten with the code and it works but it is still without the IR Code, any insights would be greatly appreciated. Also please forgive the rudimentary code.

#include <LiquidCrystal.h>
#include <SimpleDHT.h>
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
int pinDHT11 = 6;
SimpleDHT11 dht11;
const int stepPin = 3;
const int dirPin = 4;
int customDelay, customDelayMapped;
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celcius = 0;
float Fahrenheit = 0;
void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(dirPin, HIGH); 
  Serial.begin(9600);
  sensors.begin();
    lcd.begin(16, 2);
    lcd.print("     Vienne ");
  lcd.setCursor(0, 1);
}
void loop() {

  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    Serial.print("Read DHT11 failed");
    return;
  }
  Serial.print("Sample RAW Bits: ");
  for (int i = 0; i < 40; i++) {
    Serial.print((int)data[i]);
    if (i > 0 && ((i + 1) % 4) == 0) {
      Serial.print(' ');
    }
  }
  Serial.println("");
  Serial.print("Sample OK: ");
  Serial.print((int)round(1.8 * +32));
  Serial.println(" *F");
  Serial.print((int)humidity); Serial.println(" %");
  delay(500);
  sensors.requestTemperatures();
  Celcius = sensors.getTempCByIndex(0);
  Fahrenheit = sensors.toFahrenheit(Celcius);
  Serial.print(" C  ");
  Serial.print(Celcius);
  Serial.print(" F  ");
  Serial.println(Fahrenheit);
  delay(500);
  lcd.setCursor(0, 1);
  lcd.print((int)round(1.8 * +32));
  lcd.setCursor(3, 1);
  lcd.print((char)223);
  lcd.setCursor(4, 1);
  lcd.print("F");
  lcd.setCursor(6, 1);
  lcd.print((int)humidity);
  lcd.setCursor(8, 1);
  lcd.print("%H");
  lcd.setCursor(11, 1);
  lcd.print((int)Fahrenheit);
  lcd.setCursor(15, 1);
  lcd.print("F");
  lcd.setCursor(14, 1);
  lcd.print((char)223);
  delay(500);
  digitalWrite(dirPin, HIGH); 
  for (int x = 0; x < 4000; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(1000); 

  digitalWrite(dirPin, LOW); 
  for (int x = 0; x < 4000; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(1000);
}

This is the code I was trying to integrate for the IR remote and was able to get it to work by itself but I could not find how to control the speed and direction with the A4988 driver. The input signal pins were changed to accommodate the other components I had but this is the basic code for reference.

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 12; // Signal Pin of IR receiver to Arduino Digital Pin 6

/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4

Stepper small_stepper(STEPS, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

void setup()
{ 
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?

  {
    switch(results.value)

    {

      case 0xFFA857: // VOL+ button pressed
                      small_stepper.setSpeed(500); //Max seems to be 500
                      Steps2Take  =  2048;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF629D: // VOL- button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  -2048;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
                
    }
    
      irrecv.resume(); // receive the next value
                 digitalWrite(8, LOW);
                 digitalWrite(9, LOW);
                 digitalWrite(10, LOW);
                 digitalWrite(11, LOW);       
  }  


}/* --end main loop -- */

I have attached a couple of pictures as well, I can mock up a diagram if necessary. My main issue is on how to incorporate the IR code within the working one. Any feedback and/or advice would be great.

Thanks

Rather than try to have all the code in loop() I suggest you put the IR code and the stepper code into separate functions called from loop(). So the code in loop() might be as simple as

void loop() {
 checkIRinput();
 moveMotor();
}

That way you can test the code in each function separately.

When you get a value from the IR remote your code should update variables that reflect the required speed and direction.

The stepper code should check those variables before moving the motor.

Another issue is that the regular Stepper library blocks the Arduino until all the steps are complete - so it can't get any new IR input. You could either write our code so it just does one step at a time at an interval determined by you or you could use the non-blocking AccelStepper library

...R
Stepper Motor Basics
Simple Stepper Code

Thank you for the suggestions, I'm still trying to figure out how to properly code simple things. Any advice on tutorials or manuals to better understand how to code Arduino ? Seems like I should go back to basics and get better understanding of how things work...

Thanks

It's worth working through the example sketches under File > Examples. Go through each example line by line. If there's something you don't understand then start researching and experimenting until you do understand it. The reference pages here are a good place to start. There are also tutorials for each of the official examples. When that fails you'll usually find plenty of information with a search engine. If that fails, the forum members are always here to help out. Once you think you completely understand an example, check to be sure by making some modifications to the code and verify it had the expected results.

You don't need to do every single example. You definitely want to do all the basic ones but others might be specific to a topic not relevant to your projects and those are fine to skip over. One of the most important is File > Examples > 02.Digital > BlinkWithoutDelay. Writing non-blocking code is essential to success with more complex projects. For example, in your code above, you have long delays, during which the program can't react to input from the IR remote.

It still is a bit of a leap to start combining the things you've learned together but it's so much easier when you actually understand how each bit of code works, rather than copy, paste, and hope for the best.

Jair826:
Any advice on tutorials or manuals to better understand how to code Arduino ? Seems like I should go back to basics and get better understanding of how things work...

There are lots of useful examples included with the Arduino IDE.

And you may wish to have a look at Planning and Implementing a Program

...R