Touchscreen buttons - can you do 'something' while touching a button

To explain - I am using the MCUFRIEND_kbv.h and TouchScreen.h libraries and have a number of touchscreen buttons - thanks to David Prentice setting them up was a dawdle once I grasped his code.
Some of these buttons I require to operate external devices e.g. a stepper motor and I assumed I could do this my changing the status of a pin on the Arduino Mega.

I had thought that you could touch and hold an on-screen button and this would allow a 'While' loop to work - in simple terms press the button and set a pin HIGH until the touchscreen button is released and it goes LOW.

Struggled for hours - couldn't get this to work so thought use two buttons one to switch ON another to switch OFF. Once again stumped - once in the While loop there is no way out!

Trying desperately to avoid any external buttons or other physical devices, switches etc.

Any help appreciated.

Of course you can.

Just write what you want to do in plain English.

You can even quote which common device works with few buttons. e.g. setting your wristwatch or a central heating timer or ...

David.

David,

Once again I fail to grasp it. Clearly I fail to grasp the functionality of your methods.

One example - in plain English:

I have a touchscreen with a button to increment a count.

I want this count to increment until stopped:

Option1) ideally by pressing and releasing the same touchscreen button i.e. keep counting until I release the touchscreenbutton.

But if this isn't possible then :

Option 2) By pressing say a 'Stop' button

My idea was to use a 'While' loop to increment the counter but once I set it off I can't find a way to stop it.

One of many snippets:

while (countUp_btn.justPressed() == true ){
countUp_btn.drawButton(true);
digitalWrite(counterPin, HIGH);
Serial.println("Still going");
y++;
framesize = (y / 10);
tft.setCursor(148, 192);
tft.setTextColor(BLACK);
tft.print(framesize);
delay(100);
tft.fillRoundRect(139, 188, 75, 30, 4, CYAN);
tft.setCursor(150, 195);
tft.setTextColor(BLACK);
tft.print(framesize);
}

This is a never ending loop as there is nothing to stop it given that releasing countUp.btn doesn't seem to alter the truth. I had foolishly assumed that the touchscreen button would act like a physical button stay 'on' whilst pressed and on release switch 'off'.

So I thought I could use countUp.justReleased but got nowhere.

