[Solved]Code appears to be running in random order inside an if

I want 'w' to equal the higher of 'lSpeed' and 'rSpeed' but I get a random value. most of the time it stays between 0 and 2 but sometimes it jumps to the correct number. I've searched the code high and low trying to find the issue. Both 'lSpeed' and 'rSpeed' have right values but somehow 'w' does not. I have used the exact same code in order to get 'm' but it works. I'm at a loss here.

I'll post an image of my wiring and my serial reading.

If I've missed any info I'll try to get it asap.

/*
  The purpose statement for this peice of code:
  I have a corn planter with a ground drive system which means
  that it uses the wheels running on the ground to turn the meters
  at the correct speed to get the right population.
  I want to switch it to hydraulic drive. So I'm taking a speed reading from the wheels
  and from the hydralic motor shaft and trying to keep them the same. The advantage of
  hydraulic drive is that I can vary the population(motor speed) on the go. So to do
  that I have a potentiometer which edits the incoming value of the wheel speed.
*/
//User Settings
const int troubleshooting = 1;          //Basically turned the print on and off
const int aggressiveness = 10;          //Changes the speed at which the PWM is increased / decreased
const unsigned int minSpeed = 1999;     //Increase number to decrease min speed (64000 max)
//Pin Assignment
const int potPin = A0; float pot = 512; //Potentiometer for varying the rate
const int mPulseP = 2; int mPulse = 0;  //Incoming pulse from the motor
const int lPulseP = 3; int lPulse = 0;  //Incoming pulse from the left wheel
const int rPulseP = 4; int rPulse = 0;  //Incoming pulse from the right wheel
const int pwmPin =  9; int pwm =  0;    //PWM going out to the motor
const int ledPin =  6;                  //Indicates when the potentiometer is centered
const int impPin =  7; int imp =    0;  //Indicates when implement is up or down
const int pwmDrop = 8;                  //Allows me to gently drop the pwm to zero
//Millis() Value
unsigned long millisV;
//500ms Clock (Loops ever 500 milliseconds)
unsigned long clock1;
unsigned long clockRst1;
//Aggressiveness Clock (Times the incrementing/decrementing of the pwm)
unsigned long clock2;
unsigned long clockRst2;
//Speed Sensors
int mPulseOld; unsigned long mSpeed; unsigned long mSpeedOld; int m; //For motor

int lPulseOld; unsigned long lSpeed; unsigned long lSpeedOld;        //For left wheel
float w;
int rPulseOld; unsigned long rSpeed; unsigned long rSpeedOld;        //For right wheel


unsigned int loops;                                                  //Stores the amount of loops per second
void setup() {

  if (troubleshooting == 1) {
    Serial.begin(9600);
  }

}

