While loop troubles on 328P chip

Good day,

I am having trouble with programming of my PCB which makes use of 328P chip. The program I wrote was tested on a Arduino Uno and all worked fine. The problem comes when transferring the program to a chip to be used on the PCB.

Background:
The PCB runs a motor driver with input from a Nextion screen. This is the starting/stopping interface for the motors. On a certain page of the screen, the program is commanded into a function with a while loop to run the motor. This works perfectly fine on the Uno and even when I transfer the UNO chip to the PCB, but doesn't work at all with the 328P Chip.

All of the other commands work excellent on the chip, even when starting the motor outside a while loop, the only problem is experienced when calling the while loop, then nothing happens.

Below is the code for the loop:

void AdvCalibrate() {
  while (run2 == 1) {

    myNex.NextionListen();


    currT = micros();
    deltaT = currT - prevT;

    myNex.NextionListen();


    if (deltaT >= 100) {

      deltaT = ((float)(currT - prevT)) / 1.0e06;
      diff = pos - pos_i;
      velocity2 = diff / deltaT;
      v2 = velocity2 / 1080 * 60.0;

      myNex.NextionListen();


      vFilt = Filter.updateEstimate(v2);
      Input = (vFilt / RPM_max) * 255;
      prevT = currT;
      pos_i = pos;
    }

    myNex.NextionListen();


    Setpoint = MSpeed_cal;
    int gap = abs(Setpoint - Input);
    myNex.NextionListen();

    if (gap < Diffpid_save) {
      encoder.SetTunings(Kp, Ki, Kd);
    } else
    {
      encoder.SetTunings(aggKp, aggKi, aggKd);
    }
    myNex.NextionListen();


    encoder.Compute();
    analogWrite(Motor, Output);

    myNex.NextionListen();


    advcurT = millis();
    if (advcurT - advprevT >= 1000) {
      myNex.writeNum("rpmtest.val", vFilt);
      advprevT = advcurT;
    }

    myNex.NextionListen();


    if (run2 == 0) {

      analogWrite(Motor, 0);
      break;

    }
  }
}

Where is the value of run2 changed in the while loop to allow the loop to end ?

Why are you testing whether run2 equals zero in a while loop that depends on the value being 1 ?

Please post your complete sketch

The value of run2 is changed by the press of a button on the screen. This is not where the problem is as this runs perfectly on an UNO as well as the UNO’s chip on the PCB just not with a programmed chip.
The WHOLe programming works fine if it is run from an UNO and the rest of the program runs fine with the chip in the PCB. Its just the while loop that doesnt want to run.

Print a message to Serial when the button press is detected. If you don't ever see the message then you have localised the problem to how the button press is detected and passed to the sketch

The trouble seems to be with the timer. after alot of tampering it looks as if the reading received from the enocder causes the problem. This is either from the interrupt of the encoder or with the timer, with the timer being the more likely culprit. Still struggling to solve this problem.

you're ignoring accurate advice based on the fragment of code you posted.

perhaps you should post the entire code ... if you're serious about wanting help. otherwise others are unlikely to provide help if it's just going to be ignored

2 Likes

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