Go Down

Topic: A strange problem with inputs (Read 766 times) previous topic - next topic

jopiek

Mar 09, 2010, 10:45 am Last Edit: Mar 09, 2010, 11:12 am by jopiek Reason: 1
I am connecting 4 buttons to an arduino pro

At this moment I use the analog inputs, but no matter what the same problem keeps appearing: one of the buttons will not be detected. I have used my multimeter to ensure that it functions well (it becomes low when I press it):

Below the code that I use:
Code: [Select]

int btnred, btnyel, btngrn, btnblu; // buttons
int pressed = LOW;

void setup() {
 // setting the pins
 btnred = 17;
 btnyel = 16;
 btnblu = 15;
 btngrn = 14;

 pinMode(btnred, INPUT);
 digitalWrite(btnred, HIGH);
 pinMode(btnyel, INPUT);
 digitalWrite(btnyel, HIGH);
 pinMode(btngrn, INPUT);
 digitalWrite(btngrn, HIGH);  
 pinMode(btnblu, INPUT);
 digitalWrite(btnblu, HIGH);

 //Serial.begin(9600);

}

void loop() {
 int redval, yelval, bluval, grnval;
 redval = digitalRead(btnred);
 yelval = digitalRead(btnyel);
 bluval = digitalRead(bluval);
 grnval = digitalRead(btngrn);
 if (redval == pressed) {
   lc.setDigit(0,3,1,false);
   //Serial.println("Yellow");
 } else {
   lc.setDigit(0,3,0,false);
 }
 if (bluval == pressed) {
   lc.setDigit(0,1,1,false);
   //Serial.println("Yellow");
 } else {
   lc.setDigit(0,1,0,false);
 }
 if (grnval == pressed) {
   lc.setDigit(0,0,1,false);
   //Serial.println("Yellow");
 } else {
   lc.setDigit(0,0,0,false);
 }
 if (yelval == pressed) {
   lc.setDigit(0,2,1,false);
   //Serial.println("Yellow");
 } else {
   lc.setDigit(0,2,0,false);
 }
 delay(200);
}


B.t.w. I also seem to get crap when I use the terminal. I have tred using other pins, etc etc, then the blue button might function but an other one will fail to report when it is pressed.

Coding Badly

I don't see where you've defined "pressed".  Is that something included with the Arduino libraries?

jopiek

I have added that (I had it in my code, but forgot to copy it here).

jopiek

#3
Mar 09, 2010, 12:23 pm Last Edit: Mar 09, 2010, 12:31 pm by jopiek Reason: 1
I have tested some more. Connecting the pins to

Code: [Select]

 btnred = 19;
 btnyel = 18;
 btnblu = 17;
 btngrn = 16;


gives the same problem. If I turn the cable around it turns out to be that the blue button is seen by the arduino and becomes low (as is supposed to happen).

Very strange isn't it, I can't make any sense out of it.

[update]
A duemilanove gives the same outcome.

PaulS

Quote
If I turn the cable around it turns out to be that the blue button is seen by the arduino and becomes low (as is supposed to happen).


Sounds like a hardware problem, to me, not a software problem.

jopiek

Well, maybe, but the strange thing is that it is the same on a different arduino and also that it is not button specific but arduino pin specific!

jopiek

13 years of programming and helping numerous students doesn't seem to help either. It was my software mistake. Some small mistakes cost a lot of headache.

bluval = digitalRead(bluval);

is the problem... it should have been:
bluval = digitalRead(btnblul);

PaulS

#7
Mar 10, 2010, 12:05 am Last Edit: Mar 10, 2010, 12:05 am by PaulS Reason: 1
Ooops. I always try to use different length names, and mixed case, to avoid stuff like that. I like the variable part of the name up front, too (bluPin, redPin, etc.). Then, the Pin's line up and the Vals line up, making it easier to spot stuff like that.

jopiek

Yups I agree, that is also how I teach it ;)

Go Up