void loop() {
  loops++;                        //Counts the loops per second
  //Value Assignment
  millisV = millis();
  mPulse = digitalRead(mPulseP);
  lPulse = digitalRead(lPulseP);
  rPulse = digitalRead(rPulseP);
  //500ms Clock
  clock1 = millisV - clockRst1;
  if (clock1 > 500) {
    clock1 = 0;
    clockRst1 = millisV;
    //Reads potentiometer every 500ms
    pot = analogRead(potPin);
    digitalWrite(ledPin, LOW);
    if (pot > 412 and pot < 612) {
      pot = 512;
      digitalWrite(ledPin, HIGH);
    }
    //Changes potentiometer reading into a decimal value
    pot = pot / 512;
    pot = pot / 2;
    pot = pot + 0.5;
    if (pot > 1) {
      pot = pot - 0.1;
    }
    if (pot < 1) {
      pot = pot + 0.1;
    }
    //Prints troubleshooting info
    if (troubleshooting == 1) {
      loops = loops * 2;
      Serial.print("Loop Speed ~ ");
      Serial.print(loops);
      Serial.print(" | Motor Speed ~ ");
      Serial.print(m);
      Serial.print(" | Wheel Speed ~ ");
      Serial.print(w);
      Serial.print(" | L Clock ");
      Serial.print(lSpeed);
      Serial.print(" | R Clock");
      Serial.print(rSpeed);
      Serial.print(" | M Clock ");
      Serial.print(mSpeed);
      Serial.print(" | PWM ~ ");
      Serial.println(pwm);
    }
    //Zeros loop count every 500ms
    loops = 0;
  }

  if (mSpeed <= minSpeed) {                //Calculates motor speed
    mSpeed = millisV - mSpeedOld;
  }
  if (mPulse == 1 and mPulseOld != 1) {
    m = mSpeed;
    mSpeedOld = millisV;
    mSpeed = 0;
  }
  mPulseOld = mPulse;

  if (lSpeed <= minSpeed) {                //Calculates left wheel speed
    lSpeed = millisV - lSpeedOld;
  }
  if (lPulse == 1 and lPulseOld != 1) {
    w = lSpeed;
    lSpeedOld = millisV;
    lSpeed = 0;
  }
  lPulseOld = lPulse;

  if (rSpeed <= minSpeed) {                //Calculates right wheel speed
    rSpeed = millisV - rSpeedOld;
  }
  if (rPulse == 1 and rPulseOld != 1) {
    if (w > rSpeed) {
      w = rSpeed;
    }
    rSpeedOld = millisV;
    rSpeed = 0;
  }
  rPulseOld = rPulse;
  imp = digitalRead(impPin);               //Sets implement value (up(0) or down(1))
  clock2 = millisV - clockRst2;
  if (clock2 >= aggressiveness) {          //PWM control
    clockRst2 = millisV;
    clock2 = 0;
    if (imp == 1) {
      w = w * pot;                         //Adjustment of wheel speed for potentiometer value
      if ((m > w  and pwm < 255) and digitalRead(pwmDrop) == 0) { //Increments pwm
        pwm++;
      }
      if ((m < w or digitalRead(pwmDrop) == 1) and pwm > 0) {     //Decrements pwm (pwmDrop will be deleted once I have finished perfecting it in the simulator)
        pwm--;
        if (digitalRead(pwmDrop) == 1 and pwm > 5) {
          pwm = pwm - 5;
        }
      }
      w = w / pot;
    }
  }
  if (mSpeed >= minSpeed) {                                       //This ensures the motor always starts again but im not quite sure how
    m = 2001;
  }
  if (lSpeed >= minSpeed and rSpeed >= minSpeed) {                //I'm pretty sure this is unnecessary but I'm scared to touch it
    w = 2001;
  }
  if ((lSpeed < minSpeed or rSpeed < minSpeed) and imp == 1) {    //Writes the pwm if the implement is down
    analogWrite(pwmPin, pwm);
  }
  if (lSpeed >= minSpeed) {                                       //Turns off pwm if the wheel speed is below the set minimum
    if (rSpeed >= minSpeed) {
      digitalWrite(pwmPin, LOW);
    }
  }

  if (imp == 0) {                                                 //Turns off pwm if the implement is down
    digitalWrite(pwmPin, LOW);
  }
}

In the wiring image I have one nano creating a pulse on pins 3 or 4 and the other reading the pulse and doing the computing. The motor creates a pulse on pin 2.

In the serial monitor you can see my issue. I've looked for hints in the numbers getting spit out but I cant find any.

I added some serial prints in new spots with interesting results

  if (lPulse == 1 and lPulseOld == 0) {
    w = lSpeed;
    lSpeedOld = millisV;
    Serial.println(lSpeed);
    Serial.println(w);
    lSpeed = 0;
  }

In the serial monitor 'w' is the number with .00 accuracy
It looks like its not always running in order but I know that's not the issue.


I did another print in a different spot

if (lSpeed <= minSpeed) {                //Calculates left wheel speed
    lSpeed = millisV - lSpeedOld;
  }
  Serial.println(lSpeed);
  if (lPulse == 1 and lPulseOld == 0) {
    Serial.println(lSpeed);
    w = lSpeed;
    lSpeed = 0;
    lSpeedOld = millisV;
  }

Serial Monitor.jpg
Every time I do a print anywhere inside the 'if' in question it goes all funky but if I put the print right before all the values make sense

Serial Monitor.jpg

Isn't this just a continuation of this thread?:

aarg:
Isn't this just a continuation of this thread?:
https://forum.arduino.cc/index.php?topic=726547.0

Didn't that one get locked? I couldn't post there because it said it had been locked.

To anyone interested it works now and I have no idea why. :confused:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.