onebutton.h libary instead of dip switches to recall .txt files from a micro sd

Hello every one

I have a small problem. I have working code to pull up .txt files from a mirco sd card using dip switches. But i want to use one button in stead. I found a github project "OneButton.h" GitHub - mathertel/OneButton: An Arduino library for using a single button for multiple purpose input.
that i think will do the trick but I don't know how to use it on my existing code.

This is what i have

+#include <SPI.h>
+#include <SD.h>
+#include <string.h>
+#include "Keyboard.h"
+
+File myFile;
+boolean first = true;
+
+void setup() {

  • String dip = ""; // Name of the file that will be opened
  • // Sets the given pins as switches for the dip switches
  • pinMode(6, INPUT_PULLUP);
  • pinMode(7, INPUT_PULLUP);
  • pinMode(8, INPUT_PULLUP);
  • pinMode(9, INPUT_PULLUP);
  • // Switches are checked, dip string is contructed
  • if (digitalRead(6) == LOW){dip += "1";} else {dip += "0";}
  • if (digitalRead(7) == LOW){dip += "1";} else {dip += "0";}
  • if (digitalRead(8) == LOW){dip += "1";} else {dip += "0";}
  • if (digitalRead(9) == LOW){dip += "1";} else {dip += "0";}
  • dip += ".txt";
  • if (!SD.begin(4)) {
  • return;
  • }
  • // Desired file is opened
  • myFile = SD.open(dip);
  • if (myFile) {

Can anyone help me?

With the oneButton library you can detect a short click, a long press, or a double-click. With those three options, how did you want to select each of your sixteen files?

I just want to access 3 files instead of 16 . Short click for 1.txt long press for 2.txt and double click for 3.txt.
If I can.
Thanks

Look at the library documentation and examples. Add and 'attach' functions for click and press.

Well I hate to be that guy but.... I attached functions I have something wrong. This is what I did

#include <SPI.h>
#include <SD.h>
#include <string.h>
#include "Keyboard.h"
#include "OneButton.h"

File myFile;
boolean first = true;

void setup() {
String button = "script.txt";

// Sets the given pins as switches for the button press
OneButton button(A1, true);
button.attachClick(click);
button.attachDoubleClick(doubleclick);
button.attachDuringLongPress(longPress);

// Switches are checked, dip string is contructed
if (digitalRead(A1) == click){button += "1";} else {button += "0";}
if (digitalRead(A1) == doubleclick){button += "2";} else {button += "0";}
if (digitalRead(A1) == press){button += "3";} else {button += "0";}

button += ".txt";

if (!SD.begin(A1)) {
return;
}

// Desired file is opened
myFile = SD.open(button);
if (myFile) {
Keyboard.begin();

String line = "";
while (myFile.available()) {
char m = myFile.read();
if (m == '\n'){
Line(line);
line = "";
}
else if((int) m != A1)
{
line += m;
}
}
Line(line);

myFile.close();
} else {
}

Keyboard.end();
}

I will keep trying but I am kinda hung up for now. Thank you for the help.

Look at File->Examples->OneButton->SimpleOneButton.

The "OneButton button(A1, true);" is NOT inside setup().

At the bottom they have declared a function they call doubleclick(). They pass that functions name to the function "button.attachDoubleClick()". That tells the library to call that function when someone double-clicks the button. In that doubleclick() function you should put the code to use "3.txt".

Create similar functions for 'click' and 'press'. Have click() use the file "1.txt" and press() use the file "2.txt".

Oh okay i see what you are saying now. Thank you for all your help. That should work.
have a good one John.

String button = "script.txt";

  // Sets the given pins as switches for the button press
  OneButton button(A1, true);

You can not have two variables with the same name, regardless of whether the types are the same, or not.

Having a String named button makes not a shred of sense.

Okay thank you jhonwasser for your help. I got the whole code working!!

it looks like this.

#include <SPI.h>
#include <SD.h>
#include <string.h>
#include "Keyboard.h"
#include "OneButton.h"
File myFile;
// Setup a new OneButton on pin A1.
OneButton button(A1, true);

String FILE_Name1 = "1.txt"; // Click
String FILE_Name2 = "2.txt"; // DoubleClick
String FILE_Name3 = "3.txt"; // Press

void setup() {

// link the doubleclick function to be called on a doubleclick event.
button.attachClick(click);
button.attachDoubleClick(doubleclick);
button.attachPress(press);

}

void Line(String l)
{
int space_1 = l.indexOf(" ");
if (space_1 == -1)
{
Press(l);
}
else if (l.substring(0, space_1) == "STRING")
{
Keyboard.print(l.substring(space_1 + 1));
}
else if (l.substring(0, space_1) == "DELAY")
{
int delaytime = l.substring(space_1 + 1).toInt();
delay(delaytime);
}
else if (l.substring(0, space_1) == "REM") {}
else
{
String remain = l;

while (remain.length() > 0)
{
int latest_space = remain.indexOf(" ");
if (latest_space == -1)
{
Press(remain);
remain = "";
}
else
{
Press(remain.substring(0, latest_space));
remain = remain.substring(latest_space + 1);
}
delay(5);
}
}

Keyboard.releaseAll();
}

void Press(String b)
{
if (b.length() == 1)
{
char c = b[0];
Keyboard.press(c);
}
else if (b.equals("ENTER"))
{
Keyboard.press(KEY_RETURN);
}
else if (b.equals("CTRL"))
{
Keyboard.press(KEY_LEFT_CTRL);
}
else if (b.equals("SHIFT"))
{
Keyboard.press(KEY_LEFT_SHIFT);
}
else if (b.equals("ALT"))
{
Keyboard.press(KEY_LEFT_ALT);
}
else if (b.equals("GUI"))
{
Keyboard.press(KEY_LEFT_GUI);
}
else if (b.equals("UP") || b.equals("UPARROW"))
{
Keyboard.press(KEY_UP_ARROW);
}
else if (b.equals("DOWN") || b.equals("DOWNARROW"))
{
Keyboard.press(KEY_DOWN_ARROW);
}
else if (b.equals("LEFT") || b.equals("LEFTARROW"))
{
Keyboard.press(KEY_LEFT_ARROW);
}
else if (b.equals("RIGHT") || b.equals("RIGHTARROW"))
{
Keyboard.press(KEY_RIGHT_ARROW);
}
else if (b.equals("DELETE"))
{
Keyboard.press(KEY_DELETE);
}
else if (b.equals("PAGEUP"))
{
Keyboard.press(KEY_PAGE_UP);
}
else if (b.equals("PAGEDOWN"))
{
Keyboard.press(KEY_PAGE_DOWN);
}
else if (b.equals("HOME"))
{
Keyboard.press(KEY_HOME);
}
else if (b.equals("ESC"))
{
Keyboard.press(KEY_ESC);
}
else if (b.equals("INSERT"))
{
Keyboard.press(KEY_INSERT);
}
else if (b.equals("TAB"))
{
Keyboard.press(KEY_TAB);
}
else if (b.equals("END"))
{
Keyboard.press(KEY_END);
}
else if (b.equals("CAPSLOCK"))
{
Keyboard.press(KEY_CAPS_LOCK);
}
else if (b.equals("F1"))
{
Keyboard.press(KEY_F1);
}
else if (b.equals("F2"))
{
Keyboard.press(KEY_F2);
}
else if (b.equals("F3"))
{
Keyboard.press(KEY_F3);
}
else if (b.equals("F4"))
{
Keyboard.press(KEY_F4);
}
else if (b.equals("F5"))
{
Keyboard.press(KEY_F5);
}
else if (b.equals("F6"))
{
Keyboard.press(KEY_F6);
}
else if (b.equals("F7"))
{
Keyboard.press(KEY_F7);
}
else if (b.equals("F8"))
{
Keyboard.press(KEY_F8);
}
else if (b.equals("F9"))
{
Keyboard.press(KEY_F9);
}
else if (b.equals("F10"))
{
Keyboard.press(KEY_F10);
}
else if (b.equals("F11"))
{
Keyboard.press(KEY_F11);
}
else if (b.equals("F12"))
{
Keyboard.press(KEY_F12);
}
else if (b.equals("SPACE"))
{
Keyboard.press(' ');
}
}

void loop() {
// keep watching the push button:
button.tick();
// You can implement other code in here or just wait a while
delay(10);
}

void doubleclick() {
if (!SD.begin(4)) {
return;
}

myFile = SD.open(FILE_Name2);
if (myFile) {
Keyboard.begin();

String line = "";
while (myFile.available()) {
char m = myFile.read();
if (m == '\n') {
Line(line);
line = "";
}
else if ((int) m != 13)
{
line += m;
}
}
Line(line);

myFile.close();
} else {
}

Keyboard.end();
}

void click() {
if (!SD.begin(4)) {
return;
}

myFile = SD.open(FILE_Name1);
if (myFile) {
Keyboard.begin();

String line = "";
while (myFile.available()) {
char m = myFile.read();
if (m == '\n') {
Line(line);
line = "";
}
else if ((int) m != 13)
{
line += m;
}
}
Line(line);

myFile.close();
} else {
}

Keyboard.end();
}

void press() {
if (!SD.begin(4)) {
return;
}

myFile = SD.open(FILE_Name3);
if (myFile) {
Keyboard.begin();

String line = "";
while (myFile.available()) {
char m = myFile.read();
if (m == '\n') {
Line(line);
line = "";
}
else if ((int) m != 13)
{
line += m;
}
}
Line(line);

myFile.close();
} else {
}

Keyboard.end();
}