Go Down

Topic: Multiple Button Circuit (Read 2204 times) previous topic - next topic

nullvoid

Hello everybody!

I try to build a circuit with 6 buttons which are connected to 6 digital pins. I tried to multiply this solution:
http://arduino.cc/en/Tutorial/Button

I connected every button to an own digital pin and connected it with a 10k resistor to the ground.

Here is my code:
Code: [Select]

int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
int button5 = 6;
int button6 = 7;

int state[6];

int val[] = {
 0,0,0,0,0,0};

void setup() {

 Serial.begin(9600);

 pinMode(button1, INPUT);
 pinMode(button2, INPUT);
 pinMode(button3, INPUT);
 pinMode(button4, INPUT);
 pinMode(button5, INPUT);
 pinMode(button6, INPUT);

}

void loop(){

  state[0] = digitalRead(button1);
  state[1] = digitalRead(button2);
  state[2] = digitalRead(button3);
  state[3] = digitalRead(button4);
  state[4] = digitalRead(button5);
  state[5] = digitalRead(button6);

  for (int i = 0; i < 6; i++) {
   if (state[i] == HIGH) {
     val[i] = 1;
   }
   else {
     val[i] = 0;
   }
 }
 
 Serial.println(val[0]);
 Serial.println(val[1]);
 Serial.println(val[2]);
 Serial.println(val[3]);
 Serial.println(val[4]);
 Serial.println(val[5]);
 
   delay(100);
}


It gives my this compilingerror:

Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
     at gnu.io.RXTXPort.nativeavailable(Native Method)
     at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
     at processing.app.Serial.serialEvent(Serial.java:215)
     at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
     at gnu.io.RXTXPort.eventLoop(Native Method)
     at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

Is the code the problem or the circuit?


PaulS

I pasted that code into the IDE. It compiled just fine, so, it is not the code or the circuit.

When does this error appear? When you compile? Or, when you try to upload the code?

If it appears when you try to upload the code, then the problem is that the serial port selected under the Tools menu is not the one that the Arduino is connected to.

nullvoid

I could upload the code to the board so ive chosen the right usb port.
I think the problem is with the curcuit of the 6 buttons i made.

Any ideas?

PaulS

Quote
Any ideas?

One.

Please answer all the questions that are asked.

Specifically:
Quote
When does this error appear?


You seem to be able to upload the code to the board. So, something must happen on the Arduino to trigger the IDE to throw an exception. That seems very unlikely. It is like someone else's computer on the network causing my browser to throw an exception.

If, on the other hand, that exception is thrown when you unplug the Arduino with the Serial Monitor window open, then that is understandable.

nullvoid

The error appears when i press any button of my circuit.

It does NOT appear when i compile or upload the code.

I hope this answers your questions.

PaulS

It does not appear that the serial communication has anything to do with whether you press a button, or not.

If there is serial output before you press a button, and the serial output stops, and the exception is thrown, when you press a button, then you have a hardware problem.

How do you have the switches wired?

Grumpy_Mike

#6
Jul 22, 2010, 01:41 pm Last Edit: Jul 22, 2010, 01:42 pm by Grumpy_Mike Reason: 1
Quote
I connected every button to an own digital pin and connected it with a 10k resistor to the ground.

I don't think you have. I think this is wrong and you are shorting out the supply when you press a button and that is giving you the error.
The diagram you are using is easy to get wrong if the button is rotated by 90 degrees.

Also there is no need to go through the conversion from state[] to val[] these are the same things.

nullvoid

Thanks Mike. That was the problem! i connected the button, the resistor and the ground on a "straight line", thus overriding the resistor and producing a short curcuit.

nullvoid

Ok guys. Now everything is compiling nicely.

But the Arduino sends awkward values.

Again i just mutliplied this solution to get signals from 6 buttons:
http://arduino.cc/en/Tutorial/Button

This time i used this code:
Code: [Select]

const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;

int buttonState1 = 0;    
int buttonState2 = 0;  
int buttonState3 = 0;  
int buttonState4 = 0;  
int buttonState5 = 0;  
int buttonState6 = 0;  

void setup() {

Serial.begin(9600);

pinMode(button1, INPUT);
pinMode(button2, INPUT);  
pinMode(button3, INPUT);  
pinMode(button4, INPUT);  
pinMode(button5, INPUT);  
pinMode(button6, INPUT);  
}

void loop(){

 buttonState1 = digitalRead(button1);
 buttonState2 = digitalRead(button2);
 buttonState3 = digitalRead(button3);
 buttonState4 = digitalRead(button4);
 buttonState5 = digitalRead(button5);
 buttonState6 = digitalRead(button6);

 Serial.print("!");
 Serial.print(buttonState1);
 Serial.print(buttonState2);
 Serial.print(buttonState3);
 Serial.print(buttonState4);
 Serial.print(buttonState5);
 Serial.print(buttonState6);
}


