I want to know if what I have coded is good enough or if there is any simpler/faster/better/less size taking substitute.
I am trying to take input from push buttons and I also want to remove repeated input so I coded like it and saved in a variable key. I used internal pullup to eradicated random input from digital pins. I am in pre-graduation in computer applications and a beginner in electronics.
byte key=0; unsigned int st=millis(),ct=millis(); //st-starttime , ct-currenttime void setup() { delay(5000); //start prog after 5 sec pinMode(7, INPUT); pinMode(8, INPUT); pinMode(9, INPUT); byte i; for(i=7;i<=9;i++) digitalWrite(i, HIGH); //7,8,9 pins HIGH for internal pullup Serial.begin(9600); } void loop() { byte i; for(i=7;i<=9;i++) if(checkswitch(i)==1) //check which switch was last pressed { key=i; st=millis(); while(st>65000) //controlling limits of unsigned int st=st-65000; } ct=millis(); while(ct>65000) ct=ct-65000; //controlling limits of unsigned int if(ct>st+200) //Triggers only if 200 ms passed { //to not fill the Serial input if(key!=0) { Serial.println(key); key=0; //clear saved key } } } byte checkswitch(byte n) //Check button pressed with internal pullup { byte p=0; if(digitalRead(n)==LOW) p=1; //my best algo for if-else statement return p; }