# SOLVED! Problems with 16 Segment Display not displaying correct array sequence

Hi All

I am having an issue with the values in an arrays outputing to my 16 segment display.
It’s for manually selecting gears on a driving game.

I am using momentary buttons in a pull down configuration to activate the increment and decrement cycle.
I have tested the sequence with a simple sketch and all the characters are present.

But when I tried increment and decrement the characters +1 and -1 respectively they skip in two’s,the array should display a gear selection sequence which translates into the characters R N 1 2 3 4 5 6 7 on the 16 Segment Display.
Example of display behaviour when upshift or decrement button is pushed:

Upshifting

R 1 3 5 7 ( Meaningless display after the last increment of 7)
or
N 2 4 6 ( Meaningless display after the last increment of 6)

Down shifting
R 1 3 5 7 ( Meaningless display after the last decrement of R)
or
N 2 4 6 ( Meaningless display after the last decrement of N)

So I believe that calling the value from the array is skewing past the array size?

Here is my code for you to have a look if you would be so kind.

By the way this is my very first Arduino project any advice or guidance would be most welcome.

``````int data = 1;
int latch = 2;
int clock = 3;
int switchPin19 = 47;// GEAR SELECTION Up
int switchPin20 = 48;// GEAR SELECTION Down
int gearArray[] = {
1309L,11868L,15351L,1419L,1450L,3902L,5418L,5386L,9726L};//An Array of Decimal Values for the Alpha Numeric 16 segment display (AND) display R,N,1,2,3,4,5,6,7
int currentGear = 2;// will initialise the array staring point at 2 in the index
boolean lastButton19 = LOW;
boolean currentButton19 = LOW;

boolean lastButton20 = LOW;
boolean currentButton20 = LOW;

void setup()
{
pinMode(data, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(switchPin19, INPUT);
pinMode(switchPin20, INPUT);
updateAND(gearArray[currentGear]); // Sets the default gear on the AND - which will be "N"

}
// debounce the momentary buttons
boolean debounce19(boolean last19)
{
if(last19 != current19);
{
delay(5);
}
return current19;
}

boolean debounce20(boolean last20)
{
if(last20 != current20);
{
delay(5);
}
return current20;
}

void runDisplayCycle(long andValue)
{
long value, valueToDisplay;
value=0;
// Turn all the segments off
updateAND(0x00003FFF);
delay(50);
for (int i=0; i<15; i++)
{
value = value | (1 << i);
valueToDisplay = andValue | (value<<14);
updateAND(valueToDisplay);
delay(50);
}
}

void loop()
{

//  GEAR UP
currentButton19 = debounce19(lastButton19);
if (lastButton19 == LOW && currentButton19 == HIGH) //reads the input state

// DISPLAY SELECTED GEAR FROM ARRAY TO AND HERE
{
currentGear = (currentGear<8 ? currentGear+1 : currentGear);
currentGear++;
updateAND(gearArray[currentGear]);

}

digitalWrite (latch, HIGH);

{
//GEAR SHIFT DOWN
currentButton20 = debounce20(lastButton20);//
if (lastButton20 == LOW && currentButton20 == HIGH) //reads the input state

{
currentGear = (currentGear>0 ? currentGear-1 : currentGear);
currentGear--;
updateAND(gearArray[currentGear]);

}
}

digitalWrite (latch, HIGH);
}

void updateAND(long value)
{
byte byte1, byte2;
byte2 = (value & 0x0000FF00L)>>8;
byte1 = value & 0x000000FFL;

digitalWrite (latch, LOW);
shiftOut(data, clock, MSBFIRST, byte2);
shiftOut(data, clock, MSBFIRST, byte1);
digitalWrite (latch, HIGH);
}
``````
``````  val20 = digitalRead(switchPin20) == LOW;
digitalWrite (latch, HIGH);
``````

Interesting construct - is it doing what you intended?

``````byte byte1, byte2;
byte2 = (value & 0x0000FF00L)>>8;
byte1 = value & 0x000000FFL;
``````

Why write three lines when two would do?

``````  byte byte2 = (value >> 8) & 0xFF;
byte byte1 = value & 0x000000FFL;
``````

AWOL:

``````  val20 = digitalRead(switchPin20) == LOW;
``````

digitalWrite (latch, HIGH);

``````

Interesting construct - is it doing what you intended?
``````

Honestly I don’t quite know I initially had this in place to turn off and ledPin which had previously been High when a momentary button was pressed.
I have managed to get so far with the Arduino IDE which is a great for a beginner like myself although I know I have a long way to go yet.

AWOL:

``````byte byte1, byte2;
``````

byte2 = (value & 0x0000FF00L)>>8;
byte1 = value & 0x000000FFL;

``````

Why write three lines when two would do?

``````

byte byte2 = (value >> 8) & 0xFF;
byte byte1 = value & 0x000000FFL;

I have a very linear mind and as I am dealing with areas I don’t quite understand I am gleaning what I can to get things working.
Short answer I’m a long way from producing elegant code as I’m sure you can see but having said that I so far touch wood everything I’ve done has functioned as I intended

Have you succeeded in getting just the display-segments part working?

[quote author=Runaway Pancake link=topic=190770.msg1410455#msg1410455 date=1380496097] Have you succeeded in getting just the display-segments part working? [/quote]

Hi Runaway Pancake

Thanks for the reply the display works as expected. I think my issue may be here

currentGear = (currentGear>0 ? currentGear-1 : currentGear); currentGear--; updateAND(gearArray[currentGear]);

If I delete the line in bold italics hopefully this will stop the increments and decrements jumping in twos and passing through the bounds of the array!

Yep that solved it, Thanks all for your help. Will post the finished project when I complete the build