So then I tried while ((countUp_btn.justPressed() == true ) && stop_btn.justPressed() == false)){

.. no joy.

Clearly this is blindingly obvious to you - but I've spent hours and I see many others on various forums asking the same question - so at least I'm not alone!

How about a clue or two ....

best

Charles

Think about your wristwatch or digital Alarm Clock.

You either blip the button for each increment or you hold it down for 5 seconds and it starts auto-incrementing all the time you keep the steady press. Release. And you go back to individual blips.

The typewriter keyboard is similar. Individual keypresses or auto-repeat if you hold the key for a certain time.

To do this with Adafruit_GFX_Button, you would use press(), isPressed(), justPressed(), justReleased()

You measure time intervals with millis() in the normal way. e.g. while (isPressed() && millis() < threshold).

It is difficult to describe in words. But draw a flow chart on a piece of paper and you can see exactly how to implement your desired behaviour.

David.

David,

The concept absolutely clear and the original objective.

However, excuse my stupidity - but I was unaware of the other functions isPressed etc and simply cannot find any reference to them or the other button functions in the Adafruit GFX library.

I see a number of others pleading for this information, some direct to Adafruit Customer Support - only to be pointed to the Adaphon sketch which covers some of what I need. Once again what is obvious to the highly experienced ..............

Now that I am aware of the other functions e.g. isPressed in particular it is ridiculously easy to implement what I need to do without the need for any While loops.

Thanks again

Charles

Check that your Adafruit_GFX is up to date via the Library Manager.

I am not aware of any official Tutorial.

You can see which class methods are available by reading the Adafruit_GFX.h
The names are pretty intuitive. Of course this must be difficult for non-English speakers.

My "button_simple.ino" example shows the basic approach.
The "button_multiple.ino" that I pasted for you earlier requires some study.
I have previously pasted (or attached) a "button_phonecal" sketch

I was not aware of an "Adaphon" sketch.

The difficult part of any "Touch" sketch is that resistive Touch Panels vary greatly and the TouchScreen library returns ADC values and not pixel coordinates.

So you have to calibrate, paste the calibration, map the ADC values to pixel coordinates yourself.

I suppose that I could write a TouchScreen_pixel_kbv.h library that looks after the restore XM, YP and pixel mapping.
However punters would still need to run a Calibration sketch. It is hard enough to persuade them to use the Library Manager.

David.

Hi David,

I must be missing something given I cannot see any references at all to buttons in Adafruit GFX_h.

However, been romping on now that you gave me the isPressed function.

But ....... hit another problem which I don't understand and may be related to hardware - prepare for another silly question.....

All of a sudden my sketch - although compiling nicely - will not upload and hangs the Arduino IDE - the tft screen with all white. Upload fails.

Now I assumed I'd done something wrong as usual so I rebooted the p.c. - the only way to get the IDE running again and removed the last few lines of code which were writing another couple of rectangles on screen.

Everything working again - however, not matter what I now try to add if it involves adding to the tft screen display it hangs everything.

I'm 80% there with my project - is it a case of the tft screen having its own ram and I've hit its limits.

It's an Amazon cheapie - 3.5" 480 x 340, ILI9486 - unbranded.

If that's the case I'm screwed unless you can advise me of a better alternative or a way to get more memory capacity.

Searching for data but very few screen sellers give any meaningful specifications .

Thanks in advance.

Best

Charles

The Library Manager will tell you whether your Adafruit_GFX library is up to date.

If you enable verbose compile in the IDE File->Preferences you can see which library versions were used e.g.

Using library SPI at version 1.0 in folder: C:\Program Files (x86)\Arduino-1.8.1\hardware\arduino\avr\libraries\SPI 
Using library ST7789_kbv in folder: C:\Users\David Prentice\Documents\Arduino\libraries\ST7789_kbv (legacy)
Using library Adafruit_GFX_Library at version 1.4.7 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\Adafruit_GFX_Library 
Using library SerialFlash at version 0.5 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\SerialFlash 
Sketch uses 15890 bytes (49%) of program storage space. Maximum is 32256 bytes.
Global variables use 320 bytes (15%) of dynamic memory, leaving 1728 bytes for local variables. Maximum is 2048 bytes.

You get reams of boring stuff about the build. Just copy-paste the "Using library" and "memory used" lines like my example.

If you have a problem, copy-paste or attach your sketch.
Be considerate. i.e. post neatly formatted code. show that you have designed your code and thought about the logic flow.

Forum members will help if you have made your project easy to understand and attractive to read.

David.

David,

I'm confident I'm using the latest libraries - used Manage Libraries but already have preferences set to auto update.

I attach the verbose compiler output.

I now cannot add any rectangles / buttons whatsoever - totally exasperated.

I now have all the code for my project working and just need the GUI functional as the interface.

Serious depression and soaring blood pressure - if my understanding is correct the Arduino Mega isn't the limitation, it is the tft screen itself? Is there a way out of this (a new tft screen/) or do I consign all to the waste?

I can't get the compiler error messages to cut and paste given the error locks up the Arduino IDE and I'm forced to reboot the pc each time.

Once again I'm totally dependant on your knowledge and support - nobody locally interested in Arduino's.

Anxiously await your response.

Best

Charles

p.s. I'd gladly attach my sketch but it is rather lengthy.

You can attach files. Espcially with large or multi-tab projects it is the most convenient way to "post" code.
Start your message. Then click [Preview]. The attachments option appears.

I would expect the Arduino IDE and libraries to "install" painlessly.
We are all capable of programming errors. But the IDE will just give an "Error report". It should not crash.

It is helpful to paste "snippets" like library and memory use from a successful build e.g. one of the library examples.

David.

David,

I have now found that the error only happens on one screen? It happens to have a lot of buttons and a Progress Bar - I wonder if that is causing the loading problems / error? That being said the Progress bar actually works in testing.

I'm changing layouts to use a different screen and so far seems to be o.k.

I'm going to carry on overnight in an attempt to 'stitch' everything together and check if all functions as expected. I'm not in the best of health and time is of the essence. I had the underlying code working on a 20 x 4 lcd but it was painful to read and required a joystick and four buttons.

With the tft touchscreen and your assistance I now have no physical buttons, joystick or switches and a meaningful display!

I've also discovered that the Mega doesn't work to the mathematical precision I require for Depth Of Field calculations - so I've just bought a DUE from Amazon - hopefully it'll just be a plug-in replacement.

Once again - many thanks for your presence and support. No doubt I will have to get bacjk to you again.
Soooo much to learn so little time - I can see a myriad of uses for these tft screens and I will have to get a lot more information on how to get the best out of them.

Best

Charles

p.s. Multi tab projects - I'd dearly love to separate my code into different tabs for clarity - where can I find a reference to detail how?