I just started to use the Arduino so please be patient. I'm working on setting up a system that when a button is pushed it drives a stepper motor 180 degrees then stops. At the same time I have the possibility of another switch being activated for a counter to an LCD screen. All parts work individually as they should and together until during the stepper motor running the other switch will not be read. I've tried looking for how to run the stepper in the "background" while still looking for inputs to no avail.
/*
Reloading station
Operations
1) Power case feeder using light sensor as input to run motor or not.
2) Rotate case to position from button push
3) Count rounds as they are completed
1) have reset count button
2) have +1 and -1 buttons for counter
*/
#include <Stepper.h> // Stepper Library
#include <LiquidCrystal.h> // LCD Library
// Stepper Motor variables for speed and steps per revolution
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 17; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // initialize the stepper library on pins 8 through 11:
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // initialize the library with the numbers of the interface pins
// Variables for wait before accepting new input
const unsigned long eventtime_1_count = 250; // 250 milliseconds
const unsigned long eventtime_2_reset = 500; //500 miliseconds
//Delay function based off millis.
unsigned long previoustime_1 = 0;
unsigned long previoustime_2 = 0;
unsigned long currenttime;
const int button = 13; // Set input to pin 13
int lastState = HIGH; // Set Buttons last state to HIGH (not pressed)
int currState; // Declare currState
const int sensor = 12; // Declare Sensor and set pin to 12
int count = 0; // Declare count for rounds and set to 0
const int reset = 22; // Declare reset button pin to 22
void setup()
{
Serial.begin(9600);
myStepper.setSpeed(rolePerMinute); //set speed of stepper
pinMode(button, INPUT_PULLUP); //set input pin to internal resistor (high = open switch, low = closed switch)
pinMode(sensor, INPUT_PULLUP); //set input pin to internal resistor (high = open switch, low = closed switch)
pinMode(reset, INPUT_PULLUP); //set input pin to internal resistor (high = open switch, low = closed switch)
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("Rounds = 0"); // Print a message to the LCD.
}
void loop()
{
currenttime = millis(); //read current time
CaseMotorFunction(); //jump to case motor function
RoundCountFunction();
RoundCountResetFunction();
}
void CaseMotorFunction()
{
currState = digitalRead(button);
if (currState == LOW && currState != lastState)
{
Serial.println("clockwise"); // rotate motor clockwise
myStepper.step(stepsPerRevolution / 2); // rotate motor 180 degrees
lastState = button; // change last state to current state
}
lastState = currState;
}
void RoundCountFunction()
{
// check if enough time has passed since previous activation (using as a debounce/double click)
if (currenttime - previoustime_1 >= eventtime_1_count)
{
// if switch is pressed add 1 to counter
if (digitalRead(sensor) == LOW)
{
// increase counter
count = count + 1;
// set the cursor to column 0, line 1
lcd.setCursor(9, 0);
// print the number of rounds
lcd.print(count);
// update previoustime millis
previoustime_1 = currenttime;
}
}
}
void RoundCountResetFunction()
{
// check if enough time has passed since previous activation (using as a debounce/double click)
if (currenttime - previoustime_2 >= eventtime_2_reset)
{
// if switch is pressed reset counter to 0
if (digitalRead(reset) == LOW)
{
// set count int to "0"
count = 0;
// sets position of cursor on LCD
lcd.setCursor(9, 0);
// print "0 " to LCD
lcd.print("0 ");
// change millis previous time
previoustime_2 = currenttime;
}
}
}