7 segment display

I have wired a servo to go back and forth. I want the 7 segment display to count up to 9 and once at 9 go back to zero. However, the display comes out choppy. I have checked that the 7 segment display is common anode.

#include <Servo.h>

Servo base;
Servo shoulder;

// Describe each digit in terms of display segments
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
const byte numbers[10] = {
                    0b11111100,
                    0b01100000,
                    0b11011010,
                    0b11110010,
                    0b01100110,
                    0b10110110,
                    0b10111110,
                    0b11100000,
                    0b11111110,
                    0b11100110
};

/* this setup indicates the location of all the pins to there 
   coordinating pin */

const int sw = 3; // hooked up pin
const int pb1 = 2; // " "
const int org = 4; // LEDs
const int red = 5; // " "
const int grn = 6; // " "
const int clockPin = 11; // Shift register attached to the 7 segment display
const int dataPin = 12;
const int latchPin = 13;

byte pb1Num = 0; // bytes are for the display of input signals of the pushbutton
byte pos1 = 0; // variable used to establish base servo position
byte pos2 = 0; // variable used to establish shoulder servo position
byte swNum = 0; // bytes used for on and off function of the switch
int i = 0;


void setup() {
  pinMode(sw, INPUT);
  pinMode(pb1, INPUT);
  pinMode(org, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(grn, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  base.attach(9);
  shoulder.attach(10);
  Serial.begin(9600);
  Serial.flush();
}

void loop() {
  swNum = digitalRead(sw);
  pb1Num = digitalRead(pb1);
  
  
  if (swNum == 0 && pb1Num == 0) {
    digitalWrite(red, HIGH);
    digitalWrite(org, LOW);
  }
  if (swNum == 1 || pb1Num == 1) {
    digitalWrite(org, HIGH);
    digitalWrite(red, LOW);
    Serial.print("Switch is now:  ");
    Serial.println(swNum);
    Serial.print("Pushbutton 1 is now: ");
    Serial.println(pb1Num);
    turn();
  } 
}

void turn() {
  for (pos1 = 10; pos1 <170; pos1 += 1) {
    base.write(pos1);
    delay(7);
  }
  for (pos1 = 170; pos1 >=10; pos1 -= 1) {
    base.write(pos1);
    delay(7);
  }
  base.write(92);
  if (pos1 = 11) {
    show(numbers[i]);
  }
  delay(2000);
  i++;
  if (i > 10) { i = 0; }
}

void show( byte number){
  for(int j = 0; j <= 7; j++){
    byte toWrite = number & (0b10000000 >> j);
    if(!toWrite) { continue; }
    shiftIt(toWrite);
  }
}
void shiftIt (byte data){ 
  digitalWrite(latchPin, LOW);
    for (int k=0; k <= 7; k++){
      digitalWrite(clockPin, LOW);
      if ( data & (1 << k) ) {
        digitalWrite(dataPin, LOW); // turn “On”
      }
      else{
        digitalWrite(dataPin, HIGH); // turn “Off”
      }
      // and clock the bit in
      digitalWrite(clockPin, HIGH);
    }
    //stop shifting out data
    digitalWrite(clockPin, LOW); 
    //set latchPin to high to lock and send data
    digitalWrite(latchPin, HIGH);
}

However, the display comes out choppy.

What does this mean? Are the numbers not displayed correctly, or what?

  Serial.begin(9600);
  Serial.flush();

Which version of the IDE are you using? In the pre 1.0 case, flush() dumps all unread data in the incoming serial buffer. Doing so immediately after opening the serial port is silly, as there won't be any data to flush. In the 1.0 or later case, flush() blocks until the outgoing serial buffer is empty. Since you haven't written anything to the serial port, the outgoing buffer is empty, and flush() won't do anything. Again, calling it is silly.

  if (pos1 = 11) {

After assigning the value of 11 to pos, the assignment operator returns the value assigned. Your if test might as well read:

  if(11)

which will always be true, You most likely wanted == there, not =.

    if(!toWrite) { continue; }
    shiftIt(toWrite);

If not toWrite, skip to the end of the loop. Otherwise, call the function, and then end the loop. Convoluted logic, when

   if(toWrite)
     shiftIf(toWrite);

would be more intuitive. At least, it is to me.

Sorry, I mean that the display coming from the 7 segment will only illuminate one of the LEDs instead of all the LEDs of the number I want displayed. I.E I want 8 but only F is on yet A-E and G are only on for a split second.

I have version 1.0. I'm still learning the program; I thought flush was meant to clear old serial from before so it makes a clear screen for a new program.

When I put == over = the display is continuously on full as to show 8. I changed it back and get the same result.

What exactly does this mean as well for I was using someone else’s code…

void show( byte number){
  for(int j = 0; j <= 7; j++){
    byte toWrite = number & (0b10000000 >> j);
    if(toWrite)
    shiftIt(toWrite);

What exactly does this mean as well for I was using someone else's code...

I would suggest that you look at the reference page, and add some Serial.print() statements to work that out for yourself. You will remember for far longer than if we explain.

The key parts are the bit shift (>>) and the and (&). The bit shift is moving the position of the 1 around in the constant. That new value is then anded with the number you want to display. It is, effectively, forming a mask. When the particular bit in number matches the shuffled around bit in the mask, the masked value is shifted out, lighting one segment of the 7 segment display.

Since the loop loops 7 times, each of the 7 segments may, or may not be, lit, depending on what the bit pattern in number is.

If number is 0b01100000, you can see that two bits are true, so, the loop should call shiftIt() twice, to light 2 of the segments. If the correct pins of the 7 segment display are connected to the correct Arduino pins, the 2 segments that light up should be the 2 segments on the right side of the display, forming a 1.

Since the loop loops 7 times,

{{cough} eight {{cough}}

{{cough} eight {{cough}}

Can I get you a cough drop, while we forget about my error?

How have you got the LED display wired? Have you got a separate resistor between each segment of the LED and it's digital IO pin, or are you being lazy and only putting 1 resistor on the anode?

"seperate Resistor"

CrossRoads:
“seperate Resistor”

Pardon?

See reply #7

Ah, slip of the LED there. And there was me thinking you were saying I was misspelling sepArate.

Yes, you had LED instead of resistor. SepArate just didn't look right for some reason.

Anyway, let's let this thread get back on topic ...

Thank you PaulS for the help. Turns out it was the positioning in the actual coding than the code itself.

void loop() {
  swNum = digitalRead(sw);
  pb1Num = digitalRead(pb1);    
  if (ii = 92) {
    show(numbers[i]);
  }
  if (i > 10) { i = 0; }
  
  if (swNum == 0 && pb1Num == 0) {
    digitalWrite(red, HIGH);
    digitalWrite(org, LOW);
  }
  if (swNum == 1 || pb1Num == 1) {
    i++;
    digitalWrite(org, HIGH);
    digitalWrite(red, LOW);
    Serial.print("Switch is now:  ");
    Serial.println(swNum);
    Serial.print("Pushbutton 1 is now: ");
    Serial.println(pb1Num);
    turn(ii);
  } 
}

What I had done was taken the show function out of the deciding IF statement. So what was happening is that it was the correct code and the number was being displayed, yet only for a fraction of a second. So I took it out of there to place it ahead of there in which to actually see the number. Thank you for your time.

ii = 92 is always true

Not if the servo is in motion. Because once the pushbutton or switch change states to 1 than the i (indicating the number needing to be shown on the seven segment) will move up a number. When this happens the program looks only into the loop of the if statement which says to turn the servo and not the ii = 92 statement. Once the turn is over, the servo is back to 92 at rest position and the number has increased by one. Also, the segment may display the increased number after the button or switch has been hit since the turn does pass 92 once. However, it will only be for a split second.

Not if the servo is in motion. Because once the pushbutton or switch change states to 1 than the i (indicating the number needing to be shown on the seven segment) will move up a number. When this happens the program looks only into the loop of the if statement which says to turn the servo and not the ii = 92 statement. Once the turn is over, the servo is back to 92 at rest position and the number has increased by one. Also, the segment may display the increased number after the button or switch has been hit since the turn does pass 92 once. However, it will only be for a split second.

What are you talking about? It's important to quote whatever you are responding to. If you are doubting that ii = 92 is always true, then you have to learn the difference between the assignment operator (=) and the equality operator (==).

The assignment operator that you are using returns a value - the value that was assigned. The conditional part of your if test is 92, which is true, by definition.

It appears that you want if(ii == 92), not if(ii = 92).