Decrementing a byte value. --Solved

Ive been trying to program my buttons to increment and decrement a byte value and roll it over back to nine when it reaches zero. the number is stored as two nibbles in one byte and the incrementation works OK but when i decrement the value there seems to be a problem in the line that evaluates it to zero. Ive tried rewriting it every reasonable way i can think of and even a load of ways that didn’t seem reasonable just to see what happened, Ive buggered about with this particular line for a full day now.

I could change the numbers to ints, adjust them and change them back but I don’t understand why i cant perform the subtraction as the number rolls over and feel like i’m missing something fundamental now.

I am generally not familiar with any type of programming at all and only started learning to bugger about with the arduino. Thanks in advance

#include <Wire.h>

byte Array[4]={B00110010,B00110001,B00000011};                                //Define the array where we will store the time and date from the RTC  32,31,13
int index=0;                                    //An index variable to keep track of our plave in the array

//Define the buttons 
const int butA=A2;                            //+ button
const int butB=A1;                            //set button
const int butC=A3;                            //- button


void setup()
{
  pinMode(butA, INPUT);
 pinMode(butB, INPUT);
 pinMode(butC, INPUT);
  //starting communication with the ds3231
Wire.begin();
  Serial.begin(9600);

}


void loop() 
{
     displaytime();
     
         if(digitalRead(butA)==HIGH)
         {
            delay(100);
            Array[index]++;
      
              if((Array[index]&0xf)>9)
                {
                   Array[index]=Array[index]+7;
                      if((Array[index]>>4&0xf)>5)
                      {
                        Array[index]=B00000000;
                      }
                }
                displaytime();
                Serial.print('\t');
                 Serial.print("++");
         }


  
  
       if(digitalRead(butB)==HIGH)
         {
             delay(200);
             index=index+1;
    
              if(index>2)
                  {
                  index=0;
                  }
                 }
        
        
  if(digitalRead(butC)==HIGH)
    {
      delay(100);
      Array[index]--;
       
        if(Array[index]<0x00)
          {
            Array[index]=Array[index]-7;
         
   
          }
          displaytime();
          Serial.print('\t');
          Serial.print("--");
    }
}


void displaytime()
{
  int secs,secb,mins,minb,hours,hourb;
 secs=Array[0]&0xf;
 secb=(Array[0]>>4)&0xf;
 mins=Array[1]&0xf;
 minb=(Array[1]>>4)&0xf;
 hours=Array[2]&0xf;
 hourb=(Array[2]>>4)&0xf;
 Serial.print('\n');
 Serial.print(binbin(hourb));
 Serial.print(" ");
 Serial.print(hourb);
 Serial.print(" ");
 Serial.print(binbin(hours));
 Serial.print(" ");
 Serial.print(hours);
 Serial.print('\t');
 Serial.print(binbin(minb));
 Serial.print(" ");
 Serial.print(minb);
 Serial.print(" ");
 Serial.print(binbin(mins));
 Serial.print(" ");
 Serial.print(mins);
 Serial.print('\t');
 Serial.print(binbin(secb));
 Serial.print(" ");
 Serial.print(secb);
 Serial.print(" ");
 Serial.print(binbin(secs));
 Serial.print(" ");
 Serial.print(secs);
 
}


char *binbin(int n)
{
    static char bin[9];
    int x;

    for(x=0;x<8;x++)
    {
        bin[x] = n & 0x80 ? '1' : '0';
        n <<=1;
    }
    bin[x]='\0';
    return(bin);
}

After all that and finally deciding to ask I think i might be onto it now. Array[index] cant ever be less than 0 because its unsigned so the value will need to roll over when its above nine i think

              if((Array[index]&0xf)>9)
                {
                   Array[index]=Array[index]+7;

If the value is greater than 9, add 7. How does that make sense?

if((Array[index]>>4&0xf)>5)

Yourspacekeyappearstobebroken.Oughttogetthatfixed.

      if(digitalRead(butB)==HIGH)
         {
             delay(200);
             index=index+1;

Why does the set button change the value of index?

You REALLY should look at the state change detection example, instead of using delay().

I don’t really understand trying to use the high and low nibbles of a byte to store different values, if that is what you are doing, while wasting 16 bits to store a value that can only be 0, 1, or 2.

You are also wasting memory storing pin numbers in ints, when a pin number can’t ever exceed the value that fits in a byte.

       if(Array[index]<0x00)

Since a byte is an unsigned value, it can NEVER be less than 0.

The reason the digits are stored as nibble is because thats how the hardware clock stores them. you add 7 to a 9 so that it cant count through hex values all the way up to fifteen. Its a single decimal digit stored as half a byte. the other digits stored as the other half. the set button changes the value of index because i want it to move a place in the array, thats what the set button is for. Thanks anyway though

If you want to roll over going forward:

counter = (counter++) % rollover

So say rollover is 9, 0 thru 8 divided by 9 will equal 0 with remainder of 0 thru 8. 9 reverts to 0.

If you want to go the other way, simply subtract the above from the rollover,

c2 = (rollover - counter)

You'll have to do some tweaking though as counter goes from 0 to 8 where c2 goes from 9 to 1.

Another thought is if you can get by with 8 or perhaps use 16, then why not use a 3 or 4 bit def in a struct and then it will rollover all on its own?