Pages: [1]   Go Down
Author Topic: A strange problem with inputs  (Read 689 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: March 09, 2010, 05:12:07 am by jopiek » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12736
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tested some more. Connecting the pins to

Code:
 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.
« Last Edit: March 09, 2010, 06:31:28 am by jopiek » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 09, 2010, 06:05:41 pm by PaulS » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yups I agree, that is also how I teach it smiley-wink
Logged

Pages: [1]   Go Up
Jump to: