In this post I show how to control the GPIO on a Raspberry Pi using a touchscreen.
This is a follow up on my previous post Programming for a Touchscreen on the Raspberry Pi
The TFT doesn’t come up too well in the above video. The picture below gives a better idea of how it looks. Click to enlarge
Link to the code;
https://github.com/mwilliams03/Pi-Touchscreen-basic.git
In the above code touchbuttons.c creates three buttons on the TFT which will be used to turn on/off three LEDs.
This can easily be changed to add more buttons.
touchbuttons.c also requires WiringPI and needs to be compiled with gcc -g -o buttonExample buttonExample.c -l wiringPi
When a touch event happens, the coordinates returned from the touchscreen are compared to the coordinates of the buttons.
If the touch event happens within one of the buttons, it raises or lowers the GPIO pin assigned to this button.
I have created variables to track the current state of each button and then used a timer to only allow a button state to change after 500ms. This allows plenty of time to lift your finger off the touchscreen before the touchscreen thinks it is another event happening.
A snippit of the code for one of the buttons;
//See if the results retuned by the touch event fall within the coordination.s of the button if((scaledX > buttonCords2[X] && scaledX < (buttonCords2[X]+buttonCords2[W])) && (scaledY > buttonCords2[Y] && scaledY < (buttonCords2[Y]+butt$ //Has 500ms passed since the last time this button was pressed? if (mymillis() - buttonTimer2 > 500) //Is the button currently on? If so, we need to turn it off. if(button2){ //Change color to a darker red createButton(buttonCords2[X],buttonCords2[Y],buttonCords2[W],buttonCords2[H],"RED",GREY,RED); //Change state to off button2= BUTTON_OFF; //Start the timer buttonTimer2 = mymillis(); //Turn the LED off digitalWrite(LED_RED,LOW); } else{ //The button must be off.. so we need to turn it on. createButton(buttonCords2[X],buttonCords2[Y],buttonCords2[W],buttonCords2[H],"RED",WHITE,LIGHT_RED); //Change state to on button2= BUTTON_ON; //Start the timer buttonTimer2 = mymillis(); //Turn the LED on digitalWrite(LED_RED,HIGH); }
[wp_ad_camp_3]
I am having difficulty running this program. When I execute the file, the buttons are briefly displayed and then they disappear. Instead the desktop begins to show through the button image created wherever the touchscreen is touched. I am running a JBtek Latest Version 3.5 ” inch TFT LCD 480×320 RGB Pixels Touch Screen Display Monitor For Raspberry Pi that I purchased on Amazon. I am using the supplied image from JBtek that has all of the drivers pre-installed. Any suggestions for troubleshooting would be greatly appreciated. This is for a class project that is due in the coming week so time is of the essence. Thanks!