C64 Arduino Kernal switcher

I need some one to look at this code I am real new at programming like I know nothing. This is not my code its from a youtuber "Adrian" and it seems to be buggy. it's meant to switch the address by manipulating the A13 and 14 lines on a 27c256 by holding down the Restore Key for 2 sec.
I would like to get this working so I don't have to drill holes in my c64

first problem is there in an error's in the code I think I fixed that part it has some thing to do with the LED turning on at power up. there error I found was when compiling it at line 50 " digitalWrite(PowerLED, LED); // turn on the power LED " I changed it to " digitalWrite(PowerLED, HIGH); // turn on the power LED "

Second when I hit the restore key for 2 sec 70% of the time it just Resets the Commodore 64 with out switching with out changing the pins 5, and 6 High or low. I know my wiring works I can switch to the 4 Images in the rom with a basic toggle switch.
when it just happens to work with the micro controller can switch from the 4 images fine and see pin 5 "A13" and pin 6 "A14" going high or low. So it is working some of the times but its hit or miss.

any help would be wonderful thanks.

this is the video of Adrian doing this mod.
"How-To: C64 Arduino based Restore Key/Four Kernal Switcher/Hard Reset - YouTube"

here is a link to the Code. pastebin.com/disjXJVh

this is what I have going on here

#include <EEPROM.h>

// C64 Kernel Switcher and Restore Key Reset/Selector
// version 1.1 -- 26-March-2019
// By Adrian Black

// restore key mod: https://www.breadbox64.com/blog/c64-restore-mod/


const int PowerLED = 4;      // Output Power LED
const int PowerLEDAlt = 13;  // Output Power LED (onboard LED)
const int A13 = 5;           // Output EPROM Address Line 13
const int A14 = 6;           // Output EPROM Address Line 14
const int ResetLine = 7;     // Output to /RESET line
const int EXROMLine = 3;     // Output the /EXROM line
const int RestoreKey = 8;    // Input Restore key

int RestoreDelay = 2000;  // 2000ms delay for restore key
const int FlashSpeed = 75; // LED Flash delay

const unsigned long repeatdelay = 500; // used for debouncing
 
int CurrentROM; // which rom is select (0-3)
int debouncecounter = 0;       // how many times we have seen new value (for debounce)
int debouncereading;
int debounce_count;
int RestoreHeld;
unsigned long TimeHeld; // amount of time Resotre is held down

int buttonDuration = 0; // for keeping track of how long restore is held down
boolean buttonHeld = 0; // for keeping track when you are holding down 
boolean Released = 0; // Keeping track when the restore key is released
boolean holdingRestore = 0; // Keeping track if you are holding restore
boolean resetSystem = 0; // keep track wheter to reset

int buttonInput; // used to return if restore is held
unsigned long time; //used to keep track of millis output
unsigned long htime; //used to keep track of millis output
unsigned long btime; //used to keep track of bounce millis output

void setup() {
  pinMode(PowerLED, OUTPUT);
  pinMode(PowerLEDAlt, OUTPUT);
  pinMode(A13, OUTPUT);
  pinMode(A14, OUTPUT);
  pinMode(ResetLine, INPUT);
  pinMode(EXROMLine, INPUT);
  pinMode(RestoreKey, INPUT);

  digitalWrite(PowerLED, LED); // turn on the power LED

  digitalWrite(ResetLine, LOW); // keep the system reset
  pinMode(ResetLine, OUTPUT); // switch reset line to OUTPUT so it can hold it low
  digitalWrite(ResetLine, LOW); // keep the system reset

  CurrentROM = EEPROM.read(1);
  SetSlot(CurrentROM);
  delay(200);
  pinMode(ResetLine, INPUT); // set the reset pin back to high impedance which releases the INPUT line
  delay(1000); // wait 1000ms 
  FlashLED(CurrentROM);  // flash the power LED to show the current state
  
  // all set!
}

