I am working with a group to design an automated control valve. We are using a STEPPERONLINE 19:1 Planetary Gearbox High Torque Nema 17 Stepper Motor and a Sharp IR sensor. While running test with the code we created, we ran into an issue with getting the motor to settle on a fixed point. We believe that the code is ignoring the second for loop in our code and thus causing the issue, but are not sure why. Any feed back would be appreciated:
Part of code with For Loops:
//Stepper Motor:
if (setPoint-realPoint >= 0.1){
digitalWrite(2, HIGH);
for (i = 0; i <= 500; i++)
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
}
}
else if (setPoint-realPoint < 0){
//delayMicroseconds(60);
digitalWrite(2, LOW);
for (i = 0; i <= 500; i++);
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
}
}
}
Entire code:
#include <SharpIR.h>
#include <math.h>
#define logf
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// 430 for GP2Y0A41SK
/*
2 to 15 cm GP2Y0A51SK0F use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series use 430
10 to 80 cm GP2Y0A21YK0F use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F use 100550
*/
/*
Smoothing
Reads repeatedly from an analog input, calculating a running average and
printing it to the computer. Keeps ten readings in an array and continually
averages them.
The circuit:
- analog sensor (potentiometer will do) attached to analog input 0
created 22 Apr 2007
by David A. Mellis <dam@mellis.org>
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Smoothing
*/
// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
int realPoint = 0;
int setPoint = 0;
void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
//Stepper Motor:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
//digitalWrite(2, LOW); // Determines Direction Stepper Motor Spins.
}
int i=0;
int f=0;
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = 0.0311*(analogRead(inputPin))-2.4814;
//float pinFloat = readings[readIndex] * 5.0 / 1023.0;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.print("\t");
Serial.println(0.0311*(analogRead(inputPin))-2.4814);
Serial.print("\t");
Serial.print(setPoint-(0.0311*(analogRead(inputPin))-2.4814));
realPoint = 0.0311*(analogRead(inputPin))-2.4814;
delay(10); // delay in between reads for stability
//Stepper Motor:
if (setPoint-realPoint >= 0.1){
digitalWrite(2, HIGH);
for (i = 0; i <= 500; i++)
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
}
}
else if (setPoint-realPoint < 0){
//delayMicroseconds(60);
digitalWrite(2, LOW);
for (i = 0; i <= 500; i++);
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
}
}
/*
if (error >= 0.5){
digitalWrite(2, HIGH);
if (i=0, i<500)
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
i=i+1;
}
}
if (error <= 0.5){
digitalWrite(2, LOW);
if (i=0, i<500);
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
i=i+1;
}
}
if (error >= .25){
digitalWrite(2, HIGH);
if (i=0, i<250)
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
i=i+1;
}
}
if (error < .25){
digitalWrite(2, LOW);
if (i=0, i<250);
{
digitalWrite(3,HIGH);
digitalWrite(3,LOW);
delayMicroseconds(60);
i=i+1;
}
}
if (error <= .25){
Serial.print("Done.");
}
*/
}