Help me With My Code!

I'm trying to make a program that detects how many out of four buttons are pressed, and display that number on a 7 segment display, but the display shows "8" now matter how many buttons are pressed, even if none are. I have the pins of the display connected to Arduino pins 2-8, and the buttons connected to Arduino pins 9-12. The buttons are getting power from the 5V pin on the Arduino, and the display is using GND. Here is the code:

/*
  Sketch Author: xxxxxxxx
  Date Started: 2/12/2011
  Date Finished: 2/13/2011
  
  Desc: This sketch detects how many (not which) buttons are 
  pressed, and displays that number on the 7 segment display.
*/


//define pin numbers
//7 segment display
const int a = 2;
const int b = 3;
const int c = 4;
const int d = 5;
const int e = 6;
const int f = 7;
const int g = 8;
//buttons
const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;

//define varables
int b1State = 0;
int b2State = 0;
int b3State = 0;
int b4State = 0;
int numPressed = 0;

void setup()
{
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
}

void loop()
{
  if (digitalRead(b1) == HIGH) //find out if button 1 is pressed
  {
    b1State = 1;
  }
  else
  {
    b1State = 0;
  }
  
  if (digitalRead(b2) == HIGH) //find out if button 2 is pressed
  {
    b2State = 1;
  }
  else
  {
    b2State = 0;
  }
  
  if (digitalRead(b3) == HIGH) //find out if button 3 is pressed
  {
    b3State = 1;
  }
  else
  {
    b3State = 0;
  }
  
  if (digitalRead(b4) == HIGH) //find out if button 4 is pressed
  {
    b4State = 1;
  }
  else
  {
    b4State = 0;
  }
  
  numPressed = b1State + b2State + b3State + b4State; //add up how many buttons are pressed
  
  switch (numPressed)
  {
    case 1: //displays the number 1
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
    case 2: //displays the number 2
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
    case 3: //displays the number 3
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
    case 4: //displays the number 4
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
    default: //displays the number 0
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, LOW);
  }
}

you need a break; between the cases. http://www.arduino.cc/en/Reference/SwitchCase

AlphaBeta: you need a break; between the cases.

Thanks! That fixed my old problem, but now when I start, it shows nothing (insetad of 0) and only buttons 2 and 4 react. When I press 2, it shows a 9, and when I press 4, it shows something like this : | |. Any ideas how to fix it? I have a feeling it's breaking either when it adds all the states together, or when it tries to change the display. EDIT: As I was writing that, the display changed to something that looks like this, but rotated 90 degrees clockwise: O | EDIT 2: At the start of the program, it shows nothing for a while, then shows 9.

How did you wire the switches? Do they have a pulldown resistor to ground?

AlphaBeta: How did you wire the switches? Do they have a pulldown resistor to ground?

No... I'll try that. What resistance should I use?

:) http://www.arduino.cc/en/Tutorial/Button

 if (digitalRead(b1) == HIGH) //find out if button 1 is pressed
  {
    b1State = 1;}
  else
  {
    b1State = 0;
  }
b1State = digitalRead(b1) ;

AlphaBeta: :) http://www.arduino.cc/en/Tutorial/Button

That didn't work, it reacted exactly the same way.

AWOL:

 if (digitalRead(b1) == HIGH) //find out if button 1 is pressed

{
   b1State = 1;}
 else
 {
   b1State = 0;
 }






b1State = digitalRead(b1) ;

Wait, isn’t digitalRead either HIGH or LOW?
EDIT: I tried it, and it reacted the same.

Yo RoboGeekDude12, a small tip for you: make your signature shorter in vertical space :) Some of us browse the site on devices with low resolution and the scrolling becomes a bit tedious.

Great job on clearly explaining what you want to do, what happens and what you want to happen. And, congratulations on getting into this field at your age! Really hope you'll enjoy it and stick around to this forum.

Robots are awesome! :)

AlphaBeta: Yo RoboGeekDude12, a small tip for you: make your signature shorter in vertical space :) Some of us browse the site on devices with low resolution and the scrolling becomes a bit tedious.

Great job on clearly explaining what you want to do, what happens and what you want to happen. And, congratulations on getting into this field at your age! Really hope you'll enjoy it and stick around to this forum.

Robots are awesome! :)

Thanks! People are always so amazed when they learn that I'm only almost 13 :P

Wait, isn't digitalRead either HIGH or LOW

Yes it is, and what are the values of HIGH and LOW? Smaller code means the bugs have fewer dark corners to hide in. Next: arrays!

AWOL:

Wait, isn’t digitalRead either HIGH or LOW

Yes it is, and what are the values of HIGH and LOW?
Smaller code means the bugs have fewer dark corners to hide in.
Next: arrays!

Hmm… Good point!:stuck_out_tongue: I thought that it would be boolean. Thanks for the help! Do you think you could help me work out the rest of the bugs?

If you post your code again, as is now and remove your signature I'll help ]:)

AlphaBeta: If you post your code again, as is now and remove your signature I'll help ]:)

Ok, here you go. I made my signiature a lot shorter :)

/*
  Sketch Author: John Michael Gross
  Date Started: 2/12/2011
  Date Finished: 2/13/2011
  
  Desc: This sketch detects how many (not which) buttons are 
  pressed, and displays that number on the 7 segment display.
*/


//define pin numbers
//7 segment display
const int a = 2;
const int b = 3;
const int c = 4;
const int d = 5;
const int e = 6;
const int f = 7;
const int g = 8;
//buttons
const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;

//define varables
int b1State = 0;
int b2State = 0;
int b3State = 0;
int b4State = 0;
int numPressed = 0;

void setup()
{
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
}

void loop()
{
  b1State = digitalRead(b1);
  b2State = digitalRead(b2);
  b3State = digitalRead(b3);
  b4State = digitalRead(b4);
  
  numPressed = b1State + b2State + b3State + b4State; //add up how many buttons are pressed
  
  switch (numPressed)
  {
    case 1: //displays the number 1
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;
    case 2: //displays the number 2
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
      break;
    case 3: //displays the number 3
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
      break;
    case 4: //displays the number 4
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;
    default: //displays the number 0
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, LOW);
      break;
  }
}

You code is sound. Correct logic and syntax. Sure this is not an hardware bug?

I don't have an arduino nearby or else I would test it for you.

How does that code behave? What is wrong?

AlphaBeta: You code is sound. Correct logic and syntax. Sure this is not an hardware bug?

I don't have an arduino nearby or else I would test it for you.

How does that code behave? What is wrong?

It might be a power problem. The 7 segment display shows random gibberish, and when I press a button, it changes to different gibberish, but each button makes the same gibberish every time. After a few seconds, the display changes to more random gibberish. Gibberish = random segments lit up. I'm using no resistors. Do you think the Arduino might be underpowered? I'm using a plugged-in laptop with another USB device (mouse) and an external monitor. Both monitors are at almost full brightness (although the monitors probably aren't the problem).

You need one resistor for each led, aroudn 330 and you'll probably be safe. The same gibberish is good, try the resistors and report back :)

AlphaBeta:
You need one resistor for each led, aroudn 330 and you’ll probably be safe.
The same gibberish is good, try the resistors and report back :slight_smile:

I don’t have any of those, and not enough of any close to it. I guess I’ll have to take a trip to RadioShack. Wait, couldn’t I just use one on the ground wire?

AlphaBeta: You need one resistor for each led, aroudn 330 and you'll probably be safe. The same gibberish is good, try the resistors and report back :)

Ok, I put a 680 ohm resistor on the ground wire, but all it did was make the display dimmer, it still shows the same pattern of gibberish.