Go Down

### Topic: [Student] Having problems with figuring out getting button to work with build (Read 1 time)previous topic - next topic

#### zane203

So I'm new (to the forums, and the study of robotics), and am working on writing a code that adds count to a variable that will in turn change the display of a 7-segment display based on 4 buttons each representing a bit in a 4-bit setup

so here's what I have so far:

Code: [Select]
`// Zane// 7-Segment binary button// set upvoid setup(){  // 7-segment dispaly  pinMode(2, OUTPUT);   pinMode(3, OUTPUT);  pinMode(4, OUTPUT);  pinMode(5, OUTPUT);  pinMode(6, OUTPUT);  pinMode(7, OUTPUT);  pinMode(8, OUTPUT);  pinMode(9, OUTPUT);    // buttons  pinMode(10, INPUT);  pinMode(11, INPUT);  pinMode(12, INPUT);  pinMode(13, INPUT);    digitalWrite(9, 1);  // start with the "dot" off}void loop(){  // set each binary button to 0 for 0  int binary_3 = 0;  if (binary_3 > 1)  {    binary_3 =0  }  int binary_2 = 0;  if (binary_2 > 1)  {    binary_2 =0  }  int binary_1 = 0;  if (binary_1 > 1)  {    binary_1 =0  }  int binary_0 = 0;  if (binary_0 > 1)  {    binary_0 =0  }  // =====================================//  Button Press area.//  Press binary button and adds 1//  since conditions set before, if the button goes over 1, reset//    // ======================================  // Create light up sequence  // Zero  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 0);   digitalWrite(8, 1);   delay(1000);  }    // One  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 1)  {   digitalWrite(2, 1);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 1);   delay(1000);   }    // Two  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 1);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 1);   digitalWrite(8, 0);   delay(1000);  }    // Three  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 0);   delay(1000);  }    // Four  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 0)  {   digitalWrite(2, 1);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Five  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 1);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 1);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Six  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 1);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Seven  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 1);   delay(1000);      }      // Eight   if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)   {     digitalWrite(2, 0);     digitalWrite(3, 0);     digitalWrite(4, 0);     digitalWrite(5, 0);     digitalWrite(6, 0);     digitalWrite(7, 0);     digitalWrite(8, 0);     delay(1000);   }      // Nine   if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)   {     digitalWrite(2, 0);     digitalWrite(3, 0);     digitalWrite(4, 0);     digitalWrite(5, 1);     digitalWrite(6, 1);     digitalWrite(7, 0);     digitalWrite(8, 0);     delay(1000);   }    }`

I do know that I'm going to need to have a delay after the button press for debouncing, but how am I making sure the button (which is set as active high) to read being pressed in the right time?

#### TECman

#1
##### Feb 23, 2012, 01:35 amLast Edit: Feb 23, 2012, 02:17 am by TECman Reason: 1
Hey Zane!  I will try to help you out a bit with the simple stuff.

Quote

Should be -
Code: [Select]
`digitalWrite(9, LOW);  // start with the "dot" off`

Quote
binary_3 =0

Should be -
Code: [Select]
`binary_3 = 0;`

Quote
binary_2 =0

Should be -
Code: [Select]
` binary_2 = 0;`

Quote
binary_1 =0

Should be -
Code: [Select]
`binary_1 = 0;`

Quote
// Create light up sequence
// Zero
if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
delay(1000);
}

// One
if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 1)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
}

// Two
if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 0)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 0);
delay(1000);
}

// Three
if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 1)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
delay(1000);
}

// Four
if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 0)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
}

// Five
if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 1)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
}

// Six
if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 0)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
}

// Seven
if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 1)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);

}

// Eight
if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
}

// Nine
if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
}

It should be:
Code: [Select]
`  // Create light up sequence  // Zero  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 0);   digitalWrite(8, 1);   delay(1000);  }    // One  else if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 1)  {   digitalWrite(2, 1);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 1);   delay(1000);  }    // Two  else if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 1);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 1);   digitalWrite(8, 0);   delay(1000);  }    // Three  else if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 0);   delay(1000);  }    // Four  else if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 0)  {   digitalWrite(2, 1);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Five  else if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 1);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 1);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Six  else if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 1);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Seven  else if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 1);   delay(1000);     }     // Eight   else if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)   {     digitalWrite(2, 0);     digitalWrite(3, 0);     digitalWrite(4, 0);     digitalWrite(5, 0);     digitalWrite(6, 0);     digitalWrite(7, 0);     digitalWrite(8, 0);     delay(1000);   }     // Nine   else if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)   {     digitalWrite(2, 0);     digitalWrite(3, 0);     digitalWrite(4, 0);     digitalWrite(5, 1);     digitalWrite(6, 1);     digitalWrite(7, 0);     digitalWrite(8, 0);     delay(1000);   }`

