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
char 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 buttons
boolean 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 Display
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);
}