Hmm trigger pot change

hmm trigger pot change,

  int crsx=analogRead(A0);
  if(page==0){
      


    while( (abs(crsx-oldval) >0) or (abs(oldval-crsx) >0) )

        delay(1);
            
                tft.fillScreen(0x0000);
tft.fillRect(1,1,238,2,0xC618);
tft.fillRect(1,1,2,21,0xC618);
Serial.println("hello");
  oldval=crsx;

  };

but the code does spam when launch but after changing the value spam stops and never sends me example println

If you use Auto Format in the IDE then you will see that the code looks like this

int crsx = analogRead(A0);
if (page == 0)
{
  while ( (abs(crsx - oldval) > 0) or (abs(oldval - crsx) > 0) )
    delay(1);
  tft.fillScreen(0x0000);
  tft.fillRect(1, 1, 238, 2, 0xC618);
  tft.fillRect(1, 1, 2, 21, 0xC618);
  Serial.println("hello");
  oldval = crsx;
};

You only have 1 line of code in the while loop code block. The rest will be executed unconditionally

your code is locked in the while loop when the value changes (i.e. > 0)

i think you want following.
only recognize a change when >~2
a single abs() recognizes both positive and negative changes

    int crsx = analogRead (A0);
    if ( (abs(crsx-oldval) > 2) {
        tft.fillScreen (0x0000);
        tft.fillRect   (1, 1, 238, 2, 0xC618);
        tft.fillRect   (1, 1, 2, 21, 0xC618);
        Serial.println ("hello");
    }
    oldval = crsx;

hmm i think this will work

this also doesn't work

What exactly do you mean when you say the program "spams".

As originally written, the code should lock up in the while loop and never do anything else, since the value of crsx and oldval never change within the while loop. Also, the while loop condition is basically

while (crsx != oldval)

which would require a lot less code to execute.

< edit >
The code you posted will continuously print "hello" and re-write the tft display, until the trimmer pot changes, at which time the condition for the while loop becomes true, and the code goes into the endless while loop.

What is "page0"?

You may want a dead spot where any change less than 2 or more depending on your pot (know from joysticks and flight sims) ... don't set oldval to the little changes, only change past the dead zone.

I actually find it myself. The ADC or Pot or maybe Map function does noise
so. i mapped it to ten by 1024 & 11 values (+1 because it fixes bugs)

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