Now you can't have 1 or 0 if it is a digitalWrite. It needs to be HIGH or LOW so you need to change that!

Hope this helps!

#### zane203

Thanks for the reply, I'm far from finished with this, but when I tested with digitalWrite(#, 1) and etc, they work the same as low and high.

I'm more curious to how am I to go about getting the buttons to work.

as to why it says 0 for on, I do not know, or maybe it's just the display I'm using, why but having it HIGH causes the lights to be off.

but I still need clearance on how I would go about having the button input added.

#### pYro_65

#3
##### Feb 23, 2012, 03:39 amLast Edit: Feb 23, 2012, 03:41 am by pYro_65 Reason: 1
When the arduino due comes out for example, the digital pins have more modes than just on/off on the ARM processor. So the values of HIGH and LOW could change to something arbitrary, so when you explicitly put in 1 & 0 you prevent your code from being compatible with possible future releases of the Arduino core.

Forum Mod anyone?
https://arduino.land/Moduino/

#### AWOL

#4
##### Feb 24, 2012, 06:52 pmLast Edit: Feb 24, 2012, 06:58 pm by AWOL Reason: 1
Hey TECman! I will try to help you out a bit with the simple stuff.
Quote
Should be -

Code:

If you look carefully at the code, you'll recognize that the display is a common anode device, so it is quite correct to write a 1 to turn the segment off.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### zane203

Hello guys, I've done a bit of updating on the code, and everything works fine except for the buttons x.x

there are four buttons,
the 7-segment display lights up when they are LOW.

I have each set of display sequences set to a pattern represented by the binary variables.

When I press one of the four buttons, the following binary number represented by said button (I.E. button 3 goes to binary_3 - which is in the order "3 2 1 0") should increase by "1" followed by a delay for debounce.

when the button is pressed again, it adds 1 (making 2) but due to prior coding in the loop, when the number is > 1 it resets to 0.

but for some reason, the buttons aren't working, and all that will display on the segment is "0".

Here's the new code:
Code: [Select]
`// Zane// 7-Segment binary button// set up// buttonsint binary_button_3 = 10;int binary_button_2 = 11;int binary_button_1 = 12;int binary_button_0 = 13;// binary dataint binary_3 = 0;int binary_2 = 0;int binary_1 = 0;int binary_0 = 0;  void setup(){  // 7-segment dispaly  pinMode(2, OUTPUT);   pinMode(3, OUTPUT);  pinMode(4, OUTPUT);  pinMode(5, OUTPUT);  pinMode(6, OUTPUT);  pinMode(7, OUTPUT);  pinMode(8, OUTPUT);  pinMode(9, OUTPUT);       pinMode(binary_button_3, INPUT);  pinMode(binary_button_2, INPUT);  pinMode(binary_button_1, INPUT);  pinMode(binary_button_0, INPUT);    digitalWrite(9, 1);  // start with the "dot" off}void loop(){  // set each binary button to 0 for 0  if (binary_3 > 1)  {    binary_3 =0;  }  if (binary_2 > 1)  {    binary_2 =0;  }  if (binary_1 > 1)  {    binary_1 =0;  }  if (binary_0 > 1)  {    binary_0 =0;  }  // =====================================//  Button Press area.//  Press binary button and adds 1//  since conditions set before, if the button goes over 1, reset  digitalRead(binary_button_3);  digitalRead(binary_button_2);  digitalRead(binary_button_1);  digitalRead(binary_button_0);     if(binary_button_3 == HIGH)  {    binary_3 += 1;    delay(100); // debounce  }  if(binary_button_2 == HIGH)  {    binary_2 += 1;    delay(100); // debounce  }  if(binary_button_1 == HIGH)  {    binary_1 += 1;    delay(100); // debounce  }  if(binary_button_0 == HIGH)  {    binary_0 += 1;    delay(100); // debounce  }      // ======================================  // Create light up sequence  // Zero  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 0);   digitalWrite(8, 1);   delay(1000);  }    // One  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 0 && binary_0 == 1)  {   digitalWrite(2, 1);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 1);   delay(1000);   }    // Two  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 1);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 1);   digitalWrite(8, 0);   delay(1000);  }    // Three  if (binary_3 == 0 && binary_2 == 0 && binary_1 == 1 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 0);   delay(1000);  }    // Four  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 0)  {   digitalWrite(2, 1);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Five  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 0 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 1);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 1);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Six  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 0)  {   digitalWrite(2, 0);   digitalWrite(3, 1);   digitalWrite(4, 0);   digitalWrite(5, 0);   digitalWrite(6, 0);   digitalWrite(7, 0);   digitalWrite(8, 0);   delay(1000);  }    // Seven  if (binary_3 == 0 && binary_2 == 1 && binary_1 == 1 && binary_0 == 1)  {   digitalWrite(2, 0);   digitalWrite(3, 0);   digitalWrite(4, 0);   digitalWrite(5, 1);   digitalWrite(6, 1);   digitalWrite(7, 1);   digitalWrite(8, 1);   delay(1000);      }      // Eight   if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)   {     digitalWrite(2, 0);     digitalWrite(3, 0);     digitalWrite(4, 0);     digitalWrite(5, 0);     digitalWrite(6, 0);     digitalWrite(7, 0);     digitalWrite(8, 0);     delay(1000);   }      // Nine   if (binary_3 == 1 && binary_2 == 0 && binary_1 == 0 && binary_0 == 0)   {     digitalWrite(2, 0);     digitalWrite(3, 0);     digitalWrite(4, 0);     digitalWrite(5, 1);     digitalWrite(6, 1);     digitalWrite(7, 0);     digitalWrite(8, 0);     delay(1000);   }    }`

