1

I have the code given by my tutor and I need to change the code to be able enter a three digits number with the push button of switch module. Here is the code which can only enter one individual number.

#include "RL_Switch.h" // include Switch library 
Switch pushSwitch;      // create a Switch object called ‘pushSwitch’ 

int key = 0, inputflag = 0;

void setup() { 
  pushSwitch.begin(); //initialise Switch module 
  Serial.begin(57600); //initialise Serial Monitor 
}

void loop() { 
  
  /*assign to variable ‘pushKey’ the pressed button number, otherwise pushKey will be assigned -1 
  /* (Please refer to Switch module material).*/ 
  key = pushSwitch.getPushKey();
  //Serial.print("K = ");
  //Serial.println(key);
  
  if(key <= -1) inputflag = 0;
  else {
    if(inputflag == 0) {
      inputflag = 1;
    }
    else {
      key = -1;   
    }
  }

  //Serial.print("K = ");
  //Serial.println(key);
  
  if(key >= 0) {
    // if pushKey is higher than -1 then a button is pressed 
    Serial.print("Pushed : "); // print out “Pushed : “ 
    Serial.println(key); // print out the pressed button number  
    delay(500);
  }

   delay(100);
      
}
3
  • 2
    What do you mean by “switch module”? Please provide a link to a description of this hardware, and a link to the library you are using. – Edgar Bonet Apr 14 at 17:13
  • I found this coursehero.com/file/39994457/T03pdf – Juraj Apr 14 at 18:27
  • 1
    @EdgarBonet it is just like what has found. – Leslie Apr 15 at 11:35

Browse other questions tagged or ask your own question.