Hello I have a method where when you press a button a string is compared with a substring to see if the string contains the substring. The problem is when I load in the string I call the check button press and substring comparing method. Which only ever happens once. I want to know how I can keep calling the method. It sounds simple but it isn't.

void loadPrograme(){
   file = SD.open("/classes/basic.txt", FILE_READ);
   Serial.print("Reading, Checking and Compiling \n\n\n");  

   for (int i = 0; i < arraysize; i++) { 
      while (file.available()) { 
         character23 = file.read(); 
         if (character23 == '\n') break; 
         if (character23 != '^') { 
            content += character23; 
      if (content != "") { 
         result[i] = content; 
         content = "";
   for (int i = 0; i <arraysize; i++) {
      if(result[i].substring(0,5) == "print"){

         if(result[i].substring(0,5) == "delay"){
            if(result[i].substring(0,11) == "buttonPress"){




void buttonMethod(String s){ 
   s = s.substring(s.indexOf('{') + 1);
   s = s.substring(0, s.lastIndexOf('}'));
   String btn =" ";
   String daction = " ";
   String indaction = " ";
   btn= s.substring(s.indexOf('"')+1 );
   btn= btn.substring(0, btn.lastIndexOf('"'));
   daction = s.substring(s.indexOf(',')+1);
   daction = daction.substring(0, daction.lastIndexOf('('));
   indaction = s.substring(s.indexOf('~')+1);
   indaction = indaction.substring(0, indaction.lastIndexOf('~'));
   int buttonpressed;
   if(btn == "BUTTON_PIN_ENTER"){
      buttonpressed = 2;
   }else if(btn == "BUTTON_PIN_LEFT"){
      buttonpressed = 3;
   }else if(btn == "BUTTON_PIN_RIGHT"){
      buttonpressed = 4;
   }else if(btn == "BUTTON_PIN_DOWN"){
      buttonpressed = 5;
   }else if(btn == "BUTTON_PIN_UP"){
      buttonpressed = 6;
   }else if(btn == "BUTTON_PIN_ACTION"){
      buttonpressed = 7;

   int btncheck = digitalRead(buttonpressed);
   if(btncheck == LOW){
      if(daction.substring(0,5) == "print"){

      }else if(daction.substring(0,5) == "delay"){


So basically it loads in the txt file into the string array and loops through it and reads each line. each line is executed. for example if it get the substring of buttonpressed it goes to the buttonmethod(). The problem is in the button method. I want to have the button method keep looping until the button is pressed but it only runs through that method once. How can I keep running threw this method until the button is pressed?

  • Fix code format. Feb 17, 2016 at 14:10
  • Help us out by telling us about the syntax and semantics for the script language. Feb 17, 2016 at 14:18
  • what do u mean? Feb 17, 2016 at 14:19
  • Do you want to keep calling the buttonMethod() or keep calling the digitalRead portion of the buttonMethod()?
    – Dave X
    Feb 17, 2016 at 14:31
  • the buttonmethod Feb 17, 2016 at 14:32

1 Answer 1


Testing for string equality with '==' is a mistake: https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c The '==' operator on strings will compare the pointers to the strings rather than their content and likely always fail the if() tests.

To loop and block on the last bit, if that is what you mean, you might try:

   int btncheck;
   while(btncheck = digitalRead(buttonpressed)){ // wait for button
   }  // blocked until btncheck == LOW
   if(strcmp(daction.substring(0,5), "print")){

This has the blocking on non-pressed button feature, but also has the same logic as the original. If the string equality tests do not match, it appears you want to do nothing and quickly pass on.

To loop buttonMethod(), put it in a loop:


If you want to condition on getting the button press that corresponds with the string, I'd make buttonMethod return a value and loop conditionally on that:

int buttonMethod(...
    int retVal = 0;

  if(btncheck == LOW){
     retVal = 1;

while(!buttonMethod(result[i]));  // wait for the button
  • Might need to debounce the button. Feb 17, 2016 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.