Hello! I am having trouble programming a stepper motor (Kysan NEMA 17 from Arduino.com) to respond to an input. It is being used in an auto-regulating flow system. I have been learning to program in Arduino as I go, so my knowledge is still somewhat limited.
How it SHOULD work: Arduino Uno counts/stores pulses from a flow sensor (pin 6), calculates "flowrate" for display on an LCD, then a stepper motor opens or closes a valve if that value is outside a predetermined limit. Currently, the stepper is not responding to the code at all, but everything else is functional. The stepper operation has been verified with sample code.
Any insight or guesses as to why it might not be working would be GREATLY appreciated! Thank you so much!
//**These are pre-loaded libraries I want to include in my code
#include <LiquidCrystal.h>
#include <Stepper.h>
#include <MotorDriver.h>
//This is the basic setup of the code, where I define variables
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define FLOWSENSORPIN 6
#define MOTOR_CLOCKWISE 0
#define MOTOR_ANTICLOCKWISE 1
/******Motor Shield Pins definitions*************/
#define MOTORSHIELD_IN1 8
#define MOTORSHIELD_IN2 11
#define MOTORSHIELD_IN3 12
#define MOTORSHIELD_IN4 13
#define CTRLPIN_A 9
#define CTRLPIN_B 10
#define stepper_ctrl[]={0x27,0x36,0x1e,0x0f};
MotorStruct stepperMotor;
void initialize()
{
pinMode(MOTORSHIELD_IN1,OUTPUT);
pinMode(MOTORSHIELD_IN2,OUTPUT);
pinMode(MOTORSHIELD_IN3,OUTPUT);
pinMode(MOTORSHIELD_IN4,OUTPUT);
pinMode(CTRLPIN_A,OUTPUT);
pinMode(CTRLPIN_B,OUTPUT);
}
void setup()
{
pinMode(FLOWSENSORPIN, LOW);
int pin = 6;
unsigned long duration;
lcd.begin(16,2);
}
void loop()
{
int flowpinstate = 0;
int flowrate = 0;
int lastflowpinstate;
int pulses = 0;
long duration = 0;
flowpinstate = digitalRead(FLOWSENSORPIN);
if (flowpinstate != lastflowpinstate) //This compares the flow pin input to the previous to determine if there was a flow.
{ duration = pulseIn(FLOWSENSORPIN, HIGH)/1000; } //Divides the pulse time by 1000, giving a more easily interpreted number
{
if (flowpinstate == HIGH) // If there was a high on the flow sensor, increments the pulse counter
{ (pulses++); }
}
{flowrate = duration/pulses; }
lastflowpinstate = flowpinstate; // Saves the current flowpin state for comparison in the next run of the loop
if (duration >= 100)
{duration = 0;
pulses = 0;}
if (flowrate > 50)
{ stepperMotor.direction = MOTOR_ANTICLOCKWISE;}
if (flowrate < 50)
{ stepperMotor.direction = MOTOR_CLOCKWISE;}
lcd.setCursor (0,0);
lcd.print ("FLOWRATE: "); lcd.print (flowrate);
delay(500);
}