Go Down

Topic: SOLVED! Problems with 16 Segment Display not displaying correct array sequence (Read 2028 times)previous topic - next topic

DOC-DARKNESS

Sep 29, 2013, 10:17 pmLast Edit: Sep 30, 2013, 09:47 pm by DOC-DARKNESS Reason: 1
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.

Code: [Select]
`int data = 1;int latch = 2;int clock = 3;int switchPin19 = 47;// GEAR SELECTION Upint switchPin20 = 48;// GEAR SELECTION Downint 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,7int currentGear = 2;// will initialise the array staring point at 2 in the indexchar val19 = digitalRead(switchPin19);char val20 = digitalRead(switchPin20);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 buttonsboolean debounce19(boolean last19){  boolean current19 = digitalRead(switchPin19);  if(last19 != current19);  {    delay(5);    current19 = digitalRead(switchPin19);  }  return current19;}boolean debounce20(boolean last20){  boolean current20 = digitalRead(switchPin20);  if(last20 != current20);  {    delay(5);    current20 = digitalRead(switchPin20);  }  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    if(val19 == digitalRead(switchPin19) == HIGH)      // DISPLAY SELECTED GEAR FROM ARRAY TO AND HERE    {      currentGear = (currentGear<8 ? currentGear+1 : currentGear);      currentGear++;      updateAND(gearArray[currentGear]);    }  val19 = digitalRead(switchPin19) == LOW;  if(val19 = digitalRead(switchPin19) == LOW)    digitalWrite (latch, HIGH);  {    //GEAR SHIFT DOWN    currentButton20 = debounce20(lastButton20);//      if (lastButton20 == LOW && currentButton20 == HIGH) //reads the input state      if(val20 == digitalRead(switchPin20) == HIGH)      {        currentGear = (currentGear>0 ? currentGear-1 : currentGear);        currentGear--;        updateAND(gearArray[currentGear]);        }  }  val20 = digitalRead(switchPin20) == LOW;  if(val20 = digitalRead(switchPin20) == LOW)    digitalWrite (latch, HIGH);}// Updates the Alphanumeric Displayvoid 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);}`

AWOL

#1
Sep 29, 2013, 10:22 pmLast Edit: Sep 29, 2013, 10:27 pm by AWOL Reason: 1
Code: [Select]
`  val20 = digitalRead(switchPin20) == LOW;  if(val20 = digitalRead(switchPin20) == LOW)    digitalWrite (latch, HIGH);`
Interesting construct - is it doing what you intended?

Code: [Select]
`byte byte1, byte2;  byte2 = (value & 0x0000FF00L)>>8;  byte1 = value & 0x000000FFL;`
Why write three lines when two would do?
Code: [Select]
`  byte byte2 = (value >> 8) & 0xFF;  byte byte1 = value & 0x000000FFL;`
"Pete, it's a fool (who) 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.

DOC-DARKNESS

Code: [Select]
`  val20 = digitalRead(switchPin20) == LOW;  if(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.

Code: [Select]
`byte byte1, byte2;  byte2 = (value & 0x0000FF00L)>>8;  byte1 = value & 0x000000FFL;`
Why write three lines when two would do?
Code: [Select]
`  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

runaway_pancake

Have you succeeded in getting just the display-segments part working?
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

DOC-DARKNESS

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

Hi Runaway Pancake

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

Quote
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!

DOC-DARKNESS

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

Go Up