Hello,
I have a double conditional if statement based on user input. Even when I input a correct values, the if statement considers them false when they are actually true. The if statement I am refering to in my code is the third, sub-if statement where:
if (recerveddec >= minstepsize && recerveddec <= maxstepsize)
Before I added the second condition it was:
if (recerveddec >= minstepsize)
and this worked just fine. The entire code is posted below:
// Define Constants
// User Inputs (Changes based on motor and application)
// Other constants
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean userinput = false;
int dataNumber = 0; // new for this version
float recerveddec;
const int maxstepsize = 100000;
const int minstepsize = 1;
const int sethalfwavelength = 1000; // Set half wavelength to determine speed. Range of available speeds without abnormal vibrations: [1000,3000]. In units of (micro-secnds [10^-6])
// Connections to A4988
const int motor1dirPin = 2; // Motor 1 direction
const int motor1stepPin = 3; // Motor 1 movement
// Connections to User Interface
const int num1controldir = 12; // Rotation direction definition pin # for motor 1
const int num2controldir = 13; // Rotation direction definition pin # for motor 2
const int motor1ID = A1; // ID pin for motor 1
void setup() {
// Designate Output Pins
pinMode(motor1stepPin,OUTPUT);
pinMode(motor1dirPin,OUTPUT);
// Designate Input Pins
pinMode(motor1ID,INPUT_PULLUP);
pinMode(num1controldir,INPUT_PULLUP);
{
// Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
delay(5000);
}
// Stepper Motor Program Begins and Loops
void loop()
{
recData();
displayData();
MoveMotor();
}
void recData()
{
static byte ndx = 0;
char endMarker = '\n';
char c;
if (Serial.available() > 0) // If there is a serial available, read it
{
// read the incoming byte:
c = Serial.read();
if (c != endMarker)
{
receivedChars[ndx] = c;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
Serial.println(receivedChars);
ndx = 0;
userinput = true;
}
}
}
void displayData()
{
if (userinput == true)
{
recerveddec = atoi(receivedChars);
}
}
void MoveMotor()
{
// To control motor 1 positive movements via numerical input (Input must be an integer value)
if (digitalRead(motor1ID) == LOW) { // ID motor 1
if (digitalRead(num1controldir) == LOW) {
//Serial.print("<Positive rotation for motor 1 selected> ");
//delay(5000);
if (userinput == true) { // If there is a serial available, read it
Serial.print("Identified user input");
digitalWrite(motor1dirPin,HIGH); // Set motor rotation direction
if (recerveddec >= minstepsize && recerveddec <= maxstepsize) {
Serial.print("Number of steps in for loop: ");
Serial.println(recerveddec);
for (int substep = 1; substep <= recerveddec; substep++) {
digitalWrite(motor1stepPin,HIGH);
delayMicroseconds(sethalfwavelength);
digitalWrite(motor1stepPin,LOW);
delayMicroseconds(sethalfwavelength);
}
}
else {
Serial.print("Number of steps must be within a range of [1,10000]!");
}
}
}
}