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();
}
}