It randomly produces values of 1 and 0 even if i dont press any button. And when i press a button its all the same: random values all over the place.

Is the code wrong? Basically its also just a multiplication of the code provided in this tutorial: http://arduino.cc/en/Tutorial/Button

Or is something wrong with the circuit? Maybe there are different voltages when i try to press and operate six buttons with one energy source?

Erika86

Sorry for the ignorance..I'm trying to duplicate the circuit of this page http://www.arduino.cc/en/Tutorial/Button but I don't know how to connect the wires in the right way. Anyone has an image?? :'(

Graynomad

Much as I hate those Fritzing drawings this page also has a psuedo schematic. I really don't think it can be made any clearer.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Erika86

In fact doing just the first one is not a problem, all works fine! The problem is when I have to add the second button on the circuit. I don't know how to wire the second button.

const int BUTTON = 7;     // the number of the pushbutton pin
const int LED =  9;      // the number of the LED pin

int i = 0;
int state = 0;



//A function to see if the button has been pressed, returns an int
int CheckButton()
{
 return(digitalRead(BUTTON) == HIGH);
 
}

void setup()
{
 pinMode(LED, OUTPUT);
 pinMode(BUTTON, INPUT);
}

void loop()
{  
 
   //Check to see if the button has been pressed.
   if(digitalRead(BUTTON) == HIGH)
   {
     //While the digital read statement is true (high) continue the loop
     while(digitalRead(BUTTON) == HIGH)
           continue;

     //state is true as state is 0 and if we do !state then it is 1
     state = !state;
   }
   
   //If state is 1 which it will be as we wont make it 0. If we dont use state then we have to keep pressing the button
   if(state)
   {
         for (i = 0; i < 255; i++)

           {
          //If checkbutton is true it means the button has been pressed
       if(CheckButton())
       {
           //Turn off the LED and break the loop
           
         analogWrite(LED, 0);
           break;
         
       }

         //Continue fading
         
         analogWrite(LED, i);
         delay(10);
     
           }
 
           for (i = 255; i > 0; i--)
           {
         //If the checkbutton is true it means the button has been pressed
         if(CheckButton())
         {
           //Turn off the LED and break the loop
           
           analogWrite(LED, 0);
           break;
         
       }

         //Continue fading
       
         analogWrite(LED, i);
         delay(10);
         
           }
    }

}



that's my code and with it, by pressing the button once, the led starts to fade in and out, when I press the button again the led turns off.
What I wanna do is add another button and try give to the led different input.
I know maybe for you as expert is a stupind and nonsense thing but try to think as a complete ignorant as I am!!

arbarnhart

#12
Jan 18, 2011, 05:03 pm Last Edit: Jan 18, 2011, 05:04 pm by arbarnhart Reason: 1
Something of an aside, though it may help the short go away...

I use multiple buttons in a couple of my projects. After I set the digital pins to input, I write HIGH to them to use the internal pull up resistor, so I don't have to wire a resistor. I wire from one side of each button to the corresponding pin and from the other to GND. Fewer wires to run (I daisy chain GND between them) plus no parts to add besides the buttons and wire equals a much smaller chance I will screw something up. this does mean my software looks counter intuitive, checking for HIGH as off and LOW as on, but that is no big deal.

Graynomad

#13
Jan 18, 2011, 05:23 pm Last Edit: Jan 18, 2011, 05:25 pm by graynomad Reason: 1
I haven't got time to analyse your code right now, but I can give you a hint or two

Code: [Select]
const int BUTTON1 = 7;
const int BUTTON2 = 8;      // the number of the pushbutton pin
const int LED =  9;      // the number of the LED pin

int i = 0;
int state = 0;



//A function to see if the button has been pressed, returns an int
int CheckButton(int button)
{
return(digitalRead(button) == HIGH);

}

void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);

}

//
//
//
//

if(CheckButton(BUTTON1) || CheckButton(BUTTON2))
//
//
//
//
// or maybe
if(CheckButton(BUTTON1))
    do X with LED

if (CheckButton(BUTTON2))
   do Y with LED




Note also the use of the code button (# at the top of the window when editing your post) to make the code more readable.

Also your comments need some work

Code: [Select]
//While the digital read statement is true (high) continue the loop
    while(digitalRead(BUTTON) == HIGH)


We can see that, the comment is little different to the code.

Code: [Select]
// Wait for button to be released
    while(digitalRead(BUTTON) == HIGH)


Is better.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Erika86

Many thanks to both of you. I'll try to do some changes...

Go Up