How to Code 28BYJ-48 Step Motor for Vending Machine

Hello I have already made the rest of the code for the vending machine I am making which uses IR sensors instead of buttons. There is also a coin acceptor. However, I have a problem integrating the code for the step motor into the main code. When I use the code alone, the motors work perfectly. But, when I put them into the main code, they don’t. There is a condition in the main code that involves having the money being accepted reach a certain amount for the motor to turn. Any help would be appreciated. Here’s the code:

#include <AccelStepper.h>

// Define step constants
#define FULLSTEP 4
#define HALFSTEP 8

// Creates two instances
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper1(FULLSTEP, 1, 5, 6, 7);
AccelStepper stepper2(FULLSTEP, 8, 11, 12, 13);

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

int pos = 0;    // variable to store the servo position
int IRSensor1 = 4; // connect ir sensor to arduino pin 2
int IRSensor2 = 3;
int LED = 13; // conect Led to arduino pin 13

// variable use to measure the intervals in between impulses
int i = 0;
// Number of impulses detected
int impulsCount = 0;
// Sum of all the coins inseted
float total_amount = 0;


void setup() {
  {
    // set the maximum speed, acceleration factor,
    // initial speed and the target position for motor 1
    stepper1.setMaxSpeed(1000.0);
    stepper1.setAcceleration(50.0);
    stepper1.setSpeed(200);
    stepper1.moveTo(2038);

    // set the same for motor 2
    stepper2.setMaxSpeed(1000.0);
    stepper2.setAcceleration(50.0);
    stepper2.setSpeed(200);
    stepper2.moveTo(2038);
  }

  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.print("Hello");

  attachInterrupt(0, incomingImpuls, FALLING);

}

void incomingImpuls()
{
  impulsCount = impulsCount + 1;
  i = 0;
}

void loop() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Insert a coin!");

  i = i + 1;

  Serial.print("i=");
  Serial.print(i);
  Serial.print(" Impulses:");
  Serial.print(impulsCount);
  Serial.print(" Total:");
  Serial.println(total_amount);

  if (i >= 30 and impulsCount == 1) {
    total_amount = total_amount + 10;
    impulsCount = 0;
  }
  if (i >= 30 and impulsCount == 2) {
    total_amount = total_amount + 5;
    impulsCount = 0;
  }
  if (i >= 30 and impulsCount == 3) {
    total_amount = total_amount + 5;
    impulsCount = 0;
  }
  if (i >= 30 and impulsCount == 4) {
    total_amount = total_amount + 10;
    impulsCount = 0;
  }
  //  delay(200);
  int statusSensor1 = digitalRead (IRSensor1);
  //Serial.println(statusSensor1);
  if (statusSensor1 == 1) {
    digitalWrite(LED, LOW); // LED LOW

  }
  else if ( total_amount >= 20) {
    digitalWrite(LED, HIGH); // LED High
    lcd.clear();
    lcd.print("Surgical Mask");
    total_amount = total_amount - 20;
    delay(1000);
    stepper1.run();
  }

  int statusSensor2 = digitalRead (IRSensor2);
  //Serial.println(statusSensor2);
  if (statusSensor2 == 1) {
    digitalWrite(LED, LOW); // LED LOW

  }
  else if ( total_amount >= 5) {
    digitalWrite(LED, HIGH); // LED High
    lcd.clear();
    lcd.print("Cloth Mask");
    total_amount = total_amount
  delay(1000);
    stepper2.run();
  }
}

Hi, @biennne
Welcome to the forum.

To add code please click this link;

What model Arduino are you using?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

i’ve already changed it, thanks. im using arduino uno.

AccelStepper doesn’t work as you expect it to do.
What’s your idea at this point:

    delay(1000);
    stepper1.run();

Do you want it to move to a fixed position? Or do you want it to turn a fixed amount of steps every time it comes to this point?
In either case, the run method is wrong. run() never does more than one step ( or no step at all ). run() must be called over and over again to keep the stepper turning.

oh. what i meant for the delay was that the writing on the LCD would last longer but I didn’t think of the motor. I want the motor to turn one revolution. what method do you suggest? thank you so much!

There are different possibilities. One may be:

stepper1.runToPosition( stepper1.currentPosition()+2048);

if 2048 is the number of steps per revolution. But this method blocks until one revolution is done. ( maybe you don’t need the delay(1000); in this case )
Be aware, that your UNO doesn’t react on anything in this time.

When i try that it says:

no matching function for call to ‘AccelStepper::runToPosition(long int)’

sorry im still a beginner and ive already installed the latest version of the library

Sorry, my fault. It should be runToNewPosition

Thank you so much!!! You really saved me and my project. I hope you have a great day!