The Hello World! in the Arduino universe is to blink a LED. Often it can be turned on and off with a push putton. Nowadays we use touch gestures instead of physical buttons, which implies the question:
Can the button press be emulated with a touch gesture on Arduino?
Photoresistor
The answer is yes! The physical button can be replaced with a photoresistor, which detects light intensity. The intensity change will emulate the touch - button press - gesture.
Can something more advanced be built to demonstrate this idea?
Prototype
The answer is another yes. For instance it would be really cool navigate in menu on a screen with these “buttons”!
First of all the number of the LEDs and the photoresistors have to be tripled. If you work with an Arduino Nano board the end result might look like this:
In order to display a menu, a TFT LCD screen is required. I used the 1.8” Color TFT LCD display from Adafruit. It has to be wired together with the Arduino Nano board based on this page.
Sketch
Once the wiring is an Arduino sketch is required to make the prototype work. The following one can be used:
voidloop(){ // Divides input 0-1023 to resemble to 0-255 int photocellInputSelect = (analogRead(0)/4); int photocellInputUp = (analogRead(1)/4); int photocellInputDown = (analogRead(2)/4);
voidupButtonPressed(){ Serial.println("Up button pressed!"); if (selectedMenuItemIndex == 1) { // Position to the last menu item tftMenuSelect(MENU_SIZE); } else { // Move down the selection tftMenuSelect(selectedMenuItemIndex - 1); } }
if (selectedMenuItemIndex < MENU_SIZE) { // Move down the selection tftMenuSelect(selectedMenuItemIndex + 1); } else { // Position to the first menu item tftMenuSelect(1); } }
voidtftMenuInit(){ // Clear the screen and set text size tft.setTextWrap(false); tft.fillScreen(ST7735_BLACK); tft.setTextSize(1); // Display the header of the menu - the header is the first item tft.setCursor(0, 0); tft.setTextColor(ST7735_GREEN, ST7735_BLACK); tft.println(mainMenu[0]);
// Draw separation line tft.drawLine(0, 9, tft.width()-1, 9, ST7735_GREEN); // Display other menu items tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); for(int i=1; i<=MENU_SIZE; i++) { tft.setCursor(0, ((i-1)*10)+MENU_TOP); tft.println(mainMenu[i]); } }
voidtftMenuSelect(byte menuItemIndex){
// Remove the highlight from the currently selected menu item tft.setCursor(0, ((selectedMenuItemIndex-1)*10)+MENU_TOP); tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); tft.println(mainMenu[selectedMenuItemIndex]); // Highlight the new selected menu item tft.setCursor(0, ((menuItemIndex-1)*10)+MENU_TOP); tft.setTextColor(ST7735_YELLOW, currentBackgroundColor); tft.println(mainMenu[menuItemIndex]); // Save the selection selectedMenuItemIndex = menuItemIndex; }
Conclusion
After the sketch upload the prototype will look like this:
The sketch calculates the maximum sensed light value from the three photoresistors. If the difference is above a preset threshold, the right button click will be simulated.
It means the if all of the three values change together no button press will happen.
A small delay also had to be introduced in order to avoid multiple button presses in a small timeframe. This simulates better the behavior of a real life push button.