#### Sacman

I have read a post in the past about how quickly a main loop cycles but I don't remember how to calculate it. However, what I think is happening is that when you press a button, it is counting faster than you think so it is counting to two before you can release your button and going back to 0. The problem is compounded by the fact that you have not truly debounced your button in the code. That after each button state check is not going to do it since you can get bounce when you press the button as well as when you release.

I moved your zero reset. This cleans the code up a bit.

Code: [Select]
`boolean lastbutton0 = LOW;boolean currentbutton0 = HIGH;boolean lastbutton1 = LOW;boolean currentbutton1 = HIGH;boolean lastbutton2 = LOW;boolean currentbutton2 = HIGH;boolean lastbutton3 = LOW;boolean currentbutton3 = HIGH;void loop(){ //Debounce button  currentbutton0 = debounce(lastbutton0,binary_button_0);  if (lastbutton0 == LOW && currentbutton0 == HIGH)   {  binary_0++;  if (binary_0 > 1)   {   binary_0 = 0;  }  } //Debounce button  currentbutton1 = debounce(lastbutton1,binary_button_1);  if (lastbutton1 == LOW && currentbutton1 == HIGH)   {  binary_1++;  if (binary_1 > 1)   {   binary_1 = 0;  }  } //Debounce button  currentbutton2 = debounce(lastbutton2,binary_button_2);  if (lastbutton2 == LOW && currentbutton2 == HIGH)   {  binary_2++;  if (binary_2 > 1)   {   binary_2 = 0;  }  } //Debounce button  currentbutton3 = debounce(lastbutton3,binary_button_3);  if (lastbutton3 == LOW && currentbutton3 == HIGH)   {  binary_3++;  if (binary_3 > 1)   {   binary_3 = 0;  }  }}//Debounce Routineboolean debounce(boolean last, int buttonpin){  boolean current = digitalRead(buttonpin);  if (last != current)   {    delay(50);    current = digitalRead(buttonpin);  }  return current;}`

#### PaulS

Quote
but for some reason, the buttons aren't working, and all that will display on the segment is "0".

So, are we chasing a hardware problem or a software problem? You are not enabling the internal pullup resistors. Therefore, you must have external pullup or pulldown resistors. Since the test is for HIGH to mean the the switch is pressed, this implies that the switch is wired in a pulldown configuration.

So, exactly how are your switches wired?

#### zane203

I have four buttons, each connected to a resistor (not sure what makes it a pull up, over a pull down.) and each button is connected to a pin on the board.

I can take a picture if you'd like.

I was thinking that the problem was software side.

#### retrolefty

I have four buttons, each connected to a resistor (not sure what makes it a pull up, over a pull down.) and each button is connected to a pin on the board.

I can take a picture if you'd like.

I was thinking that the problem was software side.

Yes a clear picture, or better yet a schematic drawing, even if hand drawn and photographed. I suspect you don't have the resistors wired correctly. Also the value of the resistors would be helpful to know.

Lefty

#### WizenedEE

That's not quite correct. You need to have the arduino pin connected through the resistor to ground AND the button. The other side of the button should be connected to Vcc (5V)

Code: [Select]
`PIN --- resistor --- ground     |     |  button --- 5V`

The resistor should be "gently pulling" the pin to ground when the button is not pressed. When the button gets pressed, then it "strongly pulls" the pin to 5V, "overpowering" the resistor

#### zane203

I've redesigned the buttons and inputs, and now It's constantly showing "8"...

I've tried moving a lot of things around, but I'm starting to think that I'm just not getting the hang of this stuff =/

I want to thank you guys for all your help but I think I might either just change majors or possibly transfer to a school that moves slower and can teach me in smaller steps and more hands on...

#### AWOL

The simplest way to debug a program like this is to get it to talk to you.