I am having a problem implementing a the digitalRead() function in arduino uno.I want digitalRead to return 0 when nothing is connected and return 1 when it is connected to +5 volt.But seems like digitalRead returns random values when it is not connected to anything. I have kind of solved the problem by a small piece of code to stabilize the value i'll attach the code here but if any other solution is avalable..Please help.
The function is as follows:-
bool getValue()
{
String k="";
if(digitalRead(3)==HIGH)
k="HIGH";
else
k="LOW";
bool f=true;
for(int i=0;i<1000;i++)
{
String z;
if(digitalRead(3)==HIGH)
z="HIGH";
else
z="LOW";
if(k!=z)
{
f=false;
break;
}
}
if(f==true)
return true;
else
return false;
}