hi, for my project i have some home made contact switches. These contacts work pretty well, and in some parts where electricity shouldn't be conducted, i use ductape to isolate. The problem is, sometimes even though the contact is on the ductape, i get a 1 instead of a 0 using digitalRead. I really need this to work. i tried with the contact being completely off, and anything i do, i always get a 1 instead of a 0 on many pins(some work at times). What can i do, so the whenever the contact conected to the pin isn't getting electricity, it goes 0?
try using a several thousand ohm resistor from your digital input to ground. or if you want to do it without any extra parts, hook the switch to connect to ground and enable the internal pull up resistors. an open contact will read 1, and closed will read 0.
Sounds like you might be fighting 'floating input pin' condition. Search on pull-up and pull-down resistors and why they are needed for reading simple switch contacts.
So, i tried using theinternal pull up resistors, and conecting it to ground. It worked for 2 switches, but when i added 2 more, it completely failed.
I will use 4 sets of 2 switches each( a total of 8 switches).
I tried exactly this same thing with the analog read, and it worked way better, i found a barrier betwen on and off, and used it as 0 1 also. The only problem with this, is that there are only 8 pins, and there are only 6 analog pins... I guess i could create a simple circuit or each set of 2 switches, and have the combination of 1s and 0s in one single analog pin, the problem is i don't have the slightest idea how to do this...
I would try the resistors idea, but i have no way of getting them at least until monday...
There are 19 IO pins available, where did you lose 5 to? D0 thru D13 and D14-D19 (which are the analog pins, but can be used as digital pins also). I would keep D0, D1 free to allow debugging via comms over the serial port to the monitor.
Post your code, lets see what you have.
All the pins have internal pullups, to enable them:
pinMode(pinx, INPUT);
digitalWrite(pinx, HIGH);
now that pin will read as 1 unless pulled to ground.
So, you don't need any resistors, you just have to think of 0 as the pressed condition instead of 1.
* delay(250); // waits 15ms for the servo to reach the position* } int GetAngle(byte IClaw) { * // 00=1 01=2 11=3 10=4* * a=digitalRead(Pin1[IClaw]);* * b=digitalRead(Pin2[IClaw]);*
* if (a==0) return (b+1);* * else return (4-b);* * } //End GetAngle* That's my code. The circuit is simple: from arduino 5V to every contact positive pole, and from each negative pole of each switch to a different pin.
are getting set as inputs here. Maybe if you had Pin1 [ i ], Pin2 [ i ] ??
for (byte i=0; i < 2; i++){
pinMode(Pin1, INPUT);
pinMode(Pin2, INPUT);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);}
This is like Switch B in the example schematic I posted?
"The circuit is simple: from arduino 5V to every contact positive pole, and from each negative pole of each switch to a different pin."
If so, you need pull down resistors to make it read low when the switch is not pressed. If you wire like Switch A instead, that is the positive pole to an input pin and the negative to ground, the internal pullups will work. Just need to change your logic here to match that:
int GetAngle(byte IClaw) {
// 00=1 01=2 11=3 10=4
a=digitalRead(Pin1[IClaw]);
b=digitalRead(Pin2[IClaw]);
if (a==0) return (b+1);
else return (4-b);
} //End GetAngle
//End for