void loop() {
  buttonInput = readButton(); delay(500);
  time = millis(); // load the number of milliseconds the arduino has been running into variable time
  if (buttonInput == 1) {
    if (!buttonHeld) {
      htime = time; TimeHeld = 0; buttonHeld = 1; } //restore button is pushed
    else { 
      TimeHeld = time - htime; } // button is being held down, keep track of total time held.
  }
  if (buttonInput == 0) {
    if (buttonHeld) {
      Released = 1; buttonHeld = 0; htime = millis(); TimeHeld = 0; //restore button not being held anymore
    } 
  }
  
  if (TimeHeld > RestoreDelay && !Released) { // do this when the time the button is held is longer than the delay and the button is released
    htime = millis();
    if (holdingRestore == 0) { FlashLED(CurrentROM); holdingRestore = 1; resetSystem = 1; } // first time this is run, so flash the LED with current slot and reset time held. Set the holding restore variable.
    else {
      if (CurrentROM < 3) { CurrentROM++; SaveSlot(CurrentROM); } // or you've already been holding restore, so increment the current ROM slot otherwise reset it to 0
      else { CurrentROM = 0; SaveSlot(CurrentROM); }
      if (TimeHeld > RestoreDelay) { TimeHeld = 0;}  // reset the time held
      FlashLED(CurrentROM); //flash the LED
    }
  }
  
  if (Released) {
    //if time held greater than restore delay, reset the system, set the current rom slot, reselt the time held and holding restore
    if (resetSystem) { // on do this if the reset system has been set above
      htime = millis();
      resetSystem = 0;
      holdingRestore = 0;
      Released = 0;
      digitalWrite(ResetLine, LOW); // keep the system reset
      digitalWrite(EXROMLine, LOW); // keep the EXROM line low
      pinMode(ResetLine, OUTPUT);
      pinMode(EXROMLine, OUTPUT);
      digitalWrite(ResetLine, LOW); // keep the system reset
      digitalWrite(EXROMLine, LOW); // keep the EXROM line low
      delay(50); // wait 50ms
      SetSlot(CurrentROM); // select the appropriate kernal ROM
      delay(200); // wait 200ms before releasing RESET line
      pinMode(ResetLine, INPUT); // set the reset pin back to high impedance so computer boots
      delay(300); // wait 300ms before releasing EXROM line
      pinMode(EXROMLine, INPUT); // set the reset pin back to high impedance so computer boots
    } else { //otherwise do nothing
      htime = millis(); Released = 0; resetSystem = 0; holdingRestore = 0;
    }
  }
// finished with loop  
}

int readButton() {
 if (!digitalRead(RestoreKey) && (millis() - btime >= repeatdelay)) {
  for(int i = 0; i < 10; i++)
    {
      debouncereading = !digitalRead(RestoreKey);

      if(!debouncereading && debouncecounter > 0)
      {
        debouncecounter--;
      }
      if(debouncereading)
      {
        debouncecounter++; 
      }
      // If the Input has shown the same value for long enough let's switch it
      if(debouncecounter >= debounce_count)
      {
        btime = millis();
        debouncecounter = 0;
        RestoreHeld = 1;
      }
    delay (10); // wait 10ms
    }
   } else {
    RestoreHeld = 0;
   }
return RestoreHeld;
}


void SaveSlot(int CurrentRomSlot) {
  // Save Current ROM selection (0-3) into EPROM
  EEPROM.write(1,CurrentRomSlot);
}

void FlashLED(int flashcount) {
    // Flash the LED to represent which ROM slot is selected
    switch (flashcount) {
    case 0:
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      break;
    case 1:
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      delay(FlashSpeed);
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      break;
    case 2:
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      delay(FlashSpeed);
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      delay(FlashSpeed);
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      break;
    case 3:
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      delay(FlashSpeed);
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      delay(FlashSpeed);
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      delay(FlashSpeed);
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      break;
    default:
      digitalWrite(PowerLED, LOW);
      digitalWrite(PowerLEDAlt, LOW);
      delay(FlashSpeed);
      digitalWrite(PowerLED, HIGH);
      digitalWrite(PowerLEDAlt, HIGH);
      break;
  }
}

void SetSlot(int DesiredRomSlot) {
    // Select the actual ROM slot being used
    switch (DesiredRomSlot) {
    case 0:
      digitalWrite(A13, LOW);
      digitalWrite(A14, LOW);
      break;
    case 1:
      digitalWrite(A13, HIGH);
      digitalWrite(A14, LOW);
      break;
    case 2:
      digitalWrite(A13, LOW);
      digitalWrite(A14, HIGH);
      break;
    case 3:
      digitalWrite(A13, HIGH);
      digitalWrite(A14, HIGH);
      break;
    default:
      digitalWrite(A13, LOW);
      digitalWrite(A14, LOW);
      break;
  }
}

Please post the code here

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I have glanced at the code.

The loop is unresponsive, and the button has some things that go by timing.

Have you tried only using short and very long presses?

It may be a confusion of short, in between and long pressing.

Also the loop has a 500 millisecond delay, which might mean that a button press goes tots missed, or becomes misinterpreted.

The code is something to behold, and I don't really mean that as a compliment.

Where is what it does to provide the functionality you want documented?

a7

ok thanks I fixed the post.

ok i got it to work..

Wow a C64 ! This was my first own "computer" or should I say microcontroller with graphic and soundcard? This was 39 years ago. Oh man that were times. "Lego"-graphics 1 MHz clock
0,064 MB RAM

best regards Stefan

Hello,
Can you help me I am in the same position as you were, everything looks great on my end but the switching does not function as expected.

Can you explain what you did to make this work, please?

Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.